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 Stream
17
18///|
19pub fn type Stream
Stream::to_handle(self : type Stream
Stream) -> type Handle
Handle = "%identity"
20
21///|
22pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
loop_(Self) -> Loop
}
ToHandle for type Stream
Stream with (self : Stream) -> Handle
to_handle(Stream
self) {
23 Stream
self.(self : Stream) -> Handle
to_handle()
24}
25
26///|
27pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
loop_(Self) -> Loop
}
ToHandle for type Stream
Stream with of_handle(self) = "%identity"
28
29///|
30extern "c" fn uv_read_start(
31 stream : type Stream
Stream,
32 alloc_cb : (type Handle
Handle, UInt64
UInt64, @c.Pointer[Int
Int], @c.Pointer[Int
Int]) -> Bytes
Bytes,
33 read_cb : (type Stream
Stream, Int64
Int64, Bytes
Bytes?, Int
Int, Int
Int) -> Unit
Unit
34) -> Int
Int = "moonbit_uv_read_start"
35
36///|
37pub 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 raise Errno
read_start(
38 Stream
self : type Stream
Stream,
39 (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,
40 (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,
41 (Stream, Errno) -> Unit
error_cb : (type Stream
Stream, type! Errno Status
Errno) -> Unit
Unit
42) -> Unit
Unit raise type! Errno Status
Errno {
43 fn (Handle, UInt64, ?, ?) -> Bytes
uv_alloc_cb(
44 Handle
handle : type Handle
Handle,
45 UInt64
suggested_size : UInt64
UInt64,
46 ?
buf_offset : @c.Pointer[Int
Int],
47 ?
buf_length : @c.Pointer[Int
Int]
48 ) -> Bytes
Bytes {
49 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:
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())
50 ?
buf_offset[0] = @bytes.View
bytes.(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
51 ?
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:
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect(view.length(), content="2")
length()
52 return @bytes.View
bytes.(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
53 }
54
55 fn (Stream, Int64, Bytes?, Int, Int) -> Unit
uv_read_cb(
56 Stream
stream : type Stream
Stream,
57 Int64
count : Int64
Int64,
58 Bytes?
buf_data : Bytes
Bytes?,
59 Int
buf_offset : Int
Int,
60 Int
buf_length : Int
Int
61 ) -> Unit
Unit {
62 if Int64
count (self_ : Int64, other : Int64) -> Bool
< 0 {
63 (Stream, Errno) -> Unit
error_cb(Stream
stream, type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int64
count.(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:
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()))
64 } else {
65 (Stream, Int, @bytes.View) -> Unit
read_cb(Stream
stream, Int64
count.(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:
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])
66 }
67 }
68
69 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)
70 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
71 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
72 }
73}
74
75///|
76/// Starts reading from a stream with custom allocation and data handling
77/// callbacks.
78///
79/// Parameters:
80///
81/// * `stream` : The stream to read from.
82/// * `alloc_cb` : Callback function to allocate buffer space for incoming data.
83/// Takes the stream and suggested buffer size, returns a bytes view.
84/// * `read_cb` : Callback function to handle successfully read data. Takes the
85/// stream, number of bytes read, and the data as a bytes view.
86/// * `error_cb` : Callback function to handle read errors. Takes the stream and
87/// the error code.
88///
89/// Throws an error of type `Errno` if the operation fails to start.
90#deprecated("Use Stream::read_start() instead")
91pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream, alloc_cb : (Stream, Int) -> @bytes.View, read_cb : (Stream, Int, @bytes.View) -> Unit, error_cb : (Stream, Errno) -> Unit) -> Unit raise Errno
Starts reading from a stream with custom allocation and data handling
callbacks.
Parameters:
stream : The stream to read from.
alloc_cb : Callback function to allocate buffer space for incoming data.
Takes the stream and suggested buffer size, returns a bytes view.
read_cb : Callback function to handle successfully read data. Takes the
stream, number of bytes read, and the data as a bytes view.
error_cb : Callback function to handle read errors. Takes the stream and
the error code.
Throws an error of type Errno if the operation fails to start.
read_start(
92 Stream
stream : type parameter Stream
Stream,
93 (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,
94 (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,
95 (Stream, Errno) -> Unit
error_cb : (type parameter Stream
Stream, type! Errno Status
Errno) -> Unit
Unit
96) -> Unit
Unit raise type! Errno Status
Errno {
97 Stream
stream.(Stream, (Stream, Int) -> @bytes.View, (Stream, Int, @bytes.View) -> Unit, (Stream, Errno) -> Unit) -> Unit raise Errno
read_start((Stream, Int) -> @bytes.View
alloc_cb, (Stream, Int, @bytes.View) -> Unit
read_cb, (Stream, Errno) -> Unit
error_cb)
98}
99
100///|
101extern "c" fn uv_read_stop(stream : type Stream
Stream) -> Int
Int = "moonbit_uv_read_stop"
102
103///|
104pub fn type Stream
Stream::(self : Stream) -> Unit raise Errno
read_stop(Stream
self : type Stream
Stream) -> Unit
Unit raise type! Errno Status
Errno {
105 let Int
result = (stream : Stream) -> Int
uv_read_stop(Stream
self)
106 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
107 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
108 }
109}
110
111///|
112#deprecated("Use Stream::read_stop() instead")
113pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream) -> Unit raise Errno
read_stop(Stream
stream : type parameter Stream
Stream) -> Unit
Unit raise type! Errno Status
Errno {
114 Stream
stream.(Stream) -> Unit raise Errno
read_stop()
115}
116
117///|
118type Write
119
120///|
121pub impl trait ToReq {
to_req(Self) -> Req
}
ToReq for type Write
Write with to_req(self : type Write
Write) -> type Req
Req = "%identity"
122
123///|
124extern "c" fn uv_write_make() -> type Write
Write = "moonbit_uv_write_make"
125
126///|
127extern "c" fn uv_write(
128 write : type Write
Write,
129 handle : type Stream
Stream,
130 bufs_base : type FixedArray[A]
FixedArray[Bytes
Bytes],
131 bufs_offset : type FixedArray[A]
FixedArray[Int
Int],
132 bufs_length : type FixedArray[A]
FixedArray[Int
Int],
133 cb : (type Write
Write, Int
Int) -> Unit
Unit
134) -> Int
Int = "moonbit_uv_write"
135
136///|
137pub fn type Stream
Stream::(self : Stream, bufs : Array[@bytes.View], write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Write raise Errno
write(
138 Stream
self : type Stream
Stream,
139 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],
140 () -> Unit
write_cb : () -> Unit
Unit,
141 (Errno) -> Unit
error_cb : (type! Errno Status
Errno) -> Unit
Unit
142) -> type Write
Write raise type! Errno Status
Errno {
143 fn (Write, Int) -> Unit
cb(_ : type Write
Write, Int
status : Int
Int) {
144 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
145 (Errno) -> Unit
error_cb(type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
146 } else {
147 () -> Unit
write_cb()
148 }
149 }
150
151 let Write
req = () -> Write
uv_write_make()
152 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:
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
length()
153 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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, [])
154 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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)
155 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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)
156 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:
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
length() {
157 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[i].(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
158 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[i].(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
159 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[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:
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect(view.length(), content="2")
length()
160 }
161 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)
162 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
163 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
164 }
165 return Write
req
166}
167
168///|
169#deprecated("Use Stream::write() instead")
170pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream, bufs : Array[@bytes.View], write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Write raise Errno
write(
171 Stream
stream : type parameter Stream
Stream,
172 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],
173 () -> Unit
write_cb : () -> Unit
Unit,
174 (Errno) -> Unit
error_cb : (type! Errno Status
Errno) -> Unit
Unit
175) -> type Write
Write raise type! Errno Status
Errno {
176 Stream
stream.(Stream, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
write(Array[@bytes.View]
bufs, () -> Unit
write_cb, (Errno) -> Unit
error_cb)
177}
178
179///|
180extern "c" fn uv_write2(
181 write : type Write
Write,
182 handle : type Stream
Stream,
183 bufs_base : type FixedArray[A]
FixedArray[Bytes
Bytes],
184 bufs_offset : type FixedArray[A]
FixedArray[Int
Int],
185 bufs_length : type FixedArray[A]
FixedArray[Int
Int],
186 send_handle : type Stream
Stream,
187 cb : (type Write
Write, Int
Int) -> Unit
Unit
188) -> Int
Int = "moonbit_uv_write2"
189
190///|
191pub fn type Stream
Stream::(self : Stream, bufs : Array[@bytes.View], send_handle : Stream, write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Write raise Errno
write2(
192 Stream
self : type Stream
Stream,
193 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],
194 Stream
send_handle : type Stream
Stream,
195 () -> Unit
write_cb : () -> Unit
Unit,
196 (Errno) -> Unit
error_cb : (type! Errno Status
Errno) -> Unit
Unit
197) -> type Write
Write raise type! Errno Status
Errno {
198 fn (Write, Int) -> Unit
cb(_ : type Write
Write, Int
status : Int
Int) {
199 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
200 (Errno) -> Unit
error_cb(type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
201 } else {
202 () -> Unit
write_cb()
203 }
204 }
205
206 let Write
req = () -> Write
uv_write_make()
207 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:
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
length()
208 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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, [])
209 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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)
210 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:
let arr = FixedArray::make(3, 42)
inspect(arr[0], content="42")
inspect(arr.length(), content="3")
WARNING: A common pitfall is creating with the same initial value, for example:
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)
211 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:
let arr = [1, 2, 3]
inspect(arr.length(), content="3")
let empty : Array[Int] = []
inspect(empty.length(), content="0")
length() {
212 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[i].(self : @bytes.View) -> Bytes
Retrieves the underlying Bytes from a View.
data()
213 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[i].(self : @bytes.View) -> Int
Retrieves the start index of the view.
start_offset()
214 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:
let arr = [1, 2, 3]
arr[1] = 42
inspect(arr, content="[1, 42, 3]")
[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:
let arr = [1, 2, 3]
inspect(arr[1], content="2")
[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:
let bytes = b"\x00\x01\x02\x03\x04"
let view = bytes[2:4]
inspect(view.length(), content="2")
length()
215 }
216 let Int
result = (write : Write, handle : Stream, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], send_handle : Stream, cb : (Write, Int) -> Unit) -> Int
uv_write2(
217 Write
req, Stream
self, FixedArray[Bytes]
bufs_base, FixedArray[Int]
bufs_offset, FixedArray[Int]
bufs_length, Stream
send_handle, (Write, Int) -> Unit
cb,
218 )
219 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
220 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
221 }
222 return Write
req
223}
224
225///|
226pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream: trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
loop_(Self) -> Loop
}
ToHandle {
227 (Self) -> Stream
to_stream(type parameter Self
Self) -> type Stream
Stream
228 (Stream) -> Self
of_stream(type Stream
Stream) -> type parameter Self
Self
229 (Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
read_start(
230 type parameter Self
Self,
231 (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,
232 (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,
233 (type parameter Self
Self, type! Errno Status
Errno) -> Unit
Unit,
234 ) -> Unit
Unit raise type! Errno Status
Errno = _
235 (Self) -> Unit raise Errno
read_stop(type parameter Self
Self) -> Unit
Unit raise type! Errno Status
Errno = _
236 (Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise 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], () -> Unit
Unit, (type! Errno Status
Errno) -> Unit
Unit) -> type Write
Write raise type! Errno Status
Errno = _
237 (Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise 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 raise type! Errno Status
Errno = _
238 (Self) -> Bool
is_readable(type parameter Self
Self) -> Bool
Bool = _
239 (Self) -> Bool
is_writable(type parameter Self
Self) -> Bool
Bool = _
240 (Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
shutdown(type parameter Self
Self, () -> Unit
Unit, (type! Errno Status
Errno) -> Unit
Unit) -> type Shutdown
Shutdown raise type! Errno Status
Errno = _
241}
242
243///|
244pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream for type Stream
Stream with to_stream(self) = "%identity"
245
246///|
247pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream for type Stream
Stream with of_stream(self) = "%identity"
248
249///|
250impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self, alloc_cb : (Self, Int) -> @bytes.View, read_cb : (Self, Int, @bytes.View) -> Unit, error_cb : (Self, Errno) -> Unit) -> Unit raise Errno
read_start(Self
self, (Self, Int) -> @bytes.View
alloc_cb, (Self, Int, @bytes.View) -> Unit
read_cb, (Self, Errno) -> Unit
error_cb) {
251 // We are wrapping callbacks here, for two reasons:
252 // 1. We need to downcast the `Handle`/`Stream` to concrete types.
253 // 2. (Mutually) recursive indirect call will create a reference loop,
254 // and RC will fail to collect it. Therefore, instead of directly
255 // passing `alloc_cb`, `read_cb` and `error_cb` to `read_start()`,
256 // we wrap them into local functions, so that compiler can perform
257 // capture analysis and will not create closures that refer to each
258 // other.
259 fn (Handle, Int) -> @bytes.View
handle_alloc_cb(Handle
handle : type Handle
Handle, Int
suggested_size : Int
Int) {
260 (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
loop_(Self) -> Loop
}
ToHandle(_ : Handle) -> Self
::of_handle(Handle
handle), Int
suggested_size)
261 }
262
263 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) {
264 (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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream(_ : Stream) -> Self
::of_stream(Stream
stream), Int
count, @bytes.View
bytes)
265 }
266
267 fn (Stream, Errno) -> Unit
stream_error_cb(Stream
stream : type Stream
Stream, Errno
errno : type! Errno Status
Errno) {
268 (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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream(_ : Stream) -> Self
::of_stream(Stream
stream), Errno
errno)
269 }
270
271 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 raise Errno
read_start((Handle, Int) -> @bytes.View
handle_alloc_cb, (Stream, Int, @bytes.View) -> Unit
stream_read_cb, (Stream, Errno) -> Unit
stream_error_cb)
272}
273
274///|
275impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self) -> Unit raise Errno
read_stop(Self
self) {
276 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Unit raise Errno
read_stop()
277}
278
279///|
280impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self, bufs : Array[@bytes.View], write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Write raise Errno
write(Self
self, Array[@bytes.View]
bufs, () -> Unit
write_cb, (Errno) -> Unit
error_cb) {
281 Self
self.(Self) -> Stream
to_stream().(self : Stream, bufs : Array[@bytes.View], write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Write raise Errno
write(Array[@bytes.View]
bufs, () -> Unit
write_cb, (Errno) -> Unit
error_cb)
282}
283
284///|
285#borrow(stream)
286extern "c" fn uv_is_readable(stream : type Stream
Stream) -> Bool
Bool = "moonbit_uv_is_readable"
287
288///|
289#borrow(stream)
290extern "c" fn uv_is_writable(stream : type Stream
Stream) -> Bool
Bool = "moonbit_uv_is_writable"
291
292///|
293pub fn type Stream
Stream::(self : Stream) -> Bool
is_readable(Stream
self : type Stream
Stream) -> Bool
Bool {
294 (stream : Stream) -> Bool
uv_is_readable(Stream
self)
295}
296
297///|
298pub fn type Stream
Stream::(self : Stream) -> Bool
is_writable(Stream
self : type Stream
Stream) -> Bool
Bool {
299 (stream : Stream) -> Bool
uv_is_writable(Stream
self)
300}
301
302///|
303#deprecated("Use Stream::is_readable() instead")
304pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream) -> Bool
is_readable(Stream
stream : type parameter Stream
Stream) -> Bool
Bool {
305 Stream
stream.(Stream) -> Bool
is_readable()
306}
307
308///|
309#deprecated("Use Stream::is_writable() instead")
310pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream) -> Bool
is_writable(Stream
stream : type parameter Stream
Stream) -> Bool
Bool {
311 Stream
stream.(Stream) -> Bool
is_writable()
312}
313
314///|
315impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self) -> Bool
is_readable(Self
self) {
316 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Bool
is_readable()
317}
318
319///|
320impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self) -> Bool
is_writable(Self
self) {
321 Self
self.(Self) -> Stream
to_stream().(self : Stream) -> Bool
is_writable()
322}
323
324///|
325type Shutdown
326
327///|
328pub impl trait ToReq {
to_req(Self) -> Req
}
ToReq for type Shutdown
Shutdown with to_req(self : type Shutdown
Shutdown) -> type Req
Req = "%identity"
329
330///|
331extern "c" fn uv_shutdown_make() -> type Shutdown
Shutdown = "moonbit_uv_shutdown_make"
332
333///|
334extern "c" fn uv_shutdown(
335 shutdown : type Shutdown
Shutdown,
336 handle : type Stream
Stream,
337 cb : (type Shutdown
Shutdown, Int
Int) -> Unit
Unit
338) -> Int
Int = "moonbit_uv_shutdown"
339
340///|
341pub fn type Stream
Stream::(self : Stream, shutdown_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Shutdown raise Errno
shutdown(
342 Stream
self : type Stream
Stream,
343 () -> Unit
shutdown_cb : () -> Unit
Unit,
344 (Errno) -> Unit
error_cb : (type! Errno Status
Errno) -> Unit
Unit
345) -> type Shutdown
Shutdown raise type! Errno Status
Errno {
346 fn (Shutdown, Int) -> Unit
cb(_ : type Shutdown
Shutdown, Int
status : Int
Int) {
347 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
348 (Errno) -> Unit
error_cb(type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status))
349 } else {
350 () -> Unit
shutdown_cb()
351 }
352 }
353
354 let Shutdown
req = () -> Shutdown
uv_shutdown_make()
355 let Int
result = (shutdown : Shutdown, handle : Stream, cb : (Shutdown, Int) -> Unit) -> Int
uv_shutdown(Shutdown
req, Stream
self, (Shutdown, Int) -> Unit
cb)
356 if Int
result (self_ : Int, other : Int) -> Bool
< 0 {
357 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
result)
358 }
359 return Shutdown
req
360}
361
362///|
363#deprecated("Use Stream::shutdown() instead")
364pub 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream] (stream : Stream, shutdown_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Shutdown raise Errno
shutdown(
365 Stream
stream : type parameter Stream
Stream,
366 () -> Unit
shutdown_cb : () -> Unit
Unit,
367 (Errno) -> Unit
error_cb : (type! Errno Status
Errno) -> Unit
Unit
368) -> type Shutdown
Shutdown raise type! Errno Status
Errno {
369 Stream
stream.(Stream, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
shutdown(() -> Unit
shutdown_cb, (Errno) -> Unit
error_cb)
370}
371
372///|
373impl 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 raise Errno
read_stop(Self) -> Unit raise Errno
write(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Write raise Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
}
ToStream with (self : Self, shutdown_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Shutdown raise Errno
shutdown(Self
self, () -> Unit
shutdown_cb, (Errno) -> Unit
error_cb) {
374 Self
self.(Self) -> Stream
to_stream().(self : Stream, shutdown_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Shutdown raise Errno
shutdown(() -> Unit
shutdown_cb, (Errno) -> Unit
error_cb)
375}
376