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
,
type @c.Pointer[_]
@c.Pointer
[
Int
Int
],
type @c.Pointer[_]
@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, @c.Pointer[Int], @c.Pointer[Int]) -> Bytes
uv_alloc_cb
(
44
Handle
handle
:
type Handle
Handle
,
45
UInt64
suggested_size
:
UInt64
UInt64
,
46
@c.Pointer[Int]
buf_offset
:
type @c.Pointer[_]
@c.Pointer
[
Int
Int
],
47
@c.Pointer[Int]
buf_length
:
type @c.Pointer[_]
@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
@c.Pointer[Int]
buf_offset
[0] =
@bytes.View
bytes
.
(self : @bytes.View) -> Int

Retrieves the start index of the view.

start_offset
()
51
@c.Pointer[Int]
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, @c.Pointer[Int], @c.Pointer[Int]) -> Bytes, read_cb : (Stream, Int64, Bytes?, Int, Int) -> Unit) -> Int
uv_read_start
(
Stream
self
,
(Handle, UInt64, @c.Pointer[Int], @c.Pointer[Int]) -> 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) -> Unit 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) -> Unit 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) -> Unit 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) ->
Unit
Unit
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} 166 167///| 168#deprecated("Use Stream::write() instead") 169pub 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) -> Unit 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) -> Unit raise Errno
write
(
170
Stream
stream
:

type parameter Stream

Stream
,
171
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
],
172
() -> Unit
write_cb
: () ->
Unit
Unit
,
173
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
174) ->
Unit
Unit
raise
type! Errno Status
Errno
{
175
Stream
stream
.
(Stream, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Unit raise Errno
write
(
Array[@bytes.View]
bufs
,
() -> Unit
write_cb
,
(Errno) -> Unit
error_cb
)
176} 177 178///| 179extern "c" fn uv_write2( 180 write :
type Write
Write
,
181 handle :
type Stream
Stream
,
182 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
183 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
184 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
185 send_handle :
type Stream
Stream
,
186 cb : (
type Write
Write
,
Int
Int
) ->
Unit
Unit
187) ->
Int
Int
= "moonbit_uv_write2"
188 189///| 190pub fn
type Stream
Stream
::
(self : Stream, bufs : Array[@bytes.View], send_handle : Stream, write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Unit raise Errno
write2
(
191
Stream
self
:
type Stream
Stream
,
192
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
],
193
Stream
send_handle
:
type Stream
Stream
,
194
() -> Unit
write_cb
: () ->
Unit
Unit
,
195
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
196) ->
Unit
Unit
raise
type! Errno Status
Errno
{
197 fn
(Write, Int) -> Unit
cb
(_ :
type Write
Write
,
Int
status
:
Int
Int
) {
198 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
199
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
200 } else { 201
() -> Unit
write_cb
()
202 } 203 } 204 205 let
Write
req
=
() -> Write
uv_write_make
()
206 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
()
207 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
, [])
208 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)
209 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)
210 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
() {
211
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
()
212
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
()
213
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
()
214 } 215 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
(
Write
req
,
Stream
self
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Stream
send_handle
,
(Write, Int) -> Unit
cb
)
216 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
217 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
218 } 219} 220 221///| 222pub 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) -> Unit 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
{
223
(Self) -> Stream
to_stream
(

type parameter Self

Self
) ->
type Stream
Stream
224
(Stream) -> Self
of_stream
(
type Stream
Stream
) ->

type parameter Self

Self
225
(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit raise Errno
read_start
(
226

type parameter Self

Self
,
227 (

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
,
228 (

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
,
229 (

type parameter Self

Self
,
type! Errno Status
Errno
) ->
Unit
Unit
,
230 ) ->
Unit
Unit
raise
type! Errno Status
Errno
= _
231
(Self) -> Unit raise Errno
read_stop
(

type parameter Self

Self
) ->
Unit
Unit
raise
type! Errno Status
Errno
= _
232
(Self, Array[@bytes.View], () -> Unit, (Errno) -> Unit) -> Unit 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
) ->
Unit
Unit
raise
type! Errno Status
Errno
= _
233
(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
= _
234
(Self) -> Bool
is_readable
(

type parameter Self

Self
) ->
Bool
Bool
= _
235
(Self) -> Bool
is_writable
(

type parameter Self

Self
) ->
Bool
Bool
= _
236
(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
= _
237} 238 239///| 240pub 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) -> Unit 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"
241 242///| 243pub 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) -> Unit 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"
244 245///| 246impl
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) -> Unit 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
) {
247 // We are wrapping callbacks here, for two reasons: 248 // 1. We need to downcast the `Handle`/`Stream` to concrete types. 249 // 2. (Mutually) recursive indirect call will create a reference loop, 250 // and RC will fail to collect it. Therefore, instead of directly 251 // passing `alloc_cb`, `read_cb` and `error_cb` to `read_start()`, 252 // we wrap them into local functions, so that compiler can perform 253 // capture analysis and will not create closures that refer to each 254 // other. 255 fn
(Handle, Int) -> @bytes.View
handle_alloc_cb
(
Handle
handle
:
type Handle
Handle
,
Int
suggested_size
:
Int
Int
) {
256
(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
)
257 } 258 259 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
) {
260
(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) -> Unit 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
)
261 } 262 263 fn
(Stream, Errno) -> Unit
stream_error_cb
(
Stream
stream
:
type Stream
Stream
,
Errno
errno
:
type! Errno Status
Errno
) {
264
(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) -> Unit 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
)
265 } 266 267
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
)
268} 269 270///| 271impl
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) -> Unit 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
) {
272
Self
self
.
(Self) -> Stream
to_stream
().
(self : Stream) -> Unit raise Errno
read_stop
()
273} 274 275///| 276impl
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) -> Unit 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) -> Unit raise Errno
write
(
Self
self
,
Array[@bytes.View]
bufs
,
() -> Unit
write_cb
,
(Errno) -> Unit
error_cb
) {
277
Self
self
.
(Self) -> Stream
to_stream
().
(self : Stream, bufs : Array[@bytes.View], write_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Unit raise Errno
write
(
Array[@bytes.View]
bufs
,
() -> Unit
write_cb
,
(Errno) -> Unit
error_cb
)
278} 279 280///| 281#borrow(stream) 282extern "c" fn uv_is_readable(stream :
type Stream
Stream
) ->
Bool
Bool
= "moonbit_uv_is_readable"
283 284///| 285#borrow(stream) 286extern "c" fn uv_is_writable(stream :
type Stream
Stream
) ->
Bool
Bool
= "moonbit_uv_is_writable"
287 288///| 289pub fn
type Stream
Stream
::
(self : Stream) -> Bool
is_readable
(
Stream
self
:
type Stream
Stream
) ->
Bool
Bool
{
290
(stream : Stream) -> Bool
uv_is_readable
(
Stream
self
)
291} 292 293///| 294pub fn
type Stream
Stream
::
(self : Stream) -> Bool
is_writable
(
Stream
self
:
type Stream
Stream
) ->
Bool
Bool
{
295
(stream : Stream) -> Bool
uv_is_writable
(
Stream
self
)
296} 297 298///| 299#deprecated("Use Stream::is_readable() instead") 300pub 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) -> Unit 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
{
301
Stream
stream
.
(Stream) -> Bool
is_readable
()
302} 303 304///| 305#deprecated("Use Stream::is_writable() instead") 306pub 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) -> Unit 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
{
307
Stream
stream
.
(Stream) -> Bool
is_writable
()
308} 309 310///| 311impl
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) -> Unit 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
) {
312
Self
self
.
(Self) -> Stream
to_stream
().
(self : Stream) -> Bool
is_readable
()
313} 314 315///| 316impl
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) -> Unit 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
) {
317
Self
self
.
(Self) -> Stream
to_stream
().
(self : Stream) -> Bool
is_writable
()
318} 319 320///| 321type Shutdown 322 323///| 324pub impl
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Shutdown
Shutdown
with to_req(self :
type Shutdown
Shutdown
) ->
type Req
Req
= "%identity"
325 326///| 327extern "c" fn uv_shutdown_make() ->
type Shutdown
Shutdown
= "moonbit_uv_shutdown_make"
328 329///| 330extern "c" fn uv_shutdown( 331 shutdown :
type Shutdown
Shutdown
,
332 handle :
type Stream
Stream
,
333 cb : (
type Shutdown
Shutdown
,
Int
Int
) ->
Unit
Unit
334) ->
Int
Int
= "moonbit_uv_shutdown"
335 336///| 337pub fn
type Stream
Stream
::
(self : Stream, shutdown_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Shutdown raise Errno
shutdown
(
338
Stream
self
:
type Stream
Stream
,
339
() -> Unit
shutdown_cb
: () ->
Unit
Unit
,
340
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
341) ->
type Shutdown
Shutdown
raise
type! Errno Status
Errno
{
342 fn
(Shutdown, Int) -> Unit
cb
(_ :
type Shutdown
Shutdown
,
Int
status
:
Int
Int
) {
343 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
344
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
345 } else { 346
() -> Unit
shutdown_cb
()
347 } 348 } 349 350 let
Shutdown
req
=
() -> Shutdown
uv_shutdown_make
()
351 let
Int
result
=
(shutdown : Shutdown, handle : Stream, cb : (Shutdown, Int) -> Unit) -> Int
uv_shutdown
(
Shutdown
req
,
Stream
self
,
(Shutdown, Int) -> Unit
cb
)
352 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
353 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
)
354 } 355 return
Shutdown
req
356} 357 358///| 359#deprecated("Use Stream::shutdown() instead") 360pub 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) -> Unit 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
(
361
Stream
stream
:

type parameter Stream

Stream
,
362
() -> Unit
shutdown_cb
: () ->
Unit
Unit
,
363
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
364) ->
type Shutdown
Shutdown
raise
type! Errno Status
Errno
{
365
Stream
stream
.
(Stream, () -> Unit, (Errno) -> Unit) -> Shutdown raise Errno
shutdown
(
() -> Unit
shutdown_cb
,
(Errno) -> Unit
error_cb
)
366} 367 368///| 369impl
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) -> Unit 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
) {
370
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
)
371} 372