1///|
2type Stream
3
4///|
5pub fn type Stream
Stream::to_handle(self : type Stream
Stream) -> 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 Stream
Stream with (self : Stream) -> Handle
to_handle(Stream
self) {
9 Stream
self.(self : Stream) -> Handle
to_handle()
10}
11
12///|
13pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle for type Stream
Stream with of_handle(self) = "%identity"
14
15///|
16extern "c" fn uv_read_start(
17 stream : type Stream
Stream,
18 alloc_cb : (type Handle
Handle, UInt64
UInt64, @c.Pointer[Int
Int], @c.Pointer[Int
Int]) -> Bytes
Bytes,
19 read_cb : (type Stream
Stream, Int64
Int64, Bytes
Bytes?, Int
Int, Int
Int) -> Unit
Unit
20) -> Int
Int = "moonbit_uv_read_start"
21
22///|
23pub fn type Stream
Stream::(self : Stream, alloc_cb : (Handle, Int) -> @bytes.View, read_cb : (Stream, Int, @bytes.View) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit!Errno
read_start(
24 Stream
self : type Stream
Stream,
25 (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,
26 (Stream, Int, @bytes.View) -> Unit
read_cb : (type Stream
Stream, 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) -> Unit
Unit,
27 (Stream, Errno) -> Unit
error_cb : (type Stream
Stream, type! Errno Status
Errno) -> Unit
Unit
28) -> Unit
Unit!type! Errno Status
Errno {
29 fn (Handle, UInt64, ?, ?) -> Bytes
uv_alloc_cb(
30 Handle
handle : type Handle
Handle,
31 UInt64
suggested_size : UInt64
UInt64,
32 ?
buf_offset : @c.Pointer[Int
Int],
33 ?
buf_length : @c.Pointer[Int
Int]
34 ) -> Bytes
Bytes {
35 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())
36 ?
buf_offset[0] = @bytes.View
bytes.(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
37 ?
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()
38 return @bytes.View
bytes.(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
39 }
40
41 fn (Stream, Int64, Bytes?, Int, Int) -> Unit
uv_read_cb(
42 Stream
stream : type Stream
Stream,
43 Int64
nread : Int64
Int64,
44 Bytes?
buf_data : Bytes
Bytes?,
45 Int
buf_offset : Int
Int,
46 Int
buf_length : Int
Int
47 ) -> Unit
Unit {
48 if Int64
nread (self_ : Int64, other : Int64) -> Bool
< 0 {
49 (Stream, Errno) -> Unit
error_cb(Stream
stream, 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()))
50 } else {
51 (Stream, Int, @bytes.View) -> Unit
read_cb(Stream
stream, 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(), 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])
52 }
53 }
54
55 let Int
result = (stream : Stream, alloc_cb : (Handle, UInt64, ?, ?) -> Bytes, read_cb : (Stream, Int64, Bytes?, Int, Int) -> Unit) -> Int
uv_read_start(Stream
self, (Handle, UInt64, ?, ?) -> Bytes
uv_alloc_cb, (Stream, Int64, Bytes?, Int, Int) -> Unit
uv_read_cb)
56 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
57 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
58 }
59}
60
61///|
62pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream, alloc_cb : (Stream, Int) -> @bytes.View, read_cb : (Stream, Int, @bytes.View) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit!Errno
read_start(
63 Stream
stream : type parameter Stream
Stream,
64 (Stream, Int) -> @bytes.View
alloc_cb : (type parameter Stream
Stream, 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,
65 (Stream, Int, @bytes.View) -> Unit
read_cb : (type parameter Stream
Stream, 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) -> Unit
Unit,
66 (Stream, Errno) -> Unit
error_cb : (type parameter Stream
Stream, type! Errno Status
Errno) -> Unit
Unit
67) -> Unit
Unit!type! Errno Status
Errno {
68 Stream
stream.(Stream, (Stream, Int) -> @bytes.View, (Stream, Int, @bytes.View) -> Unit, (Stream, Errno) -> Unit) -> Unit!Errno
read_start!((Stream, Int) -> @bytes.View
alloc_cb, (Stream, Int, @bytes.View) -> Unit
read_cb, (Stream, Errno) -> Unit
error_cb)
69}
70
71///|
72extern "c" fn uv_read_stop(stream : type Stream
Stream) -> Int
Int = "moonbit_uv_read_stop"
73
74///|
75pub fn type Stream
Stream::(self : Stream) -> Unit!Errno
read_stop(Stream
self : type Stream
Stream) -> Unit
Unit!type! Errno Status
Errno {
76 let Int
result = (stream : Stream) -> Int
uv_read_stop(Stream
self)
77 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
78 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
79 }
80}
81
82///|
83pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream) -> Unit!Errno
read_stop(Stream
stream : type parameter Stream
Stream) -> Unit
Unit!type! Errno Status
Errno {
84 Stream
stream.(Stream) -> Unit!Errno
read_stop!()
85}
86
87///|
88type Write
89
90///|
91pub impl trait ToReq {
to_req(Self) -> Req
}
ToReq for type Write
Write with to_req(self : type Write
Write) -> type Req
Req = "%identity"
92
93///|
94extern "c" fn uv_write_make() -> type Write
Write = "moonbit_uv_write_make"
95
96///|
97extern "c" fn uv_write(
98 write : type Write
Write,
99 handle : type Stream
Stream,
100 bufs_base : type FixedArray[A]
FixedArray[Bytes
Bytes],
101 bufs_offset : type FixedArray[A]
FixedArray[Int
Int],
102 bufs_length : type FixedArray[A]
FixedArray[Int
Int],
103 cb : (type Write
Write, Int
Int) -> Unit
Unit
104) -> Int
Int = "moonbit_uv_write"
105
106///|
107pub fn type Stream
Stream::(self : Stream, bufs : Array[@bytes.View], write_cb : (Write) -> Unit, error_cb : (Write, Errno) -> Unit) -> Unit!Errno
write(
108 Stream
self : type Stream
Stream,
109 Array[@bytes.View]
bufs : 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],
110 (Write) -> Unit
write_cb : (type Write
Write) -> Unit
Unit,
111 (Write, Errno) -> Unit
error_cb : (type Write
Write, type! Errno Status
Errno) -> Unit
Unit
112) -> Unit
Unit!type! Errno Status
Errno {
113 fn (Write, Int) -> Unit
cb(Write
req : type Write
Write, Int
status : Int
Int) {
114 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
115 (Write, Errno) -> Unit
error_cb(Write
req, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
116 } else {
117 (Write) -> Unit
write_cb(Write
req)
118 }
119 }
120
121 let Write
req = () -> Write
uv_write_make()
122 let Int
bufs_size = Array[@bytes.View]
bufs.(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()
123 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, [])
124 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)
125 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)
126 for Int
i in Int
0..<Array[@bytes.View]
bufs.(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() {
127 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]
bufs(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()
128 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]
bufs(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()
129 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]
bufs(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()
130 }
131 let Int
result = (write : Write, handle : Stream, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], cb : (Write, Int) -> Unit) -> Int
uv_write(Write
req, Stream
self, FixedArray[Bytes]
bufs_base, FixedArray[Int]
bufs_offset, FixedArray[Int]
bufs_length, (Write, Int) -> Unit
cb)
132 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
133 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
134 }
135}
136
137///|
138pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream, bufs : Array[@bytes.View], write_cb : (Write) -> Unit, error_cb : (Write, Errno) -> Unit) -> Unit!Errno
write(
139 Stream
stream : type parameter Stream
Stream,
140 Array[@bytes.View]
bufs : 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],
141 (Write) -> Unit
write_cb : (type Write
Write) -> Unit
Unit,
142 (Write, Errno) -> Unit
error_cb : (type Write
Write, type! Errno Status
Errno) -> Unit
Unit
143) -> Unit
Unit!type! Errno Status
Errno {
144 Stream
stream.(Stream, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
write!(Array[@bytes.View]
bufs, (Write) -> Unit
write_cb, (Write, Errno) -> Unit
error_cb)
145}
146
147///|
148pub trait trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream: trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle {
149 (Self) -> Stream
to_stream(type parameter Self
Self) -> type Stream
Stream
150 (Stream) -> Self
of_stream(type Stream
Stream) -> type parameter Self
Self
151 (Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_start(
152 type parameter Self
Self,
153 (type parameter Self
Self, 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,
154 (type parameter Self
Self, 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) -> Unit
Unit,
155 (type parameter Self
Self, type! Errno Status
Errno) -> Unit
Unit,
156 ) -> Unit
Unit!type! Errno Status
Errno = _
157 (Self) -> Unit!Errno
read_stop(type parameter Self
Self) -> Unit
Unit!type! Errno Status
Errno = _
158 (Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
write(type parameter Self
Self, 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], (type Write
Write) -> Unit
Unit, (type Write
Write, type! Errno Status
Errno) -> Unit
Unit) ->
159 Unit
Unit!type! Errno Status
Errno = _
160 (Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
listen(type parameter Self
Self, Int
Int, (type parameter Self
Self) -> Unit
Unit, (type parameter Self
Self, type! Errno Status
Errno) -> Unit
Unit) -> Unit
Unit!type! Errno Status
Errno = _
161 (Self) -> Bool
is_readable(type parameter Self
Self) -> Bool
Bool = _
162 (Self) -> Bool
is_writable(type parameter Self
Self) -> Bool
Bool = _
163 (Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown(type parameter Self
Self, (type Shutdown
Shutdown) -> Unit
Unit, (type Shutdown
Shutdown, type! Errno Status
Errno) -> Unit
Unit) -> Unit
Unit!type! Errno Status
Errno = _
164}
165
166///|
167pub impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream for type Stream
Stream with to_stream(self) = "%identity"
168
169///|
170pub impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream for type Stream
Stream with of_stream(self) = "%identity"
171
172///|
173impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self, alloc_cb : (Self, Int) -> @bytes.View, read_cb : (Self, Int, @bytes.View) -> Unit, error_cb : (Self, Errno) -> Unit) -> Unit!Errno
read_start(Self
self, (Self, Int) -> @bytes.View
alloc_cb, (Self, Int, @bytes.View) -> Unit
read_cb, (Self, Errno) -> Unit
error_cb) {
174 // We are wrapping callbacks here, for two reasons:
175 // 1. We need to downcast the `Handle`/`Stream` to concrete types.
176 // 2. (Mutually) recursive indirect call will create a reference loop,
177 // and RC will fail to collect it. Therefore, instead of directly
178 // passing `alloc_cb`, `read_cb` and `error_cb` to `read_start()`,
179 // we wrap them into local functions, so that compiler can perform
180 // capture analysis and will not create closures that refer to each
181 // other.
182 fn (Handle, Int) -> @bytes.View
handle_alloc_cb(Handle
handle : type Handle
Handle, Int
suggested_size : Int
Int) {
183 (Self, Int) -> @bytes.View
alloc_cb(trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle(_ : Handle) -> Self
::of_handle(Handle
handle), Int
suggested_size)
184 }
185
186 fn (Stream, Int, @bytes.View) -> Unit
stream_read_cb(Stream
stream : type Stream
Stream, Int
count : Int
Int, @bytes.View
bytes : 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) {
187 (Self, Int, @bytes.View) -> Unit
read_cb(trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream(_ : Stream) -> Self
::of_stream(Stream
stream), Int
count, @bytes.View
bytes)
188 }
189
190 fn (Stream, Errno) -> Unit
stream_error_cb(Stream
stream : type Stream
Stream, Errno
errno : type! Errno Status
Errno) {
191 (Self, Errno) -> Unit
error_cb(trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream(_ : Stream) -> Self
::of_stream(Stream
stream), Errno
errno)
192 }
193
194 Self
self.(Self) -> Stream
to_stream().(self : Stream, alloc_cb : (Handle, Int) -> @bytes.View, read_cb : (Stream, Int, @bytes.View) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit!Errno
read_start!((Handle, Int) -> @bytes.View
handle_alloc_cb, (Stream, Int, @bytes.View) -> Unit
stream_read_cb, (Stream, Errno) -> Unit
stream_error_cb)
195}
196
197///|
198impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self) -> Unit!Errno
read_stop(Self
self) {
199 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Unit!Errno
read_stop!()
200}
201
202///|
203impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self, bufs : Array[@bytes.View], write_cb : (Write) -> Unit, error_cb : (Write, Errno) -> Unit) -> Unit!Errno
write(Self
self, Array[@bytes.View]
bufs, (Write) -> Unit
write_cb, (Write, Errno) -> Unit
error_cb) {
204 Self
self.(Self) -> Stream
to_stream().(self : Stream, bufs : Array[@bytes.View], write_cb : (Write) -> Unit, error_cb : (Write, Errno) -> Unit) -> Unit!Errno
write!(Array[@bytes.View]
bufs, (Write) -> Unit
write_cb, (Write, Errno) -> Unit
error_cb)
205}
206
207///|
208#borrow(stream)
209extern "c" fn uv_is_readable(stream : type Stream
Stream) -> Bool
Bool = "moonbit_uv_is_readable"
210
211///|
212#borrow(stream)
213extern "c" fn uv_is_writable(stream : type Stream
Stream) -> Bool
Bool = "moonbit_uv_is_writable"
214
215///|
216pub fn type Stream
Stream::(self : Stream) -> Bool
is_readable(Stream
self : type Stream
Stream) -> Bool
Bool {
217 (stream : Stream) -> Bool
uv_is_readable(Stream
self)
218}
219
220///|
221pub fn type Stream
Stream::(self : Stream) -> Bool
is_writable(Stream
self : type Stream
Stream) -> Bool
Bool {
222 (stream : Stream) -> Bool
uv_is_writable(Stream
self)
223}
224
225///|
226pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream) -> Bool
is_readable(Stream
stream : type parameter Stream
Stream) -> Bool
Bool {
227 (stream : Stream) -> Bool
uv_is_readable(Stream
stream.(Stream) -> Stream
to_stream())
228}
229
230///|
231pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream) -> Bool
is_writable(Stream
stream : type parameter Stream
Stream) -> Bool
Bool {
232 (stream : Stream) -> Bool
uv_is_writable(Stream
stream.(Stream) -> Stream
to_stream())
233}
234
235///|
236impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self) -> Bool
is_readable(Self
self) {
237 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Bool
is_readable()
238}
239
240///|
241impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self) -> Bool
is_writable(Self
self) {
242 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Bool
is_writable()
243}
244
245///|
246type Shutdown
247
248///|
249pub impl trait ToReq {
to_req(Self) -> Req
}
ToReq for type Shutdown
Shutdown with to_req(self : type Shutdown
Shutdown) -> type Req
Req = "%identity"
250
251///|
252extern "c" fn uv_shutdown_make() -> type Shutdown
Shutdown = "moonbit_uv_shutdown_make"
253
254///|
255extern "c" fn uv_shutdown(
256 shutdown : type Shutdown
Shutdown,
257 handle : type Stream
Stream,
258 cb : (type Shutdown
Shutdown, Int
Int) -> Unit
Unit
259) -> Int
Int = "moonbit_uv_shutdown"
260
261///|
262pub fn type Stream
Stream::(self : Stream, shutdown_cb : (Shutdown) -> Unit, error_cb : (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown(
263 Stream
self : type Stream
Stream,
264 (Shutdown) -> Unit
shutdown_cb : (type Shutdown
Shutdown) -> Unit
Unit,
265 (Shutdown, Errno) -> Unit
error_cb : (type Shutdown
Shutdown, type! Errno Status
Errno) -> Unit
Unit
266) -> Unit
Unit!type! Errno Status
Errno {
267 fn (Shutdown, Int) -> Unit
cb(Shutdown
req : type Shutdown
Shutdown, Int
status : Int
Int) {
268 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
269 (Shutdown, Errno) -> Unit
error_cb(Shutdown
req, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
270 } else {
271 (Shutdown) -> Unit
shutdown_cb(Shutdown
req)
272 }
273 }
274
275 let Shutdown
req = () -> Shutdown
uv_shutdown_make()
276 let Int
result = (shutdown : Shutdown, handle : Stream, cb : (Shutdown, Int) -> Unit) -> Int
uv_shutdown(Shutdown
req, Stream
self, (Shutdown, Int) -> Unit
cb)
277 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
278 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
279 }
280}
281
282///|
283pub fn[Stream : trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream] (stream : Stream, shutdown_cb : (Shutdown) -> Unit, error_cb : (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown(
284 Stream
stream : type parameter Stream
Stream,
285 (Shutdown) -> Unit
shutdown_cb : (type Shutdown
Shutdown) -> Unit
Unit,
286 (Shutdown, Errno) -> Unit
error_cb : (type Shutdown
Shutdown, type! Errno Status
Errno) -> Unit
Unit
287) -> Unit
Unit!type! Errno Status
Errno {
288 Stream
stream.(Stream, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown!((Shutdown) -> Unit
shutdown_cb, (Shutdown, Errno) -> Unit
error_cb)
289}
290
291///|
292impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream with (self : Self, shutdown_cb : (Shutdown) -> Unit, error_cb : (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown(Self
self, (Shutdown) -> Unit
shutdown_cb, (Shutdown, Errno) -> Unit
error_cb) {
293 Self
self.(Self) -> Stream
to_stream().(self : Stream, shutdown_cb : (Shutdown) -> Unit, error_cb : (Shutdown, Errno) -> Unit) -> Unit!Errno
shutdown!((Shutdown) -> Unit
shutdown_cb, (Shutdown, Errno) -> Unit
error_cb)
294}
295