1///|
2type Udp
3
4///|
5pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle for type Udp
Udp with to_handle(self : type Udp
Udp) -> type Handle
Handle = "%identity"
6
7///|
8pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle for type Udp
Udp with of_handle(self : type Handle
Handle) -> type Udp
Udp = "%identity"
9
10///|
11extern "c" fn uv_udp_make() -> type Udp
Udp = "moonbit_uv_udp_make"
12
13///|
14extern "c" fn uv_udp_init(uv : type Loop Bytes
Loop, udp : type Udp
Udp) -> Int
Int = "moonbit_uv_udp_init"
15
16///|
17pub fn type Udp
Udp::(loop_ : Loop) -> Udp!Errno
new(Loop
loop_ : type Loop Bytes
Loop) -> type Udp
Udp!type! Errno Status
Errno {
18 let Udp
udp = () -> Udp
uv_udp_make()
19 let Int
result = (uv : Loop, udp : Udp) -> Int
uv_udp_init(Loop
loop_, Udp
udp)
20 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
21 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
22 }
23 Udp
udp
24}
25
26///|
27extern "c" fn uv_udp_connect(handle : type Udp
Udp, addr : type Sockaddr Bytes
Sockaddr) -> Int
Int = "moonbit_uv_udp_connect"
28
29///|
30pub fn[Sockaddr : trait ToSockaddr {
to_sockaddr(Self) -> Sockaddr
}
ToSockaddr] type Udp
Udp::(self : Udp, addr : Sockaddr) -> Unit!Errno
connect(
31 Udp
self : type Udp
Udp,
32 Sockaddr
addr : type parameter Sockaddr
Sockaddr
33) -> Unit
Unit!type! Errno Status
Errno {
34 let Int
result = (handle : Udp, addr : Sockaddr) -> Int
uv_udp_connect(Udp
self, Sockaddr
addr.(Sockaddr) -> Sockaddr
to_sockaddr())
35 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
36 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
37 }
38}
39
40///|
41extern "c" fn uv_udp_bind(handle : type Udp
Udp, addr : type Sockaddr Bytes
Sockaddr, flags : UInt
UInt) -> Int
Int = "moonbit_uv_udp_bind"
42
43///|
44pub fn[Sockaddr : trait ToSockaddr {
to_sockaddr(Self) -> Sockaddr
}
ToSockaddr] type Udp
Udp::(self : Udp, addr : Sockaddr, flags : UdpFlags) -> Unit!Errno
bind(
45 Udp
self : type Udp
Udp,
46 Sockaddr
addr : type parameter Sockaddr
Sockaddr,
47 UdpFlags
flags : type UdpFlags UInt
UdpFlags
48) -> Unit
Unit!type! Errno Status
Errno {
49 let Int
result = (handle : Udp, addr : Sockaddr, flags : UInt) -> Int
uv_udp_bind(Udp
self, Sockaddr
addr.(Sockaddr) -> Sockaddr
to_sockaddr(), UdpFlags
flags.UInt
_)
50 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
51 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
52 }
53}
54
55///|
56type UdpSend
57
58///|
59pub impl trait ToReq {
to_req(Self) -> Req
}
ToReq for type UdpSend
UdpSend with to_req(self : type UdpSend
UdpSend) -> type Req
Req = "%identity"
60
61///|
62extern "c" fn uv_udp_send_make() -> type UdpSend
UdpSend = "moonbit_uv_udp_send_make"
63
64///|
65extern "c" fn uv_udp_send(
66 send : type UdpSend
UdpSend,
67 udp : type Udp
Udp,
68 bufs_base : type FixedArray[A]
FixedArray[Bytes
Bytes],
69 bufs_offset : type FixedArray[A]
FixedArray[Int
Int],
70 bufs_length : type FixedArray[A]
FixedArray[Int
Int],
71 addr : @c.Nullable[type Sockaddr Bytes
Sockaddr],
72 cb : (type UdpSend
UdpSend, Int
Int) -> Unit
Unit
73) -> Int
Int = "moonbit_uv_udp_send"
74
75///|
76pub fn[Sockaddr : trait ToSockaddr {
to_sockaddr(Self) -> Sockaddr
}
ToSockaddr] type Udp
Udp::(self : Udp, data : Array[@bytes.View], send_cb : (UdpSend) -> Unit, error_cb : (UdpSend, Errno) -> Unit, addr? : Sockaddr) -> Unit!Errno
send(
77 Udp
self : type Udp
Udp,
78 Array[@bytes.View]
data : type Array[T]
An Array is a collection of values that supports random access and can
grow in size.
Array[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],
79 (UdpSend) -> Unit
send_cb : (type UdpSend
UdpSend) -> Unit
Unit,
80 (UdpSend, Errno) -> Unit
error_cb : (type UdpSend
UdpSend, type! Errno Status
Errno) -> Unit
Unit,
81 Sockaddr?
addr? : type parameter Sockaddr
Sockaddr
82) -> Unit
Unit!type! Errno Status
Errno {
83 fn (UdpSend, Int) -> Unit
cb(UdpSend
req : type UdpSend
UdpSend, Int
status : Int
Int) {
84 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
85 (UdpSend, Errno) -> Unit
error_cb(UdpSend
req, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
86 } else {
87 (UdpSend) -> Unit
send_cb(UdpSend
req)
88 }
89 }
90
91 let UdpSend
req = () -> UdpSend
uv_udp_send_make()
92 let Int
bufs_size = Array[@bytes.View]
data.(self : Array[@bytes.View]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
}
length()
93 let FixedArray[Bytes]
bufs_base : type FixedArray[A]
FixedArray[Bytes
Bytes] = type FixedArray[A]
FixedArray::(len : Int, init : Bytes) -> FixedArray[Bytes]
Creates a new fixed-size array with the specified length, initializing all
elements with the given value.
Parameters:
length : The length of the array to create. Must be non-negative.
initial_value : The value used to initialize all elements in the array.
Returns a new fixed-size array of type FixedArray[T] with length
elements, where each element is initialized to initial_value.
Throws a panic if length is negative.
Example:
test "FixedArray::make" {
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
}
test "panic FixedArray::make/negative_length" {
ignore(FixedArray::make(-1, 0))
}
WARNING: A common pitfall is creating with the same initial value, for example:
test {
let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
two_dimension_array[0][5] = 10
assert_eq(two_dimension_array[5][5], 10)
}
This is because all the cells reference to the same object (the FixedArray[Int] in this case).
One should use makei() instead which creates an object for each index.
make(Int
bufs_size, [])
94 let FixedArray[Int]
bufs_offset = type FixedArray[A]
FixedArray::(len : Int, init : Int) -> FixedArray[Int]
Creates a new fixed-size array with the specified length, initializing all
elements with the given value.
Parameters:
length : The length of the array to create. Must be non-negative.
initial_value : The value used to initialize all elements in the array.
Returns a new fixed-size array of type FixedArray[T] with length
elements, where each element is initialized to initial_value.
Throws a panic if length is negative.
Example:
test "FixedArray::make" {
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
}
test "panic FixedArray::make/negative_length" {
ignore(FixedArray::make(-1, 0))
}
WARNING: A common pitfall is creating with the same initial value, for example:
test {
let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
two_dimension_array[0][5] = 10
assert_eq(two_dimension_array[5][5], 10)
}
This is because all the cells reference to the same object (the FixedArray[Int] in this case).
One should use makei() instead which creates an object for each index.
make(Int
bufs_size, 0)
95 let FixedArray[Int]
bufs_length = type FixedArray[A]
FixedArray::(len : Int, init : Int) -> FixedArray[Int]
Creates a new fixed-size array with the specified length, initializing all
elements with the given value.
Parameters:
length : The length of the array to create. Must be non-negative.
initial_value : The value used to initialize all elements in the array.
Returns a new fixed-size array of type FixedArray[T] with length
elements, where each element is initialized to initial_value.
Throws a panic if length is negative.
Example:
test "FixedArray::make" {
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
}
test "panic FixedArray::make/negative_length" {
ignore(FixedArray::make(-1, 0))
}
WARNING: A common pitfall is creating with the same initial value, for example:
test {
let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
two_dimension_array[0][5] = 10
assert_eq(two_dimension_array[5][5], 10)
}
This is because all the cells reference to the same object (the FixedArray[Int] in this case).
One should use makei() instead which creates an object for each index.
make(Int
bufs_size, 0)
96 for Int
i in Int
0..<Array[@bytes.View]
data.(self : Array[@bytes.View]) -> Int
Returns the number of elements in the array.
Parameters:
array : The array whose length is to be determined.
Returns the number of elements in the array as an integer.
Example:
test "Array::length" {
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
}
length() {
97 FixedArray[Bytes]
bufs_base(FixedArray[Bytes], Int, Bytes) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // Index out of bounds
}
[i] = Array[@bytes.View]
data(Array[@bytes.View], Int) -> @bytes.View
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
ignore(arr[3]) // Index out of bounds
}
[i].(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
98 FixedArray[Int]
bufs_offset(FixedArray[Int], Int, Int) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // Index out of bounds
}
[i] = Array[@bytes.View]
data(Array[@bytes.View], Int) -> @bytes.View
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
ignore(arr[3]) // Index out of bounds
}
[i].(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
99 FixedArray[Int]
bufs_length(FixedArray[Int], Int, Int) -> Unit
Sets a value at the specified index in a fixed-size array. The original value
at that index is overwritten.
Parameters:
array : The fixed-size array to modify.
index : The position in the array where the value will be set.
value : The new value to assign at the specified index.
Throws a runtime error if the index is out of bounds (less than 0 or greater
than or equal to the length of the array).
Example:
test "FixedArray::op_set" {
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
}
test "panic FixedArray::op_set/out_of_bounds" {
let arr = [1, 2, 3]
arr[3] = 42 // Index out of bounds
}
[i] = Array[@bytes.View]
data(Array[@bytes.View], Int) -> @bytes.View
Retrieves an element from the array at the specified index.
Parameters:
array : The array to get the element from.
index : The position in the array from which to retrieve the element.
Returns the element at the specified index.
Throws a panic if the index is negative or greater than or equal to the
length of the array.
Example:
test "Array::op_get" {
let arr = [1, 2, 3]
inspect(arr[1], content="2")
}
test "panic Array::op_get/out_of_bounds" {
let arr = [1, 2, 3]
ignore(arr[3]) // Index out of bounds
}
[i].(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()
100 }
101 let Int
result = (send : UdpSend, udp : Udp, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], addr : ?, cb : (UdpSend, Int) -> Unit) -> Int
uv_udp_send(
102 UdpSend
req,
103 Udp
self,
104 FixedArray[Bytes]
bufs_base,
105 FixedArray[Int]
bufs_offset,
106 FixedArray[Int]
bufs_length,
107 (Sockaddr?) -> ?
@c.Nullable::of(Sockaddr?
addr.(self : Sockaddr?, f : (Sockaddr) -> Sockaddr) -> Sockaddr?
Maps the value of an Option using a provided function.
Example
let a = Some(5)
assert_eq(a.map(fn(x){ x * 2 }), Some(10))
let b = None
assert_eq(b.map(fn(x){ x * 2 }), None)
map(type parameter
Sockaddr(Sockaddr) -> Sockaddr
::to_sockaddr)),
108 (UdpSend, Int) -> Unit
cb,
109 )
110 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
111 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
112 }
113}
114
115///|
116extern "c" fn uv_udp_recv_start(
117 udp : type Udp
Udp,
118 alloc_cb : (type Handle
Handle, UInt64
UInt64, @c.Pointer[Int
Int], @c.Pointer[Int
Int]) -> Bytes
Bytes,
119 recv_cb : (type Udp
Udp, Int64
Int64, Bytes
Bytes?, Int
Int, Int
Int, type Sockaddr Bytes
Sockaddr, UInt
UInt) -> Unit
Unit
120) -> Int
Int = "moonbit_uv_udp_recv_start"
121
122///|
123pub fn type Udp
Udp::(self : Udp, alloc_cb : (Handle, Int) -> @bytes.View, read_cb : (Udp, Int, @bytes.View, Sockaddr, UdpFlags) -> Unit, error_cb : (Udp, Errno) -> Unit) -> Unit!Errno
recv_start(
124 Udp
self : type Udp
Udp,
125 (Handle, Int) -> @bytes.View
alloc_cb : (type Handle
Handle, Int
Int) -> 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,
126 (Udp, Int, @bytes.View, Sockaddr, UdpFlags) -> Unit
read_cb : (type Udp
Udp, Int
Int, 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, type Sockaddr Bytes
Sockaddr, type UdpFlags UInt
UdpFlags) -> Unit
Unit,
127 (Udp, Errno) -> Unit
error_cb : (type Udp
Udp, type! Errno Status
Errno) -> Unit
Unit
128) -> Unit
Unit!type! Errno Status
Errno {
129 fn (Handle, UInt64, ?, ?) -> Bytes
uv_alloc_cb(
130 Handle
handle : type Handle
Handle,
131 UInt64
suggested_size : UInt64
UInt64,
132 ?
buf_offset : @c.Pointer[Int
Int],
133 ?
buf_length : @c.Pointer[Int
Int]
134 ) -> Bytes
Bytes {
135 let @bytes.View
bytes = (Handle, Int) -> @bytes.View
alloc_cb(Handle
handle, UInt64
suggested_size.(self : UInt64) -> Int
Converts an unsigned 64-bit integer to a 32-bit signed integer. The
conversion truncates the value to fit within the 32-bit range, possibly
losing information if the input value is too large.
Parameters:
self : The unsigned 64-bit integer to be converted.
Returns a 32-bit signed integer representing the lower 32 bits of the input
value. If the input value is larger than the maximum value of a 32-bit signed
integer (2147483647), the result will be the truncated value interpreted as a
signed integer.
Example:
test "UInt64::to_int" {
let a = 42UL
inspect(a.to_int(), content="42")
let b = 18446744073709551615UL // max value of UInt64
inspect(b.to_int(), content="-1") // truncated to 32 bits
}
to_int())
136 ?
buf_offset[0] = @bytes.View
bytes.(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
137 ?
buf_length[0] = @bytes.View
bytes.(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()
138 return @bytes.View
bytes.(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
139 }
140
141 fn (Udp, Int64, Bytes?, Int, Int, Sockaddr, UInt) -> Unit
uv_recv_cb(
142 Udp
udp : type Udp
Udp,
143 Int64
nread : Int64
Int64,
144 Bytes?
buf_data : Bytes
Bytes?,
145 Int
buf_offset : Int
Int,
146 Int
buf_length : Int
Int,
147 Sockaddr
sockaddr : type Sockaddr Bytes
Sockaddr,
148 UInt
flags : UInt
UInt
149 ) -> Unit
Unit {
150 if Int64
nread (self_ : Int64, other : Int64) -> Bool
< 0 {
151 (Udp, Errno) -> Unit
error_cb(Udp
udp, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int64
nread.(self : Int64) -> Int
Converts a 64-bit signed integer to a 32-bit signed integer by truncating
higher bits.
Parameters:
value : The 64-bit signed integer (Int64) to be converted.
Returns a 32-bit signed integer (Int). Note that values outside the range
of 32-bit integers will be truncated, potentially leading to loss of
information.
Example:
test "Int64::to_int" {
let small = 42L
let big = 2147483648L // 2^31
inspect(small.to_int(), content="42")
inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int()))
152 } else {
153 (Udp, Int, @bytes.View, Sockaddr, UdpFlags) -> Unit
read_cb(
154 Udp
udp,
155 Int64
nread.(self : Int64) -> Int
Converts a 64-bit signed integer to a 32-bit signed integer by truncating
higher bits.
Parameters:
value : The 64-bit signed integer (Int64) to be converted.
Returns a 32-bit signed integer (Int). Note that values outside the range
of 32-bit integers will be truncated, potentially leading to loss of
information.
Example:
test "Int64::to_int" {
let small = 42L
let big = 2147483648L // 2^31
inspect(small.to_int(), content="42")
inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int(),
156 Bytes?
buf_data.(self : Bytes?) -> Bytes
Extract the value in Some.
If the value is None, it throws a panic.
unwrap()[Int
buf_offset:Int
buf_length],
157 Sockaddr
sockaddr,
158 (UInt) -> UdpFlags
UdpFlags(UInt
flags),
159 )
160 }
161 }
162
163 let Int
result = (udp : Udp, alloc_cb : (Handle, UInt64, ?, ?) -> Bytes, recv_cb : (Udp, Int64, Bytes?, Int, Int, Sockaddr, UInt) -> Unit) -> Int
uv_udp_recv_start(Udp
self, (Handle, UInt64, ?, ?) -> Bytes
uv_alloc_cb, (Udp, Int64, Bytes?, Int, Int, Sockaddr, UInt) -> Unit
uv_recv_cb)
164 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
165 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
166 }
167}
168
169///|
170extern "c" fn uv_udp_recv_stop(udp : type Udp
Udp) -> Int
Int = "moonbit_uv_udp_recv_stop"
171
172///|
173pub fn type Udp
Udp::(self : Udp) -> Unit!Errno
recv_stop(Udp
self : type Udp
Udp) -> Unit
Unit!type! Errno Status
Errno {
174 let Int
result = (udp : Udp) -> Int
uv_udp_recv_stop(Udp
self)
175 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
176 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
177 }
178}
179
180///|
181type UdpFlags UInt
UInt
182
183///|
184pub fn type UdpFlags UInt
UdpFlags::() -> UdpFlags
new() -> type UdpFlags UInt
UdpFlags {
185 (UInt) -> UdpFlags
UdpFlags(0)
186}
187