1///|
2type Fs
3
4///|
5pub impl 
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Fs
Fs
with to_req(self :
type Fs
Fs
) ->
type Req
Req
= "%identity"
6 7///| 8extern "c" fn uv_fs_make() ->
type Fs
Fs
= "moonbit_uv_fs_make"
9 10///| 11type OpenFlags
Int
Int
12 13///| 14pub fn
type OpenFlags Int
OpenFlags
::
(create~ : Bool = ..) -> OpenFlags
read
(
Bool
create
~ :
Bool
Bool
= false) ->
type OpenFlags Int
OpenFlags
{
15 let mut
Int
flags
=
Int
_O_RDONLY
16 if
Bool
create
{
17
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_CREAT
18 } 19
Int
flags
20} 21 22///| 23pub fn
type OpenFlags Int
OpenFlags
::
(create~ : Bool = ..) -> OpenFlags
write
(
Bool
create
~ :
Bool
Bool
= false) ->
type OpenFlags Int
OpenFlags
{
24 let mut
Int
flags
=
Int
_O_WRONLY
25 if
Bool
create
{
26
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_CREAT
27 } 28
Int
flags
29} 30 31///| 32pub fn
type OpenFlags Int
OpenFlags
::
(create~ : Bool = ..) -> OpenFlags
read_write
(
Bool
create
~ :
Bool
Bool
= false) ->
type OpenFlags Int
OpenFlags
{
33 let mut
Int
flags
=
Int
_O_RDWR
34 if
Bool
create
{
35
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_CREAT
36 } 37
Int
flags
38} 39 40///| 41extern "c" fn uv_fs_O_RDONLY() ->
Int
Int
= "moonbit_uv_fs_O_RDONLY"
42 43///| 44let
Int
_O_RDONLY
:
Int
Int
=
() -> Int
uv_fs_O_RDONLY
()
45 46///| 47extern "c" fn uv_fs_O_WRONLY() ->
Int
Int
= "moonbit_uv_fs_O_WRONLY"
48 49///| 50let
Int
_O_WRONLY
:
Int
Int
=
() -> Int
uv_fs_O_WRONLY
()
51 52///| 53extern "c" fn uv_fs_O_RDWR() ->
Int
Int
= "moonbit_uv_fs_O_RDWR"
54 55///| 56let
Int
_O_RDWR
:
Int
Int
=
() -> Int
uv_fs_O_RDWR
()
57 58///| 59extern "c" fn uv_fs_O_CREAT() ->
Int
Int
= "moonbit_uv_fs_O_CREAT"
60 61///| 62let
Int
_O_CREAT
:
Int
Int
=
() -> Int
uv_fs_O_CREAT
()
63 64///| 65extern "c" fn uv_fs_open( 66 uv :
type Loop Bytes
Loop
,
67 req :
type Fs
Fs
,
68 path :
Bytes
Bytes
,
69 flags :
Int
Int
,
70 mode :
Int
Int
,
71 cb : (
type Fs
Fs
) ->
Unit
Unit
72) ->
Int
Int
= "moonbit_uv_fs_open"
73 74///| 75pub(open) trait
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
{
76
(Self) -> Bytes
to_ntbs
(

type parameter Self

Self
) ->
Bytes
Bytes
77} 78 79///| 80pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
for
String
String
with
(self : String) -> Bytes
to_ntbs
(
String
self
:
String
String
) ->
Bytes
Bytes
{
81 let
@buffer.T
buffer
=
(size_hint~ : Int = ..) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
()
82
(src : String, buffer : @buffer.T, encoding~ : @encoding.Encoding) -> Unit

Encodes a string into the specified character encoding and writes the result directly into a buffer.

Parameters:

  • string : The input string to be encoded.
  • buffer : The buffer where the encoded bytes will be written to.
  • encoding : The target encoding format. Defaults to UTF8 if not specified.

Example:

test "encode_to" {
  let buf = @buffer.new()
  let text = "Hello, world"
  @encoding.encode_to(text, buf, encoding=UTF16)
  inspect!(buf.to_string(), content="Hello, world")
}
@encoding.encode_to
(
String
self
,
@buffer.T
buffer
,
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
)
83
@buffer.T
buffer
.
(self : @buffer.T, value : Byte) -> Unit

Writes a single byte to the end of the buffer. The buffer will automatically grow if necessary to accommodate the new byte.

Parameters:

  • buffer : The buffer to write to.
  • byte : The byte value to be written.

Example:

test "write_byte" {
  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte
(0)
84 return
@buffer.T
buffer
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
()
85} 86 87///| 88pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
for
Bytes
Bytes
with
(self : Bytes) -> Bytes
to_ntbs
(
Bytes
self
:
Bytes
Bytes
) ->
Bytes
Bytes
{
89 let
@buffer.T
buffer
=
(size_hint~ : Int = ..) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
()
90
@buffer.T
buffer
.
(self : @buffer.T, value : Bytes) -> Unit

Writes a sequence of bytes into the buffer.

Parameters:

  • buffer : An extensible buffer to write into.
  • bytes : The sequence of bytes to be written.

Example:

test "write_bytes" {
  let buf = @buffer.new()
  buf.write_bytes(b"Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x65\x73\x74"
    ,
  )
}
write_bytes
(
Bytes
self
)
91
@buffer.T
buffer
.
(self : @buffer.T, value : Byte) -> Unit

Writes a single byte to the end of the buffer. The buffer will automatically grow if necessary to accommodate the new byte.

Parameters:

  • buffer : The buffer to write to.
  • byte : The byte value to be written.

Example:

test "write_byte" {
  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte
(0)
92 return
@buffer.T
buffer
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
()
93} 94 95///| 96pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
for
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
with
(self : @string.StringView) -> Bytes
to_ntbs
(
@string.StringView
self
:
type @string.StringView

A @string.View represents a view of a String that maintains proper Unicode character boundaries. It allows safe access to a substring while handling multi-byte characters correctly.

@string.View
) ->
Bytes
Bytes
{
97
@string.StringView
self
.
(self : @string.StringView) -> String

Returns a new String containing a copy of the characters in this view.

Examples

let str = "Hello World"
let view = str[0:5]  // "Hello"
inspect(view.to_string(), content="Hello")
to_string
().
(self : String) -> Bytes
to_ntbs
()
98} 99 100///| 101pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
for
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
with
(self : @bytes.View) -> Bytes
to_ntbs
(
@bytes.View
self
:
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
) ->
Bytes
Bytes
{
102 let
@buffer.T
buffer
=
(size_hint~ : Int = ..) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
()
103
@buffer.T
buffer
.
(self : @buffer.T, value : @bytes.View) -> Unit

Writes a sequence of bytes from a @bytes.View into the buffer.

Parameters:

  • buffer : The buffer to write to.
  • value : The View containing the bytes to write.

Example:

test "write_bytesview" {
  let buf = @buffer.new()
  let view = b"Test"[1:3]
  buf.write_bytesview(view)
  inspect(
    buf.contents(),
    content=
      #|b"\x65\x73"
    ,
  )
}
write_bytesview
(
@bytes.View
self
)
104
@buffer.T
buffer
.
(self : @buffer.T, value : Byte) -> Unit

Writes a single byte to the end of the buffer. The buffer will automatically grow if necessary to accommodate the new byte.

Parameters:

  • buffer : The buffer to write to.
  • byte : The byte value to be written.

Example:

test "write_byte" {
  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte
(0)
105 return
@buffer.T
buffer
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
()
106} 107 108///| 109pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, flags : OpenFlags, mode : Int, open_cb : (Fs, File) -> Unit, error_cb : (Fs, Errno) -> Unit) -> Unit!Errno
fs_open
(
110
Loop
self
:
type Loop Bytes
Loop
,
111
Ntbs
path
:

type parameter Ntbs

Ntbs
,
112
OpenFlags
flags
:
type OpenFlags Int
OpenFlags
,
113
Int
mode
:
Int
Int
,
114
(Fs, File) -> Unit
open_cb
: (
type Fs
Fs
,
type File Int
File
) ->
Unit
Unit
,
115
(Fs, Errno) -> Unit
error_cb
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
116) ->
Unit
Unit
!
type! Errno Status
Errno
{
117 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
118 let
Int
result
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
119 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
120
(Fs, Errno) -> Unit
error_cb
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
))
121 } else { 122
(Fs, File) -> Unit
open_cb
(
Fs
req
,
(Int) -> File
File
(
Int
result
))
123 } 124 } 125 126 let
Fs
req
=
() -> Fs
uv_fs_make
()
127 let
Bytes
path
=
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
()
128 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, flags : Int, mode : Int, cb : (Fs) -> Unit) -> Int
uv_fs_open
(
Loop
self
,
Fs
req
,
Bytes
path
,
OpenFlags
flags
.
Int
_
,
Int
mode
,
(Fs) -> Unit
cb
)
129 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
130 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
131 } 132} 133 134///| 135extern "c" fn uv_fs_close( 136 uv :
type Loop Bytes
Loop
,
137 req :
type Fs
Fs
,
138 file :
type File Int
File
,
139 cb : (
type Fs
Fs
) ->
Unit
Unit
140) ->
Int
Int
= "moonbit_uv_fs_close"
141 142///| 143pub fn
(self : Loop, file : File, cb : (Fs) -> Unit) -> Unit!Errno
fs_close
(
Loop
self
:
type Loop Bytes
Loop
,
File
file
:
type File Int
File
,
(Fs) -> Unit
cb
: (
type Fs
Fs
) ->
Unit
Unit
) ->
Unit
Unit
!
type! Errno Status
Errno
{
144 let
Fs
req
=
() -> Fs
uv_fs_make
()
145 let
Int
status
=
(uv : Loop, req : Fs, file : File, cb : (Fs) -> Unit) -> Int
uv_fs_close
(
Loop
self
,
Fs
req
,
File
file
,
(Fs) -> Unit
cb
)
146 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
147 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
148 } 149} 150 151///| 152extern "c" fn uv_fs_get_result(req :
type Fs
Fs
) ->
Int64
Int64
= "moonbit_uv_fs_get_result"
153 154///| 155fn
type Fs
Fs
::
(self : Fs) -> Int64
result
(
Fs
self
:
type Fs
Fs
) ->
Int64
Int64
{
156
(req : Fs) -> Int64
uv_fs_get_result
(
Fs
self
)
157} 158 159///| 160type File
Int
Int
161 162///| 163extern "c" fn uv_fs_read( 164 uv :
type Loop Bytes
Loop
,
165 req :
type Fs
Fs
,
166 file :
type File Int
File
,
167 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
168 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
169 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
170 offset :
Int64
Int64
,
171 cb : (
type Fs
Fs
) ->
Unit
Unit
172) ->
Int
Int
= "moonbit_uv_fs_read"
173 174///| 175pub fn
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = .., read_cb : (Fs, Int) -> Unit, error_cb : (Fs, Errno) -> Unit) -> Unit!Errno
fs_read
(
176
Loop
self
:
type Loop Bytes
Loop
,
177
File
file
:
type File Int
File
,
178
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
],
179
Int64
offset
~ :
Int64
Int64
= -1,
180
(Fs, Int) -> Unit
read_cb
: (
type Fs
Fs
,
Int
Int
) ->
Unit
Unit
,
181
(Fs, Errno) -> Unit
error_cb
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
182) ->
Unit
Unit
!
type! Errno Status
Errno
{
183 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
184 let
Int64
result
=
Fs
req
.
(self : Fs) -> Int64
result
()
185 if
Int64
result
(self_ : Int64, other : Int64) -> Bool
<
0 {
186
(Fs, Errno) -> Unit
error_cb
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int64
result
.
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()))
187 } else { 188
(Fs, Int) -> Unit
read_cb
(
Fs
req
,
Int64
result
.
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
())
189 } 190 } 191 192 let
Fs
req
=
() -> Fs
uv_fs_make
()
193 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
()
194 let
FixedArray[Bytes]
bufs_base
:
type FixedArray[A]
FixedArray
[
Bytes
Bytes
] =
type FixedArray[A]
FixedArray
::
(len : Int, init : Bytes) -> FixedArray[Bytes]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, [])
195 let
FixedArray[Int]
bufs_offset
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Int) -> FixedArray[Int]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, 0)
196 let
FixedArray[Int]
bufs_length
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Int) -> FixedArray[Int]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, 0)
197 for
Int
i
in
Int
0
..<
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
() {
198
FixedArray[Bytes]
bufs_base
(FixedArray[Bytes], Int, Bytes) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

data
()
199
FixedArray[Int]
bufs_offset
(FixedArray[Int], Int, Int) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

start_offset
()
200
FixedArray[Int]
bufs_length
(FixedArray[Int], Int, Int) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

test "View::length" {
  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
}
length
()
201 } 202 let
Int
status
=
(uv : Loop, req : Fs, file : File, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], offset : Int64, cb : (Fs) -> Unit) -> Int
uv_fs_read
(
203
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
(Fs) -> Unit
cb
,
204 ) 205 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
206 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
207 } 208} 209 210///| 211extern "c" fn uv_fs_write( 212 uv :
type Loop Bytes
Loop
,
213 req :
type Fs
Fs
,
214 file :
type File Int
File
,
215 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
216 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
217 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
218 offset :
Int64
Int64
,
219 write_cb : (
type Fs
Fs
) ->
Unit
Unit
220) ->
Int
Int
= "moonbit_uv_fs_write"
221 222///| 223pub fn
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = .., write_cb : (Fs, Int) -> Unit, error_cb : (Fs, Errno) -> Unit) -> Unit!Errno
fs_write
(
224
Loop
self
:
type Loop Bytes
Loop
,
225
File
file
:
type File Int
File
,
226
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
],
227
Int64
offset
~ :
Int64
Int64
= -1,
228
(Fs, Int) -> Unit
write_cb
: (
type Fs
Fs
,
Int
Int
) ->
Unit
Unit
,
229
(Fs, Errno) -> Unit
error_cb
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
230) ->
Unit
Unit
!
type! Errno Status
Errno
{
231 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
232 let
Int
result
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
233 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
234
(Fs, Errno) -> Unit
error_cb
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
))
235 } else { 236
(Fs, Int) -> Unit
write_cb
(
Fs
req
,
Int
result
)
237 } 238 } 239 240 let
Fs
req
=
() -> Fs
uv_fs_make
()
241 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

