1///|
2type GetAddrInfo
3
4///|
5extern "c" fn uv_getaddrinfo_make() -> type GetAddrInfo
GetAddrInfo = "moonbit_uv_getaddrinfo_make"
6
7///|
8type AddrInfoHints
9
10///|
11extern "c" fn uv_addrinfo_hints(
12 flags : Int
Int,
13 family : Int
Int,
14 socktype : Int
Int,
15 protocol : Int
Int
16) -> type AddrInfoHints
AddrInfoHints = "moonbit_uv_addrinfo_hints"
17
18///|
19type AddrInfoFlags Int
Int
20
21///|
22extern "c" fn uv_AI_PASSIVE() -> Int
Int = "moonbit_uv_AI_PASSIVE"
23
24///|
25let Int
_AI_PASSIVE : Int
Int = () -> Int
uv_AI_PASSIVE()
26
27///|
28extern "c" fn uv_AI_CANONNAME() -> Int
Int = "moonbit_uv_AI_CANONNAME"
29
30///|
31let Int
_AI_CANONNAME : Int
Int = () -> Int
uv_AI_CANONNAME()
32
33///|
34extern "c" fn uv_AI_NUMERICHOST() -> Int
Int = "moonbit_uv_AI_NUMERICHOST"
35
36///|
37let Int
_AI_NUMERICHOST : Int
Int = () -> Int
uv_AI_NUMERICHOST()
38
39///|
40extern "c" fn uv_AI_NUMERICSERV() -> Int
Int = "moonbit_uv_AI_NUMERICSERV"
41
42///|
43let Int
_AI_NUMERICSERV : Int
Int = () -> Int
uv_AI_NUMERICSERV()
44
45///|
46extern "c" fn uv_AI_ALL() -> Int
Int = "moonbit_uv_AI_ALL"
47
48///|
49let Int
_AI_ALL : Int
Int = () -> Int
uv_AI_ALL()
50
51///|
52extern "c" fn uv_AI_ADDRCONFIG() -> Int
Int = "moonbit_uv_AI_ADDRCONFIG"
53
54///|
55let Int
_AI_ADDRCONFIG : Int
Int = () -> Int
uv_AI_ADDRCONFIG()
56
57///|
58extern "c" fn uv_AI_V4MAPPED() -> Int
Int = "moonbit_uv_AI_V4MAPPED"
59
60///|
61let Int
_AI_V4MAPPED : Int
Int = () -> Int
uv_AI_V4MAPPED()
62
63///|
64pub fn type AddrInfoFlags Int
AddrInfoFlags::(passive~ : Bool = .., canonname~ : Bool = .., numeric_host~ : Bool = .., numeric_serv~ : Bool = .., all~ : Bool = .., addrconfig~ : Bool = .., v4mapped~ : Bool = ..) -> AddrInfoFlags
new(
65 Bool
passive~ : Bool
Bool = false,
66 Bool
canonname~ : Bool
Bool = false,
67 Bool
numeric_host~ : Bool
Bool = false,
68 Bool
numeric_serv~ : Bool
Bool = false,
69 Bool
all~ : Bool
Bool = false,
70 Bool
addrconfig~ : Bool
Bool = false,
71 Bool
v4mapped~ : Bool
Bool = false
72) -> type AddrInfoFlags Int
AddrInfoFlags {
73 let mut Int
flags = 0
74 if Bool
passive {
75 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_PASSIVE
76 }
77 if Bool
canonname {
78 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_CANONNAME
79 }
80 if Bool
numeric_host {
81 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_NUMERICHOST
82 }
83 if Bool
numeric_serv {
84 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_NUMERICSERV
85 }
86 if Bool
all {
87 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_ALL
88 }
89 if Bool
addrconfig {
90 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_ADDRCONFIG
91 }
92 if Bool
v4mapped {
93 Int
flags = Int
flags (Int, Int) -> Int
| Int
_AI_V4MAPPED
94 }
95 return Int
flags
96}
97
98///|
99pub fn type AddrInfoHints
AddrInfoHints::(flags? : AddrInfoFlags, family? : AddressFamily, socktype? : SockType, protocol? : Protocol) -> AddrInfoHints
new(
100 AddrInfoFlags?
flags? : type AddrInfoFlags Int
AddrInfoFlags,
101 AddressFamily?
family? : enum AddressFamily {
INET
INET6
}
AddressFamily,
102 SockType?
socktype? : enum SockType {
STREAM
RAW
DGRAM
}
SockType,
103 Protocol?
protocol? : enum Protocol {
TCP
UDP
}
Protocol
104) -> type AddrInfoHints
AddrInfoHints {
105 let Int
flags = match AddrInfoFlags?
flags {
106 (AddrInfoFlags) -> AddrInfoFlags?
Some(AddrInfoFlags
flags) => AddrInfoFlags
flags.Int
_
107 AddrInfoFlags?
None => 0
108 }
109 let Int
family = match AddressFamily?
family {
110 (AddressFamily) -> AddressFamily?
Some(AddressFamily
family) => AddressFamily
family.(self : AddressFamily) -> Int
to_int()
111 AddressFamily?
None => Int
_AF_UNSPEC
112 }
113 let Int
socktype = match SockType?
socktype {
114 (SockType) -> SockType?
Some(SockType
socktype) => SockType
socktype.(self : SockType) -> Int
to_int()
115 SockType?
None => 0
116 }
117 let Int
protocol = match Protocol?
protocol {
118 (Protocol) -> Protocol?
Some(Protocol
protocol) => Protocol
protocol.(self : Protocol) -> Int
to_int()
119 Protocol?
None => 0
120 }
121 (flags : Int, family : Int, socktype : Int, protocol : Int) -> AddrInfoHints
uv_addrinfo_hints(Int
flags, Int
family, Int
socktype, Int
protocol)
122}
123
124///|
125extern "c" fn type AddrInfoHints
AddrInfoHints::nil() -> type AddrInfoHints
AddrInfoHints = "moonbit_c_null"
126
127///|
128priv type AddrInfoResults
129
130///|
131#borrow(addrinfo_results, cb)
132extern "c" fn uv_addrinfo_results_iterate(
133 addrinfo_results : type AddrInfoResults
AddrInfoResults,
134 cb : (Int
Int, Int
Int, Int
Int, Int
Int, type Sockaddr Bytes
Sockaddr, @c.Pointer[Byte
Byte]) -> Bool
Bool
135) -> Bool
Bool = "moonbit_uv_addrinfo_results_iterate"
136
137///|
138extern "c" fn uv_SOCK_STREAM() -> Int
Int = "moonbit_uv_SOCK_STREAM"
139
140///|
141let Int
_SOCK_STREAM : Int
Int = () -> Int
uv_SOCK_STREAM()
142
143///|
144extern "c" fn uv_SOCK_DGRAM() -> Int
Int = "moonbit_uv_SOCK_DGRAM"
145
146///|
147let Int
_SOCK_DGRAM : Int
Int = () -> Int
uv_SOCK_DGRAM()
148
149///|
150extern "c" fn uv_SOCK_RAW() -> Int
Int = "moonbit_uv_SOCK_RAW"
151
152///|
153let Int
_SOCK_RAW : Int
Int = () -> Int
uv_SOCK_RAW()
154
155///|
156extern "c" fn uv_IPPROTO_UDP() -> Int
Int = "moonbit_uv_IPPROTO_UDP"
157
158///|
159let Int
_IPPROTO_UDP : Int
Int = () -> Int
uv_IPPROTO_UDP()
160
161///|
162extern "c" fn uv_IPPROTO_TCP() -> Int
Int = "moonbit_uv_IPPROTO_TCP"
163
164///|
165let Int
_IPPROTO_TCP : Int
Int = () -> Int
uv_IPPROTO_TCP()
166
167///|
168extern "c" fn uv_IF_NAMESIZE() -> Int
Int = "moonbit_uv_IF_NAMESIZE"
169
170///|
171let Int
_IF_NAMESIZE : Int
Int = () -> Int
uv_IF_NAMESIZE()
172
173///|
174#borrow(src, dst)
175extern "c" fn uv_ip_name(src : type Sockaddr Bytes
Sockaddr, dst : Bytes
Bytes) -> Int
Int = "moonbit_uv_ip_name"
176
177///|
178pub fn (self : Sockaddr) -> String!Errno
ip_name(Sockaddr
self : type Sockaddr Bytes
Sockaddr) -> String
String!type! Errno Status
Errno {
179 let Bytes
bytes = (Int, Byte) -> Bytes
Bytes::(len : Int, init : Byte) -> Bytes
Creates a new byte sequence of the specified length, where each byte is
initialized to the given value. Returns an empty byte sequence if the
length is negative.
Parameters:
length : The length of the byte sequence to create. Must be non-negative.
initial_value : The byte value used to initialize each position in the
sequence.
Example:
test "Bytes::make" {
let bytes = Bytes::make(3, b'\xFF')
inspect(bytes, content=
#|b"\xff\xff\xff"
)
let empty = Bytes::make(0, b'\x00')
inspect(empty, content="b\"\"")
}
make(Int
_IF_NAMESIZE, 0)
180 let Int
status = (src : Sockaddr, dst : Bytes) -> Int
uv_ip_name(Sockaddr
self, Bytes
bytes)
181 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
182 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
183 }
184 let @buffer.T
buffer = (size_hint~ : Int = ..) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
test "new" {
let buf = @buffer.new(size_hint=10)
inspect(buf.length(), content="0")
buf.write_string("test")
inspect(buf.length(), content="8")
}
@buffer.new()
185 for Int
i = 0; Bytes
bytes(Bytes, Int) -> Byte
Retrieves a byte at the specified index from a byte sequence.
Parameters:
bytes : The byte sequence to access.
index : The position in the byte sequence from which to retrieve the
byte.
Returns a byte value from the specified position in the sequence.
Throws a panic if the index is negative or greater than or equal to the
length of the byte sequence.
Example:
test "Bytes::op_get" {
let bytes = b"\x01\x02\x03"
inspect(bytes[1], content="b'\\x02'")
}
test "panic Bytes::op_get/out_of_bounds" {
let bytes = b"\x01\x02\x03"
ignore(bytes[3]) // Index out of bounds
}
[i] (x : Byte, y : Byte) -> Bool
!= 0; Int
i = Int
i (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
}
+ 1 {
186 @buffer.T
buffer.(self : @buffer.T, value : Byte) -> Unit
Writes a single byte to the end of the buffer. The buffer will automatically
grow if necessary to accommodate the new byte.
Parameters:
buffer : The buffer to write to.
byte : The byte value to be written.
Example:
test "write_byte" {
let buf = @buffer.new()
buf.write_byte(b'\x41')
inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte(Bytes
bytes(Bytes, Int) -> Byte
Retrieves a byte at the specified index from a byte sequence.
Parameters:
bytes : The byte sequence to access.
index : The position in the byte sequence from which to retrieve the
byte.
Returns a byte value from the specified position in the sequence.
Throws a panic if the index is negative or greater than or equal to the
length of the byte sequence.
Example:
test "Bytes::op_get" {
let bytes = b"\x01\x02\x03"
inspect(bytes[1], content="b'\\x02'")
}
test "panic Bytes::op_get/out_of_bounds" {
let bytes = b"\x01\x02\x03"
ignore(bytes[3]) // Index out of bounds
}
[i])
187 }
188 return (encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, @buffer.T
buffer.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
test "contents" {
let buf = @buffer.new()
buf.write_string("Test")
inspect(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
}
contents())
189}
190
191///|
192struct AddrInfo {
193 Int
family : Int
Int
194 Int
socktype : Int
Int
195 Int
protocol : Int
Int
196 Sockaddr
addr : type Sockaddr Bytes
Sockaddr
197 String?
canonname : String
String?
198}
199
200///|
201extern "c" fn uv_AF_INET() -> Int
Int = "moonbit_uv_AF_INET"
202
203///|
204let Int
_AF_INET : Int
Int = () -> Int
uv_AF_INET()
205
206///|
207extern "c" fn uv_AF_INET6() -> Int
Int = "moonbit_uv_AF_INET6"
208
209///|
210let Int
_AF_INET6 : Int
Int = () -> Int
uv_AF_INET6()
211
212///|
213extern "c" fn uv_AF_UNSPEC() -> Int
Int = "moonbit_uv_AF_UNSPEC"
214
215///|
216let Int
_AF_UNSPEC : Int
Int = () -> Int
uv_AF_UNSPEC()
217
218///|
219pub enum AddressFamily {
220 AddressFamily
INET
221 AddressFamily
INET6
222} derive((AddressFamily, &Logger) -> Unit
Trait for types that can be converted to String
Show)
223
224///|
225pub fn enum AddressFamily {
INET
INET6
}
AddressFamily::() -> AddressFamily
inet() -> enum AddressFamily {
INET
INET6
}
AddressFamily {
226 return AddressFamily
INET
227}
228
229///|
230pub fn enum AddressFamily {
INET
INET6
}
AddressFamily::() -> AddressFamily
inet6() -> enum AddressFamily {
INET
INET6
}
AddressFamily {
231 return AddressFamily
INET6
232}
233
234///|
235fn enum AddressFamily {
INET
INET6
}
AddressFamily::(self : Int) -> AddressFamily!Errno
of_int(Int
self : Int
Int) -> enum AddressFamily {
INET
INET6
}
AddressFamily!type! Errno Status
Errno {
236 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_AF_INET {
237 return AddressFamily
INET
238 }
239 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_AF_INET6 {
240 return AddressFamily
INET6
241 }
242 raise (Status) -> Errno
Errno(Status
EINVAL)
243}
244
245///|
246fn enum AddressFamily {
INET
INET6
}
AddressFamily::(self : AddressFamily) -> Int
to_int(AddressFamily
self : enum AddressFamily {
INET
INET6
}
AddressFamily) -> Int
Int {
247 match AddressFamily
self {
248 AddressFamily
INET => Int
_AF_INET
249 AddressFamily
INET6 => Int
_AF_INET6
250 }
251}
252
253///|
254pub fn struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo::(self : AddrInfo) -> AddressFamily!Errno
family(AddrInfo
self : struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo) -> enum AddressFamily {
INET
INET6
}
AddressFamily!type! Errno Status
Errno {
255 enum AddressFamily {
INET
INET6
}
AddressFamily::(self : Int) -> AddressFamily!Errno
of_int!(AddrInfo
self.Int
family)
256}
257
258///|
259pub fn struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo::(self : AddrInfo) -> Sockaddr
addr(AddrInfo
self : struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo) -> type Sockaddr Bytes
Sockaddr {
260 AddrInfo
self.Sockaddr
addr
261}
262
263///|
264enum SockType {
265 SockType
STREAM
266 SockType
RAW
267 SockType
DGRAM
268} derive((SockType, &Logger) -> Unit
Trait for types that can be converted to String
Show)
269
270///|
271pub fn enum SockType {
STREAM
RAW
DGRAM
}
SockType::() -> SockType
stream() -> enum SockType {
STREAM
RAW
DGRAM
}
SockType {
272 return SockType
STREAM
273}
274
275///|
276pub fn enum SockType {
STREAM
RAW
DGRAM
}
SockType::() -> SockType
raw() -> enum SockType {
STREAM
RAW
DGRAM
}
SockType {
277 return SockType
RAW
278}
279
280///|
281pub fn enum SockType {
STREAM
RAW
DGRAM
}
SockType::() -> SockType
dgram() -> enum SockType {
STREAM
RAW
DGRAM
}
SockType {
282 return SockType
DGRAM
283}
284
285///|
286fn enum SockType {
STREAM
RAW
DGRAM
}
SockType::(self : Int) -> SockType!Errno
of_int(Int
self : Int
Int) -> enum SockType {
STREAM
RAW
DGRAM
}
SockType!type! Errno Status
Errno {
287 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_SOCK_STREAM {
288 return SockType
STREAM
289 }
290 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_SOCK_RAW {
291 return SockType
RAW
292 }
293 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_SOCK_DGRAM {
294 return SockType
DGRAM
295 }
296 raise (Status) -> Errno
Errno(Status
EINVAL)
297}
298
299///|
300fn enum SockType {
STREAM
RAW
DGRAM
}
SockType::(self : SockType) -> Int
to_int(SockType
self : enum SockType {
STREAM
RAW
DGRAM
}
SockType) -> Int
Int {
301 match SockType
self {
302 SockType
STREAM => Int
_SOCK_STREAM
303 SockType
RAW => Int
_SOCK_RAW
304 SockType
DGRAM => Int
_SOCK_DGRAM
305 }
306}
307
308///|
309pub fn struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo::(self : AddrInfo) -> SockType!Errno
socktype(AddrInfo
self : struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo) -> enum SockType {
STREAM
RAW
DGRAM
}
SockType!type! Errno Status
Errno {
310 return enum SockType {
STREAM
RAW
DGRAM
}
SockType::(self : Int) -> SockType!Errno
of_int!(AddrInfo
self.Int
socktype)
311}
312
313///|
314pub enum Protocol {
315 Protocol
TCP
316 Protocol
UDP
317} derive((Protocol, &Logger) -> Unit
Trait for types that can be converted to String
Show)
318
319///|
320pub fn enum Protocol {
TCP
UDP
}
Protocol::() -> Protocol
tcp() -> enum Protocol {
TCP
UDP
}
Protocol {
321 return Protocol
TCP
322}
323
324///|
325pub fn enum Protocol {
TCP
UDP
}
Protocol::() -> Protocol
udp() -> enum Protocol {
TCP
UDP
}
Protocol {
326 return Protocol
UDP
327}
328
329///|
330fn enum Protocol {
TCP
UDP
}
Protocol::(self : Int) -> Protocol!Errno
of_int(Int
self : Int
Int) -> enum Protocol {
TCP
UDP
}
Protocol!type! Errno Status
Errno {
331 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_IPPROTO_TCP {
332 return Protocol
TCP
333 }
334 if Int
self (self : Int, other : Int) -> Bool
Compares two integers for equality.
Parameters:
self : The first integer to compare.
other : The second integer to compare.
Returns true if both integers have the same value, false otherwise.
Example:
test "Int::op_equal" {
inspect(42 == 42, content="true")
inspect(42 == -42, content="false")
}
== Int
_IPPROTO_UDP {
335 return Protocol
UDP
336 }
337 raise (Status) -> Errno
Errno(Status
EINVAL)
338}
339
340///|
341fn enum Protocol {
TCP
UDP
}
Protocol::(self : Protocol) -> Int
to_int(Protocol
self : enum Protocol {
TCP
UDP
}
Protocol) -> Int
Int {
342 match Protocol
self {
343 Protocol
TCP => Int
_IPPROTO_TCP
344 Protocol
UDP => Int
_IPPROTO_UDP
345 }
346}
347
348///|
349pub fn struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo::(self : AddrInfo) -> Protocol!Errno
protocol(AddrInfo
self : struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo) -> enum Protocol {
TCP
UDP
}
Protocol!type! Errno Status
Errno {
350 enum Protocol {
TCP
UDP
}
Protocol::(self : Int) -> Protocol!Errno
of_int!(AddrInfo
self.Int
protocol)
351}
352
353///|
354pub fn struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo::(self : AddrInfo) -> String?
canonname(AddrInfo
self : struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo) -> String
String? {
355 AddrInfo
self.String?
canonname
356}
357
358///|
359fn type AddrInfoResults
AddrInfoResults::(self : AddrInfoResults) -> Iter[AddrInfo]
iter(AddrInfoResults
self : type AddrInfoResults
AddrInfoResults) -> type Iter[T]
Iter[struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo] {
360 type Iter[T]
Iter::(((AddrInfo) -> IterResult) -> IterResult) -> Iter[AddrInfo]
Do not use this method, it is for internal use only.
new(fn((AddrInfo) -> IterResult
yield_) {
361 if (addrinfo_results : AddrInfoResults, cb : (Int, Int, Int, Int, Sockaddr, ?) -> Bool) -> Bool
uv_addrinfo_results_iterate(AddrInfoResults
self, fn(
362 _,
363 Int
family,
364 Int
socktype,
365 Int
protocol,
366 Sockaddr
addr,
367 ?
canonname
368 ) {
369 let String?
canonname = if ?
canonname.() -> Bool
is_null() {
370 String?
None
371 } else {
372 let @buffer.T
buffer = (size_hint~ : Int = ..) -> @buffer.T
Creates a new extensible buffer with specified initial capacity. If the
initial capacity is less than 1, the buffer will be initialized with capacity
1.
Parameters:
size_hint : Initial capacity of the buffer in bytes. Defaults to 0.
Returns a new buffer of type T.
Example:
test "new" {
let buf = @buffer.new(size_hint=10)
inspect(buf.length(), content="0")
buf.write_string("test")
inspect(buf.length(), content="8")
}
@buffer.new()
373 for Int
i = 0; ?
canonname(Int) -> Int
[i] (x : Int, y : Int) -> Bool
!= 0; Int
i = Int
i (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
}
+ 1 {
374 @buffer.T
buffer.(self : @buffer.T, value : Byte) -> Unit
Writes a single byte to the end of the buffer. The buffer will automatically
grow if necessary to accommodate the new byte.
Parameters:
buffer : The buffer to write to.
byte : The byte value to be written.
Example:
test "write_byte" {
let buf = @buffer.new()
buf.write_byte(b'\x41')
inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte(?
canonname(Int) -> Byte
[i])
375 }
376 (String) -> String?
Some((encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy(@encoding.Encoding
encoding=@encoding.Encoding
UTF8, @buffer.T
buffer.(self : @buffer.T) -> Bytes
Returns a copy of the buffer's content as a sequence of bytes.
Parameters:
buffer : The buffer to read from.
Returns a Bytes object containing all bytes written to the buffer.
Example:
test "contents" {
let buf = @buffer.new()
buf.write_string("Test")
inspect(
buf.contents(),
content=
#|b"\x54\x00\x65\x00\x73\x00\x74\x00"
,
)
}
contents()))
377 }
378 match (AddrInfo) -> IterResult
yield_({ Int
family, Int
socktype, Int
protocol, Sockaddr
addr, String?
canonname }) {
379 IterResult
IterEnd => true
380 IterResult
IterContinue => false
381 }
382 }) {
383 return IterResult
IterEnd
384 } else {
385 return IterResult
IterContinue
386 }
387 })
388}
389
390///|
391extern "c" fn uv_getaddrinfo(
392 uv : type Loop Bytes
Loop,
393 req : type GetAddrInfo
GetAddrInfo,
394 getaddrinfo_cb : (type GetAddrInfo
GetAddrInfo, Int
Int, type AddrInfoResults
AddrInfoResults) -> Unit
Unit,
395 node : Bytes
Bytes,
396 service : Bytes
Bytes,
397 hints : type AddrInfoHints
AddrInfoHints
398) -> Int
Int = "moonbit_uv_getaddrinfo"
399
400///|
401pub fn[Node : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
ToNtbs, Service : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
ToNtbs] (self : Loop, getaddrinfo_cb : (GetAddrInfo, Iter[AddrInfo]) -> Unit, error_cb : (GetAddrInfo, Errno) -> Unit, node : Node, service : Service, hints? : AddrInfoHints) -> Unit!Errno
getaddrinfo(
402 Loop
self : type Loop Bytes
Loop,
403 (GetAddrInfo, Iter[AddrInfo]) -> Unit
getaddrinfo_cb : (type GetAddrInfo
GetAddrInfo, type Iter[T]
Iter[struct AddrInfo {
family: Int
socktype: Int
protocol: Int
addr: Sockaddr
canonname: String?
}
AddrInfo]) -> Unit
Unit,
404 (GetAddrInfo, Errno) -> Unit
error_cb : (type GetAddrInfo
GetAddrInfo, type! Errno Status
Errno) -> Unit
Unit,
405 Node
node : type parameter Node
Node,
406 Service
service : type parameter Service
Service,
407 AddrInfoHints?
hints? : type AddrInfoHints
AddrInfoHints
408) -> Unit
Unit!type! Errno Status
Errno {
409 fn (GetAddrInfo, Int, AddrInfoResults) -> Unit
uv_cb(GetAddrInfo
req : type GetAddrInfo
GetAddrInfo, Int
status : Int
Int, AddrInfoResults
addrinfo : type AddrInfoResults
AddrInfoResults) {
410 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
411 (GetAddrInfo, Errno) -> Unit
error_cb(GetAddrInfo
req, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
412 } else {
413 (GetAddrInfo, Iter[AddrInfo]) -> Unit
getaddrinfo_cb(GetAddrInfo
req, AddrInfoResults
addrinfo.(self : AddrInfoResults) -> Iter[AddrInfo]
iter())
414 }
415 }
416
417 let GetAddrInfo
req = () -> GetAddrInfo
uv_getaddrinfo_make()
418 let Int
status = (uv : Loop, req : GetAddrInfo, getaddrinfo_cb : (GetAddrInfo, Int, AddrInfoResults) -> Unit, node : Bytes, service : Bytes, hints : AddrInfoHints) -> Int
uv_getaddrinfo(
419 Loop
self,
420 GetAddrInfo
req,
421 (GetAddrInfo, Int, AddrInfoResults) -> Unit
uv_cb,
422 Node
node.(Node) -> Bytes
to_ntbs(),
423 Service
service.(Service) -> Bytes
to_ntbs(),
424 AddrInfoHints?
hints.(self : AddrInfoHints?, default : () -> AddrInfoHints) -> AddrInfoHints
Return the contained Some value or the provided default.
Default is lazily evaluated
or_else(type AddrInfoHints
AddrInfoHints() -> AddrInfoHints
::nil),
425 )
426 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
427 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
428 }
429}
430