1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Random
17
18///|
19pub impl 
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Random
Random
with to_req(self :
type Random
Random
) ->
type Req
Req
= "%identity"
20 21///| 22extern "c" fn uv_random_make() ->
type Random
Random
= "moonbit_uv_random_make"
23 24///| 25extern "c" fn uv_random( 26 uv :
type Loop Bytes
Loop
,
27 req :
type Random
Random
,
28 buffer :
Bytes
Bytes
,
29 buffer_offset :
Int
Int
,
30 buffer_length :
Int
Int
,
31 flags :
Int
Int
,
32 random_cb : (
type Random
Random
,
Int
Int
,
Bytes
Bytes
,
Int
Int
,
Int
Int
) ->
Unit
Unit
33) ->
Int
Int
= "moonbit_uv_random"
34 35///| 36pub fn
type Loop Bytes
Loop
::
(self : Loop, buffer : @bytes.View, flags : Int, random_cb : (@bytes.View) -> Unit, error_cb : (Errno) -> Unit) -> Random raise Errno
random
(
37
Loop
self
:
type Loop Bytes
Loop
,
38
@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
,
39
Int
flags
:
Int
Int
,
40
(@bytes.View) -> Unit
random_cb
: (
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
,
41
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
42) ->
type Random
Random
raise
type! Errno Status
Errno
{
43 fn
(Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb
(_,
Int
status
,
Bytes
buffer
:
Bytes
Bytes
,
Int
offset
,
Int
length
) {
44 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
45
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
46 } else { 47
(@bytes.View) -> Unit
random_cb
(
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:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
length
])
48 } 49 } 50 51 let
Random
req
=
() -> Random
uv_random_make
()
52 let
Int
buffer_offset
=
@bytes.View
buffer
.
(self : @bytes.View) -> Int

Retrieves the start index of the view.

start_offset
()
53 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:

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

Retrieves the underlying Bytes from a View.

data
()
55 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
(
56
Loop
self
,
Random
req
,
Bytes
buffer
,
Int
buffer_offset
,
Int
buffer_length
,
Int
flags
,
(Random, Int, Bytes, Int, Int) -> Unit
uv_random_cb
,
57 ) 58 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
59 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
60 } 61 return
Random
req
62} 63