test "Array::length" {
  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
}
length
()
242 let
FixedArray[Bytes]
bufs_base
:
type FixedArray[A]
FixedArray
[
Bytes
Bytes
] =
type FixedArray[A]
FixedArray
::
(len : Int, init : Bytes) -> FixedArray[Bytes]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, [])
243 let
FixedArray[Int]
bufs_offset
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Int) -> FixedArray[Int]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, 0)
244 let
FixedArray[Int]
bufs_length
=
type FixedArray[A]
FixedArray
::
(len : Int, init : Int) -> FixedArray[Int]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

test "FixedArray::make" {
  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")
}

test "panic FixedArray::make/negative_length" {
  ignore(FixedArray::make(-1, 0))
}

WARNING: A common pitfall is creating with the same initial value, for example:

test {
  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)
}

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(
Int
bufs_size
, 0)
245 for
Int
i
in
Int
0
..<
Int
bufs_size
{
246
FixedArray[Bytes]
bufs_base
(FixedArray[Bytes], Int, Bytes) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

data
()
247
FixedArray[Int]
bufs_offset
(FixedArray[Int], Int, Int) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

start_offset
()
248
FixedArray[Int]
bufs_length
(FixedArray[Int], Int, Int) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

test "FixedArray::op_set" {
  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
}

test "panic FixedArray::op_set/out_of_bounds" {
  let arr = [1, 2, 3]
  arr[3] = 42 // Index out of bounds
}
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

test "Array::op_get" {
  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
}

test "panic Array::op_get/out_of_bounds" {
  let arr = [1, 2, 3]
  ignore(arr[3]) // Index out of bounds
}
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

test "View::length" {
  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
}
length
()
249 } 250 let
Int
status
=
(uv : Loop, req : Fs, file : File, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], offset : Int64, write_cb : (Fs) -> Unit) -> Int
uv_fs_write
(
251
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
(Fs) -> Unit
cb
,
252 ) 253 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
254 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
255 } 256} 257 258///| 259pub fn
() -> File
stdin
() ->
type File Int
File
{
260 0 261} 262 263///| 264pub fn
() -> File
stdout
() ->
type File Int
File
{
265 1 266} 267 268///| 269pub fn
() -> File
stderr
() ->
type File Int
File
{
270 2 271} 272 273///| 274extern "c" fn uv_fs_unlink( 275 uv :
type Loop Bytes
Loop
,
276 req :
type Fs
Fs
,
277 path :
Bytes
Bytes
,
278 cb : (
type Fs
Fs
) ->
Unit
Unit
279) ->
Int
Int
= "moonbit_uv_fs_unlink"
280 281///| 282pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, k : (Fs) -> Unit, e : (Fs, Error) -> Unit) -> Unit!Errno
fs_unlink
(
283
Loop
self
:
type Loop Bytes
Loop
,
284
Ntbs
path
:

type parameter Ntbs

Ntbs
,
285
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
286
(Fs, Error) -> Unit
e
: (
type Fs
Fs
,
type Error[A]
Error
) ->
Unit
Unit
287) ->
Unit
Unit
!
type! Errno Status
Errno
{
288 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
289 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
290 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
291
(Fs, Error) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
292 } else { 293
(Fs) -> Unit
k
(
Fs
req
)
294 } 295 } 296 297 let
Fs
req
=
() -> Fs
uv_fs_make
()
298 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_unlink
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
299 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
300 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
301 } 302} 303 304///| 305extern "c" fn uv_fs_mkdir( 306 uv :
type Loop Bytes
Loop
,
307 req :
type Fs
Fs
,
308 path :
Bytes
Bytes
,
309 mode :
Int
Int
,
310 cb : (
type Fs
Fs
) ->
Unit
Unit
311) ->
Int
Int
= "moonbit_uv_fs_mkdir"
312 313///| 314pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, mode : Int, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_mkdir
(
315
Loop
self
:
type Loop Bytes
Loop
,
316
Ntbs
path
:

type parameter Ntbs

Ntbs
,
317
Int
mode
:
Int
Int
,
318
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
319
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
320) ->
Unit
Unit
!
type! Errno Status
Errno
{
321 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
322 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
323 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
324
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
325 } else { 326
(Fs) -> Unit
k
(
Fs
req
)
327 } 328 } 329 330 let
Fs
req
=
() -> Fs
uv_fs_make
()
331 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, mode : Int, cb : (Fs) -> Unit) -> Int
uv_fs_mkdir
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
Int
mode
,
(Fs) -> Unit
cb
)
332 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
333 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
334 } 335} 336 337///| 338extern "c" fn uv_fs_rmdir( 339 uv :
type Loop Bytes
Loop
,
340 req :
type Fs
Fs
,
341 path :
Bytes
Bytes
,
342 cb : (
type Fs
Fs
) ->
Unit
Unit
343) ->
Int
Int
= "moonbit_uv_fs_rmdir"
344 345///| 346pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_rmdir
(
347
Loop
self
:
type Loop Bytes
Loop
,
348
Ntbs
path
:

type parameter Ntbs

Ntbs
,
349
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
350
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
351) ->
Unit
Unit
!
type! Errno Status
Errno
{
352 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
353 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
354 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
355
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
356 } else { 357
(Fs) -> Unit
k
(
Fs
req
)
358 } 359 } 360 361 let
Fs
req
=
() -> Fs
uv_fs_make
()
362 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_rmdir
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
363 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
364 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
365 } 366} 367 368///| 369extern "c" fn uv_fs_scandir( 370 uv :
type Loop Bytes
Loop
,
371 req :
type Fs
Fs
,
372 path :
Bytes
Bytes
,
373 flags :
Int
Int
,
374 cb : (
type Fs
Fs
) ->
Unit
Unit
375) ->
Int
Int
= "moonbit_uv_fs_scandir"
376 377///| 378type Dirent 379 380///| 381pub enum DirentType { 382
DirentType
Unknown
383
DirentType
File
384
DirentType
Dir
385
DirentType
Link
386
DirentType
Fifo
387
DirentType
Socket
388
DirentType
Char
389
DirentType
Block
390} derive(
(DirentType, &Logger) -> Unit

Trait for types that can be converted to String

Show
)
391 392///| 393pub impl
trait ToJson {
  to_json(Self) -> Json
}

