1///|
2type Random
3
4///|
5extern "c" fn uv_random_make() -> 
type Random
Random
= "moonbit_uv_random_make"
6 7///| 8extern "c" fn uv_random( 9 uv :
type Loop Bytes
Loop
,
10 req :
type Random
Random
,
11 buffer :
Bytes
Bytes
,
12 buffer_offset :
Int
Int
,
13 buffer_length :
Int
Int
,
14 flags :
Int
Int
,
15 random_cb : (
type Random
Random
,
Int
Int
,
Bytes
Bytes
,
Int
Int
,
Int
Int
) ->
Unit
Unit
16) ->
Int
Int
= "moonbit_uv_random"
17 18///| 19pub fn
(self : Loop, buffer : @bytes.View, flags : Int, random_cb : (Random, @bytes.View) -> Unit, error_cb : (Random, Errno) -> Unit) -> Unit!Errno
random
(
20
Loop
self
:
type Loop Bytes
Loop
,
21
@bytes.View
buffer
:
type @bytes.View

A @bytes.View represents a view into a section of a Bytes without copying the data.

Example

let bs = b"\x00\x01\x02\x03\x04\x05"
let bv = bs[1:4]
assert_eq(bv.length(), 3)
assert_eq(bv[0], b'\x01')
assert_eq(bv[1], b'\x02')
assert_eq(bv[2], b'\x03')
@bytes.View
,
22
Int
flags
:
Int
Int
,
23
(Random, @bytes.View) -> Unit
random_cb
: (
type Random
Random
,
type @bytes.View

A @bytes.View represents a view into a section of a Bytes without copying the data.

Example

let bs = b"\x00\x01\x02\x03\x04\x05"
let bv = bs[1:4]
assert_eq(bv.length(), 3)
assert_eq(bv[0], b'\x01')
assert_eq(bv[1], b'\x02')
assert_eq(bv[2], b'\x03')
@bytes.View
) ->
Unit
Unit
,
24
(Random, Errno) -> Unit
error_cb
: (
type Random
Random
,
type! Errno Status
Errno
) ->
Unit
Unit
25) ->
Unit
Unit
!
type! Errno Status
Errno
{
26 fn
(Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb
(
Random
random
,
Int
status
,
Bytes
buffer
:
Bytes
Bytes
,
Int
offset
,
Int
length
) {
27 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
28
(Random, Errno) -> Unit
error_cb
(
Random
random
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
29 } else { 30
(Random, @bytes.View) -> Unit
random_cb
(
Random
random
,
Bytes
buffer
[
Int
offset
:
Int
offset
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
Int
length
])
31 } 32 } 33 34 let
Random
req
=
() -> Random
uv_random_make
()
35 let
Int
buffer_offset
=
@bytes.View
buffer
.
(self : @bytes.View) -> Int

Retrieves the start index of the view.

start_offset
()
36 let
Int
buffer_length
=
@bytes.View
buffer
.
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

test "View::length" {
  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
}
length
()
37 let
Bytes
buffer
=
@bytes.View
buffer
.
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

data
()
38 let
Int
status
=
(uv : Loop, req : Random, buffer : Bytes, buffer_offset : Int, buffer_length : Int, flags : Int, random_cb : (Random, Int, Bytes, Int, Int) -> Unit) -> Int
uv_random
(
39
Loop
self
,
Random
req
,
Bytes
buffer
,
Int
buffer_offset
,
Int
buffer_length
,
Int
flags
,
(Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb
,
40 ) 41 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
42 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
43 } 44} 45