Trait for types that can be converted to Json

ToJson
for
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}
DirentType
with
(self : DirentType) -> Json
to_json
(
DirentType
self
) ->
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
{
394
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
::
(String) -> Json

Creates a JSON string value from a MoonBit string.

Parameters:

  • string : A MoonBit string to be converted to a JSON string value.

Returns a JSON value representing the given string.

Example:

test "string" {
  inspect(Json::string("hello"), content="String(\"hello\")")
}
string
(
DirentType
self
.
(self : DirentType) -> String
to_string
())
395} 396 397///| 398fn init { 399
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Unknown
)
400
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
File
)
401
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Dir
)
402
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Link
)
403
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Fifo
)
404
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Socket
)
405
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Char
)
406
(t : DirentType) -> Unit

Evaluates an expression and discards its result. This is useful when you want to execute an expression for its side effects but don't care about its return value, or when you want to explicitly indicate that a value is intentionally unused.

Parameters:

  • value : The value to be ignored. Can be of any type.

Example:

test "ignore" {
  let x = 42
  ignore(x) // Explicitly ignore the value
  let mut sum = 0
  ignore([1, 2, 3].iter().each(fn(x) { sum = sum + x })) // Ignore the Unit return value of each()
}
ignore
(DirentType::
DirentType
Block
)
407} 408 409///| 410extern "c" fn uv_dirent_make() ->
type Dirent
Dirent
= "moonbit_uv_dirent_make"
411 412///| 413extern "c" fn uv_dirent_get_name(dirent :
type Dirent
Dirent
) -> @c.Pointer[
Byte
Byte
] = "moonbit_uv_dirent_get_name"
414 415///| 416pub fn
type Dirent
Dirent
::
(self : Dirent) -> String
name
(
Dirent
self
:
type Dirent
Dirent
) ->
String
String
{
417 let
?
name
=
(dirent : Dirent) -> ?
uv_dirent_get_name
(
Dirent
self
)
418 let
@buffer.T
buffer
=
(size_hint~ : Int = ..) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
()
419 for
Int
i
= 0;
?
name
(Int) -> Int
[
i]
(x : Int, y : Int) -> Bool
!=
0;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
420
@buffer.T
buffer
.
(self : @buffer.T, value : Byte) -> Unit

Writes a single byte to the end of the buffer. The buffer will automatically grow if necessary to accommodate the new byte.

Parameters:

  • buffer : The buffer to write to.
  • byte : The byte value to be written.

Example:

test "write_byte" {
  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte
(
?
name
(Int) -> Byte
[
i])
421 } 422 let
Bytes
bytes
=
@buffer.T
buffer
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
()
423 return
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
)
424} 425 426///| 427extern "c" fn uv_dirent_get_type(dirent :
type Dirent
Dirent
) ->
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}
DirentType
= "moonbit_uv_dirent_get_type"
428 429///| 430pub fn
type Dirent
Dirent
::
(self : Dirent) -> DirentType
type_
(
Dirent
self
:
type Dirent
Dirent
) ->
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}
DirentType
{
431
(dirent : Dirent) -> DirentType
uv_dirent_get_type
(
Dirent
self
)
432} 433 434///| 435extern "c" fn uv_fs_scandir_next(req :
type Fs
Fs
, ent :
type Dirent
Dirent
) ->
Int
Int
= "moonbit_uv_fs_scandir_next"
436 437///| 438pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, flags : Int, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_scandir
(
439
Loop
self
:
type Loop Bytes
Loop
,
440
Ntbs
path
:

type parameter Ntbs

Ntbs
,
441
Int
flags
:
Int
Int
,
442
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
443
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
444) ->
Unit
Unit
!
type! Errno Status
Errno
{
445 let
Fs
req
=
() -> Fs
uv_fs_make
()
446 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, flags : Int, cb : (Fs) -> Unit) -> Int
uv_fs_scandir
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
Int
flags
, fn(
Fs
req
) {
447 let
Int
status
=
(req : Fs) -> Int64
uv_fs_get_result
(
Fs
req
).
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
448 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
449 return
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
450 } else { 451
(Fs) -> Unit
k
(
Fs
req
)
452 } 453 }) 454 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
455 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
456 } 457} 458 459///| 460pub fn
(req : Fs) -> Dirent!Errno
fs_scandir_next
(
Fs
req
:
type Fs
Fs
) ->
type Dirent
Dirent
!
type! Errno Status
Errno
{
461 let
Dirent
dirent
=
() -> Dirent
uv_dirent_make
()
462 let
Int
status
=
(req : Fs, ent : Dirent) -> Int
uv_fs_scandir_next
(
Fs
req
,
Dirent
dirent
)
463 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
464 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
465 } 466
Dirent
dirent
467} 468 469///| 470extern "c" fn uv_fs_rename( 471 uv :
type Loop Bytes
Loop
,
472 req :
type Fs
Fs
,
473 path :
Bytes
Bytes
,
474 new_path :
Bytes
Bytes
,
475 cb : (
type Fs
Fs
) ->
Unit
Unit
476) ->
Int
Int
= "moonbit_uv_fs_rename"
477 478///| 479pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, new_path : Ntbs, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_rename
(
480
Loop
self
:
type Loop Bytes
Loop
,
481
Ntbs
path
:

type parameter Ntbs

Ntbs
,
482
Ntbs
new_path
:

type parameter Ntbs

Ntbs
,
483
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
484
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
485) ->
Unit
Unit
!
type! Errno Status
Errno
{
486 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
487 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
488 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
489
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
490 } else { 491
(Fs) -> Unit
k
(
Fs
req
)
492 } 493 } 494 495 let
Fs
req
=
() -> Fs
uv_fs_make
()
496 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, new_path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_rename
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
Ntbs
new_path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
497 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
498 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
499 } 500} 501 502///| 503type CopyFileFlags
Int
Int
504 505///| 506extern "c" fn uv_fs_COPYFILE_EXCL() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_EXCL"
507 508///| 509extern "c" fn uv_fs_COPYFILE_FICLONE() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_FICLONE"
510 511///| 512extern "c" fn uv_fs_COPYFILE_FICLONE_FORCE() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_FICLONE_FORCE"
513 514///| 515pub(all) enum CopyOnWrite { 516
CopyOnWrite
False
517
CopyOnWrite
True
518
CopyOnWrite
Force
519} 520 521///| 522pub fn
type CopyFileFlags Int
CopyFileFlags
::
(allow_exists~ : Bool = .., copy_on_write~ : CopyOnWrite = ..) -> CopyFileFlags
new
(
523
Bool
allow_exists
~ :
Bool
Bool
= true,
524
CopyOnWrite
copy_on_write
~ :
enum CopyOnWrite {
  False
  True
  Force
}
CopyOnWrite
=
CopyOnWrite
False
525) ->
type CopyFileFlags Int
CopyFileFlags
{
526 let mut
Int
flags
= if
Bool
allow_exists
{ 0 } else {
() -> Int
uv_fs_COPYFILE_EXCL
() }
527 match
CopyOnWrite
copy_on_write
{
528
CopyOnWrite
False
=> ()
529
CopyOnWrite
True
=>
Int
flags
=
Int
flags
(Int, Int) -> Int
|
() -> Int
uv_fs_COPYFILE_FICLONE
()
530
CopyOnWrite
Force
=>
Int
flags
=
Int
flags
(Int, Int) -> Int
|
() -> Int
uv_fs_COPYFILE_FICLONE_FORCE
()
531 } 532 return
Int
flags
533} 534 535///| 536extern "c" fn uv_fs_copyfile( 537 uv :
type Loop Bytes
Loop
,
538 req :
type Fs
Fs
,
539 path :
Bytes
Bytes
,
540 new_path :
Bytes
Bytes
,
541 flags :
Int
Int
,
542 cb : (
type Fs
Fs
) ->
Unit
Unit
543) ->
Int
Int
= "moonbit_uv_fs_copyfile"
544 545///| 546pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, new_path : Ntbs, flags : CopyFileFlags, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_copyfile
(
547
Loop
self
:
type Loop Bytes
Loop
,
548
Ntbs
path
:

type parameter Ntbs

Ntbs
,
549
Ntbs
new_path
:

type parameter Ntbs

Ntbs
,
550
CopyFileFlags
flags
:
type CopyFileFlags Int
CopyFileFlags
,
551
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
552
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
553) ->
Unit
Unit
!
type! Errno Status
Errno
{
554 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
555 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
556 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
557
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
558 } else { 559
(Fs) -> Unit
k
(
Fs
req
)
560 } 561 } 562 563 let
Fs
req
=
() -> Fs
uv_fs_make
()
564 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, new_path : Bytes, flags : Int, cb : (Fs) -> Unit) -> Int
uv_fs_copyfile
(
565
Loop
self
,
566
Fs
req
,
567
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
568
Ntbs
new_path
.
(Ntbs) -> Bytes
to_ntbs
(),
569
CopyFileFlags
flags
.
Int
_
,
570
(Fs) -> Unit
cb
,
571 ) 572 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
573 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
574 } 575} 576 577///| 578extern "c" fn uv_cwd(buffer :
Bytes
Bytes
, length :
type FixedArray[A]
FixedArray
[
Int
Int
]) ->
Int
Int
= "moonbit_uv_cwd"
579 580///| 581pub fn
() -> String!Errno
cwd
() ->
String
String
!
type! Errno Status
Errno
{
582 let
Bytes
buffer
=
(Int, Byte) -> Bytes
Bytes::
(len : Int, init : Byte) -> Bytes

Creates a new byte sequence of the specified length, where each byte is initialized to the given value. Returns an empty byte sequence if the length is negative.

Parameters:

  • length : The length of the byte sequence to create. Must be non-negative.
  • initial_value : The byte value used to initialize each position in the sequence.

Example:

test "Bytes::make" {
  let bytes = Bytes::make(3, b'\xFF')
  inspect(bytes, content=
  #|b"\xff\xff\xff"
)
  let empty = Bytes::make(0, b'\x00')
  inspect(empty, content="b\"\"")
}
make
(1024, 0)
583 let
FixedArray[Int]
length
:
type FixedArray[A]
FixedArray
[
Int
Int
] = [
Bytes
buffer
.
(self : Bytes) -> Int

Returns the number of bytes in a byte sequence.

Parameters:

  • bytes : The byte sequence whose length is to be determined.

Returns an integer representing the length (number of bytes) of the sequence.

Example:

test "Bytes::length" {
  let bytes = b"\x01\x02\x03"
  inspect(bytes.length(), content="3")
  let empty = b""
  inspect(empty.length(), content="0")
}
length
()]
584 let mut
Int
status
=
(buffer : Bytes, length : FixedArray[Int]) -> Int
uv_cwd
(
Bytes
buffer
,
FixedArray[Int]
length
)
585 if
Int
status
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "Int::op_equal" {
  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
}
==
() -> Int
uv_ENOBUFS
() {
586 let
Bytes
buffer
=
(Int, Byte) -> Bytes
Bytes::
(len : Int, init : Byte) -> Bytes

Creates a new byte sequence of the specified length, where each byte is initialized to the given value. Returns an empty byte sequence if the length is negative.

Parameters:

  • length : The length of the byte sequence to create. Must be non-negative.
  • initial_value : The byte value used to initialize each position in the sequence.

Example:

test "Bytes::make" {
  let bytes = Bytes::make(3, b'\xFF')
  inspect(bytes, content=
  #|b"\xff\xff\xff"
)
  let empty = Bytes::make(0, b'\x00')
  inspect(empty, content="b\"\"")
}
make
(
FixedArray[Int]
length
[0], 0)
587
Int
status
=
(buffer : Bytes, length : FixedArray[Int]) -> Int
uv_cwd
(
Bytes
buffer
,
FixedArray[Int]
length
)
588 } 589 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
590 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
591 } 592 return
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
buffer
[:
FixedArray[Int]
length
[0]])
593} 594 595///| 596extern "c" fn uv_fs_get_ptr(req :
type Fs
Fs
) -> @c.Pointer[
Unit
Unit
] = "moonbit_uv_fs_get_ptr"
597 598///| 599extern "c" fn uv_fs_realpath( 600 uv :
type Loop Bytes
Loop
,
601 req :
type Fs
Fs
,
602 path :
Bytes
Bytes
,
603 cb : (
type Fs
Fs
) ->
Unit
Unit
604) ->
Int
Int
= "moonbit_uv_fs_realpath"
605 606///| 607type Stat
Bytes
Bytes
608 609///| 610extern "c" fn moonbit_uv_stat_get_dev(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_dev"
611 612///| 613extern "c" fn moonbit_uv_stat_get_mode(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_mode"
614 615///| 616extern "c" fn moonbit_uv_stat_get_nlink(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_nlink"
617 618///| 619extern "c" fn moonbit_uv_stat_get_uid(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_uid"
620 621///| 622extern "c" fn moonbit_uv_stat_get_gid(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_gid"
623 624///| 625extern "c" fn moonbit_uv_stat_get_rdev(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_rdev"
626 627///| 628extern "c" fn moonbit_uv_stat_get_ino(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_ino"
629 630///| 631extern "c" fn moonbit_uv_stat_get_size(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_size"
632 633///| 634extern "c" fn moonbit_uv_stat_get_blksize(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_blksize"
635 636///| 637extern "c" fn moonbit_uv_stat_get_blocks(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_blocks"
638 639///| 640extern "c" fn moonbit_uv_stat_get_flags(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_flags"
641 642///| 643extern "c" fn moonbit_uv_stat_get_gen(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_gen"
644 645///| 646extern "c" fn moonbit_uv_stat_get_atim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_atim_sec"
647 648///| 649extern "c" fn moonbit_uv_stat_get_atim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_atim_nsec"
650 651///| 652extern "c" fn moonbit_uv_stat_get_mtim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_mtim_sec"
653 654///| 655extern "c" fn moonbit_uv_stat_get_mtim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_mtim_nsec"
656 657///| 658extern "c" fn moonbit_uv_stat_get_ctim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_ctim_sec"
659 660///| 661extern "c" fn moonbit_uv_stat_get_ctim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_ctim_nsec"
662 663///| 664extern "c" fn moonbit_uv_stat_get_birthtim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_birthtim_sec"
665 666///| 667extern "c" fn moonbit_uv_stat_get_birthtim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_birthtim_nsec"
668 669///| 670pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
dev
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
671
(stat : Stat) -> UInt64
moonbit_uv_stat_get_dev
(
Stat
self
)
672} 673 674///| 675pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
mode
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
676
(stat : Stat) -> UInt64
moonbit_uv_stat_get_mode
(
Stat
self
)
677} 678 679///| 680pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
nlink
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
681
(stat : Stat) -> UInt64
moonbit_uv_stat_get_nlink
(
Stat
self
)
682} 683 684///| 685pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
uid
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
686
(stat : Stat) -> UInt64
moonbit_uv_stat_get_uid
(
Stat
self
)
687} 688 689///| 690pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
gid
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
691
(stat : Stat) -> UInt64
moonbit_uv_stat_get_gid
(
Stat
self
)
692} 693 694///| 695pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
rdev
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
696
(stat : Stat) -> UInt64
moonbit_uv_stat_get_rdev
(
Stat
self
)
697} 698 699///| 700pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
ino
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
701
(stat : Stat) -> UInt64
moonbit_uv_stat_get_ino
(
Stat
self
)
702} 703 704///| 705pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
size
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
706
(stat : Stat) -> UInt64
moonbit_uv_stat_get_size
(
Stat
self
)
707} 708 709///| 710pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
blksize
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
711
(stat : Stat) -> UInt64
moonbit_uv_stat_get_blksize
(
Stat
self
)
712} 713 714///| 715pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
blocks
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
716
(stat : Stat) -> UInt64
moonbit_uv_stat_get_blocks
(
Stat
self
)
717} 718 719///| 720pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
flags
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
721
(stat : Stat) -> UInt64
moonbit_uv_stat_get_flags
(
Stat
self
)
722} 723 724///| 725pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
gen
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
726
(stat : Stat) -> UInt64
moonbit_uv_stat_get_gen
(
Stat
self
)
727} 728 729///| 730pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
atim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
731
(stat : Stat) -> Int64
moonbit_uv_stat_get_atim_sec
(
Stat
self
)
732} 733 734///| 735pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
atim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
736
(stat : Stat) -> Int64
moonbit_uv_stat_get_atim_nsec
(
Stat
self
)
737} 738 739///| 740pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
mtim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
741
(stat : Stat) -> Int64
moonbit_uv_stat_get_mtim_sec
(
Stat
self
)
742} 743 744///| 745pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
mtim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
746
(stat : Stat) -> Int64
moonbit_uv_stat_get_mtim_nsec
(
Stat
self
)
747} 748 749///| 750pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
ctim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
751
(stat : Stat) -> Int64
moonbit_uv_stat_get_ctim_sec
(
Stat
self
)
752} 753 754///| 755pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
ctim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
756
(stat : Stat) -> Int64
moonbit_uv_stat_get_ctim_nsec
(
Stat
self
)
757} 758 759///| 760pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
birthtim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
761
(stat : Stat) -> Int64
moonbit_uv_stat_get_birthtim_sec
(
Stat
self
)
762} 763 764///| 765pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
birthtim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
766
(stat : Stat) -> Int64
moonbit_uv_stat_get_birthtim_nsec
(
Stat
self
)
767} 768 769///| 770pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_file
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
771 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFREG
772} 773 774///| 775pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_directory
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
776 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFDIR
777} 778 779///| 780pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_symlink
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
781 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFLNK
782} 783 784///| 785pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool!Errno
is_socket
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
!
type! Errno Status
Errno
{
786 if
UInt64
_S_IFSOCK
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
@uint64.max_value
{
787 raise
(Status) -> Errno
Errno
(
Status
ENOSYS
)
788 } 789 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFSOCK
790} 791 792///| 793pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_pipe
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
794 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFIFO
795} 796 797///| 798pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool!Errno
is_block_device
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
!
type! Errno Status
Errno
{
799 if
UInt64
_S_IFBLK
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
@uint64.max_value
{
800 raise
(Status) -> Errno
Errno
(
Status
ENOSYS
)
801 } 802 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFBLK
803} 804 805///| 806pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_character_device
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
807 (
Stat
self
.
(self : Stat) -> UInt64
mode
()
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

test "UInt64::land" {
  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
}
&
UInt64
_S_IFMT
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

test "UInt64::op_equal" {
  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
}
==
UInt64
_S_IFCHR
808} 809 810///| 811extern "c" fn uv_fs_S_IFMT() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFMT"
812 813///| 814let
UInt64
_S_IFMT
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFMT
()
815 816///| 817extern "c" fn uv_fs_S_IFREG() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFREG"
818 819///| 820let
UInt64
_S_IFREG
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFREG
()
821 822///| 823extern "c" fn uv_fs_S_IFDIR() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFDIR"
824 825///| 826let
UInt64
_S_IFDIR
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFDIR
()
827 828///| 829extern "c" fn uv_fs_S_IFLNK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFLNK"
830 831///| 832let
UInt64
_S_IFLNK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFLNK
()
833 834///| 835extern "c" fn uv_fs_S_IFSOCK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFSOCK"
836 837///| 838let
UInt64
_S_IFSOCK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFSOCK
()
839 840///| 841extern "c" fn uv_fs_S_IFIFO() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFIFO"
842 843///| 844let
UInt64
_S_IFIFO
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFIFO
()
845 846///| 847extern "c" fn uv_fs_S_IFBLK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFBLK"
848 849///| 850let
UInt64
_S_IFBLK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFBLK
()
851 852///| 853extern "c" fn uv_fs_S_IFCHR() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFCHR"
854 855///| 856let
UInt64
_S_IFCHR
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFCHR
()
857 858///| 859extern "c" fn uv_fs_get_statbuf(req :
type Fs
Fs
) ->
type Stat Bytes
Stat
= "moonbit_uv_fs_get_statbuf"
860 861///| 862extern "c" fn uv_fs_stat( 863 uv :
type Loop Bytes
Loop
,
864 req :
type Fs
Fs
,
865 path :
Bytes
Bytes
,
866 cb : (
type Fs
Fs
) ->
Unit
Unit
867) ->
Int
Int
= "moonbit_uv_fs_stat"
868 869///| 870pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, k : (Fs, Stat) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_stat
(
871
Loop
self
:
type Loop Bytes
Loop
,
872
Ntbs
path
:

type parameter Ntbs

Ntbs
,
873
(Fs, Stat) -> Unit
k
: (
type Fs
Fs
,
type Stat Bytes
Stat
) ->
Unit
Unit
,
874
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
875) ->
Unit
Unit
!
type! Errno Status
Errno
{
876 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
877 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
878 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
879
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
880 } else { 881 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
882
(Fs, Stat) -> Unit
k
(
Fs
req
,
Stat
statbuf
)
883 } 884 } 885 886 let
Fs
req
=
() -> Fs
uv_fs_make
()
887 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_stat
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
888 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
889 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
890 } 891} 892 893///| 894extern "c" fn uv_fs_lstat( 895 uv :
type Loop Bytes
Loop
,
896 req :
type Fs
Fs
,
897 path :
Bytes
Bytes
,
898 cb : (
type Fs
Fs
) ->
Unit
Unit
899) ->
Int
Int
= "moonbit_uv_fs_lstat"
900 901///| 902pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, k : (Fs, Stat) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_lstat
(
903
Loop
self
:
type Loop Bytes
Loop
,
904
Ntbs
path
:

type parameter Ntbs

Ntbs
,
905
(Fs, Stat) -> Unit
k
: (
type Fs
Fs
,
type Stat Bytes
Stat
) ->
Unit
Unit
,
906
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
907) ->
Unit
Unit
!
type! Errno Status
Errno
{
908 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
909 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
910 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
911
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
912 } else { 913 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
914
(Fs, Stat) -> Unit
k
(
Fs
req
,
Stat
statbuf
)
915 } 916 } 917 918 let
Fs
req
=
() -> Fs
uv_fs_make
()
919 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_lstat
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
920 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
921 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
922 } 923} 924 925///| 926extern "c" fn uv_fs_fstat( 927 uv :
type Loop Bytes
Loop
,
928 req :
type Fs
Fs
,
929 file :
type File Int
File
,
930 cb : (
type Fs
Fs
) ->
Unit
Unit
931) ->
Int
Int
= "moonbit_uv_fs_fstat"
932 933///| 934pub fn
(self : Loop, file : File, k : (Fs, Stat) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_fstat
(
935
Loop
self
:
type Loop Bytes
Loop
,
936
File
file
:
type File Int
File
,
937
(Fs, Stat) -> Unit
k
: (
type Fs
Fs
,
type Stat Bytes
Stat
) ->
Unit
Unit
,
938
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
939) ->
Unit
Unit
!
type! Errno Status
Errno
{
940 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
941 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
942 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
943
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
944 } else { 945 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
946
(Fs, Stat) -> Unit
k
(
Fs
req
,
Stat
statbuf
)
947 } 948 } 949 950 let
Fs
req
=
() -> Fs
uv_fs_make
()
951 let
Int
status
=
(uv : Loop, req : Fs, file : File, cb : (Fs) -> Unit) -> Int
uv_fs_fstat
(
Loop
self
,
Fs
req
,
File
file
,
(Fs) -> Unit
cb
)
952 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
953 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
954 } 955} 956 957///| 958pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, k : (Fs, String) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_realpath
(
959
Loop
self
:
type Loop Bytes
Loop
,
960
Ntbs
path
:

type parameter Ntbs

Ntbs
,
961
(Fs, String) -> Unit
k
: (
type Fs
Fs
,
String
String
) ->
Unit
Unit
,
962
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
963) ->
Unit
Unit
!
type! Errno Status
Errno
{
964 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
965 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
966 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
967
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
968 } else { 969 let
Unit
ptr
:
Unit
@c.Pointer
[
Byte
Byte
] =
(req : Fs) -> ?
uv_fs_get_ptr
(
Fs
req
).
() -> Unit
cast
()
970 let
@buffer.T
buffer
=
(size_hint~ : Int = ..) -> @buffer.T

Creates a new extensible buffer with specified initial capacity. If the initial capacity is less than 1, the buffer will be initialized with capacity 1.

Parameters:

  • size_hint : Initial capacity of the buffer in bytes. Defaults to 0.

Returns a new buffer of type T.

Example:

test "new" {
  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
}
@buffer.new
()
971 for
Int
i
= 0;
Unit
ptr
(Int) -> Int
[
i]
(x : Int, y : Int) -> Bool
!=
0;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

test "Int::op_add" {
  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
}
+
1 {
972
@buffer.T
buffer
.
(self : @buffer.T, value : Byte) -> Unit

Writes a single byte to the end of the buffer. The buffer will automatically grow if necessary to accommodate the new byte.

Parameters:

  • buffer : The buffer to write to.
  • byte : The byte value to be written.

Example:

test "write_byte" {
  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
}
write_byte
(
Unit
ptr
(Int) -> Byte
[
i])
973 } 974 let
Bytes
bytes
=
@buffer.T
buffer
.
(self : @buffer.T) -> Bytes

Returns a copy of the buffer's content as a sequence of bytes.

Parameters:

  • buffer : The buffer to read from.

Returns a Bytes object containing all bytes written to the buffer.

Example:

test "contents" {
  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
}
contents
()
975
(Fs, String) -> Unit
k
(
Fs
req
,
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
))
976 } 977 } 978 979 let
Fs
req
=
() -> Fs
uv_fs_make
()
980 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_realpath
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
981 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
982 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
983 } 984} 985 986///| 987extern "c" fn uv_F_OK() ->
Int
Int
= "moonbit_uv_F_OK"
988 989///| 990let
Int
_F_OK
:
Int
Int
=
() -> Int
uv_F_OK
()
991 992///| 993extern "c" fn uv_R_OK() ->
Int
Int
= "moonbit_uv_R_OK"
994 995///| 996let
Int
_R_OK
:
Int
Int
=
() -> Int
uv_R_OK
()
997 998///| 999extern "c" fn uv_W_OK() ->
Int
Int
= "moonbit_uv_W_OK"
1000 1001///| 1002let
Int
_W_OK
:
Int
Int
=
() -> Int
uv_W_OK
()
1003 1004///| 1005extern "c" fn uv_X_OK() ->
Int
Int
= "moonbit_uv_X_OK"
1006 1007///| 1008let
Int
_X_OK
:
Int
Int
=
() -> Int
uv_X_OK
()
1009 1010///| 1011type AccessFlags
Int
Int
1012 1013///| 1014pub fn
type AccessFlags Int
AccessFlags
::
(read~ : Bool = .., write~ : Bool = .., execute~ : Bool = ..) -> AccessFlags
new
(
1015
Bool
read
~ :
Bool
Bool
= false,
1016
Bool
write
~ :
Bool
Bool
= false,
1017
Bool
execute
~ :
Bool
Bool
= false
1018) ->
type AccessFlags Int
AccessFlags
{
1019 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

test "not" {
  inspect(not(true), content="false")
  inspect(not(false), content="true")
}
not
(
Bool
read
)
(Bool, Bool) -> Bool
&&
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

test "not" {
  inspect(not(true), content="false")
  inspect(not(false), content="true")
}
not
(
Bool
write
)
(Bool, Bool) -> Bool
&&
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

test "not" {
  inspect(not(true), content="false")
  inspect(not(false), content="true")
}
not
(
Bool
execute
) {
1020 return
Int
_F_OK
1021 } 1022 let mut
Int
flags
= 0
1023 if
Bool
read
{
1024
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_R_OK
1025 } 1026 if
Bool
write
{
1027
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_W_OK
1028 } 1029 if
Bool
execute
{
1030
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_X_OK
1031 } 1032 return
Int
flags
1033} 1034 1035///| 1036extern "c" fn uv_fs_access( 1037 uv :
type Loop Bytes
Loop
,
1038 req :
type Fs
Fs
,
1039 path :
Bytes
Bytes
,
1040 mode :
Int
Int
,
1041 cb : (
type Fs
Fs
) ->
Unit
Unit
1042) ->
Int
Int
= "moonbit_uv_fs_access"
1043 1044///| 1045pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}
ToNtbs
]
(self : Loop, path : Ntbs, mode : AccessFlags, k : (Fs) -> Unit, e : (Fs, Errno) -> Unit) -> Unit!Errno
fs_access
(
1046
Loop
self
:
type Loop Bytes
Loop
,
1047
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1048
AccessFlags
mode
:
type AccessFlags Int
AccessFlags
,
1049
(Fs) -> Unit
k
: (
type Fs
Fs
) ->
Unit
Unit
,
1050
(Fs, Errno) -> Unit
e
: (
type Fs
Fs
,
type! Errno Status
Errno
) ->
Unit
Unit
1051) ->
Unit
Unit
!
type! Errno Status
Errno
{
1052 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1053 let
Int
status
=
Fs
req
.
(self : Fs) -> Int64
result
().
(self : Int64) -> Int

Converts a 64-bit signed integer to a 32-bit signed integer by truncating higher bits.

Parameters:

  • value : The 64-bit signed integer (Int64) to be converted.

Returns a 32-bit signed integer (Int). Note that values outside the range of 32-bit integers will be truncated, potentially leading to loss of information.

Example:

test "Int64::to_int" {
  let small = 42L
  let big = 2147483648L // 2^31
  inspect(small.to_int(), content="42")
  inspect(big.to_int(), content="-2147483648") // Truncated to Int.min_value
}
to_int
()
1054 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1055
(Fs, Errno) -> Unit
e
(
Fs
req
,
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1056 } else { 1057
(Fs) -> Unit
k
(
Fs
req
)
1058 } 1059 } 1060 1061 let
Fs
req
=
() -> Fs
uv_fs_make
()
1062 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, mode : Int, cb : (Fs) -> Unit) -> Int
uv_fs_access
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
AccessFlags
mode
.
Int
_
,
(Fs) -> Unit
cb
)
1063 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1064 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1065 } 1066} 1067