1// Copyright 2025 International Digital Economy Academy
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15///|
16type Fs
17
18///|
19pub impl 
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Fs
Fs
with to_req(self :
type Fs
Fs
) ->
type Req
Req
= "%identity"
20 21///| 22extern "c" fn uv_fs_make() ->
type Fs
Fs
= "moonbit_uv_fs_make"
23 24///| 25extern "c" fn uv_fs_req_cleanup(req :
type Fs
Fs
) = "moonbit_uv_fs_req_cleanup"
26 27///| 28type OpenFlags
Int
Int
29 30///| 31/// Creates file open flags for read-only access. 32/// 33/// Returns an `OpenFlags` instance configured for read-only file access. Files 34/// opened with these flags can only be read from, not written to or modified. 35/// 36/// Example: 37/// 38/// ```moonbit 39/// let uv = @uv.Loop::new() 40/// let flags = @uv.OpenFlags::read_only() 41/// let file = uv.fs_open_sync("README.md", flags, 0o644) 42/// uv.fs_close_sync(file) 43/// uv.close() 44/// ``` 45pub fn
type OpenFlags Int
OpenFlags
::
() -> OpenFlags

Creates file open flags for read-only access.

Returns an OpenFlags instance configured for read-only file access. Files opened with these flags can only be read from, not written to or modified.

Example:

let uv = @uv.Loop::new()
let flags = @uv.OpenFlags::read_only()
let file = uv.fs_open_sync("README.md", flags, 0o644)
uv.fs_close_sync(file)
uv.close()
read_only
() ->
type OpenFlags Int
OpenFlags
{
46
Int
_O_RDONLY
47} 48 49///| 50/// Creates file open flags for write-only access with configurable behaviors. 51/// 52/// Parameters: 53/// 54/// * `append` : Whether to append to the file instead of overwriting. When 55/// `true`, writes will be positioned at the end of the file. Defaults to 56/// `false`. 57/// * `create` : Whether to create the file if it doesn't exist. When `true`, a 58/// new file will be created if the specified path doesn't exist. Defaults to 59/// `false`. 60/// * `truncate` : Whether to truncate the file to zero length when opening. 61/// When `true`, any existing content will be discarded. Defaults to `false`. 62/// * `exclusive` : Whether to fail if the file already exists when creating. 63/// When `true` and `create` is also `true`, the operation will fail if the 64/// file already exists. Defaults to `false`. 65/// 66/// Returns an `OpenFlags` instance configured for write-only access with the 67/// specified behaviors. 68/// 69/// Example: 70/// 71/// ```moonbit 72/// let uv = @uv.Loop::new() 73/// // Create a new file for writing, truncate if it exists 74/// let flags = @uv.OpenFlags::write_only(create=true, truncate=true) 75/// let path = "output.txt" 76/// let file = uv.fs_open_sync(path, flags, 0o644) 77/// uv.fs_close_sync(file) 78/// uv.fs_unlink_sync(path) 79/// uv.close() 80/// ``` 81pub fn
type OpenFlags Int
OpenFlags
::
(append~ : Bool = .., create~ : Bool = .., truncate~ : Bool = .., exclusive~ : Bool = ..) -> OpenFlags

Creates file open flags for write-only access with configurable behaviors.

Parameters:

  • append : Whether to append to the file instead of overwriting. When true, writes will be positioned at the end of the file. Defaults to false.
  • create : Whether to create the file if it doesn't exist. When true, a new file will be created if the specified path doesn't exist. Defaults to false.
  • truncate : Whether to truncate the file to zero length when opening. When true, any existing content will be discarded. Defaults to false.
  • exclusive : Whether to fail if the file already exists when creating. When true and create is also true, the operation will fail if the file already exists. Defaults to false.

Returns an OpenFlags instance configured for write-only access with the specified behaviors.

Example:

let uv = @uv.Loop::new()
// Create a new file for writing, truncate if it exists
let flags = @uv.OpenFlags::write_only(create=true, truncate=true)
let path = "output.txt"
let file = uv.fs_open_sync(path, flags, 0o644)
uv.fs_close_sync(file)
uv.fs_unlink_sync(path)
uv.close()
write_only
(
82
Bool
append
~ :
Bool
Bool
= false,
83
Bool
create
~ :
Bool
Bool
= false,
84
Bool
truncate
~ :
Bool
Bool
= false,
85
Bool
exclusive
~ :
Bool
Bool
= false
86) ->
type OpenFlags Int
OpenFlags
{
87 let mut
Int
flags
=
Int
_O_WRONLY
88 if
Bool
append
{
89
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_APPEND
90 } 91 if
Bool
create
{
92
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_CREAT
93 } 94 if
Bool
truncate
{
95
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_TRUNC
96 } 97 if
Bool
exclusive
{
98
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_EXCL
99 } 100
Int
flags
101} 102 103///| 104/// Creates file open flags for read-write access with configurable behaviors. 105/// 106/// Parameters: 107/// 108/// * `append` : Whether to append to the file instead of overwriting. When 109/// `true`, writes will be positioned at the end of the file. Defaults to 110/// `false`. 111/// * `create` : Whether to create the file if it doesn't exist. When `true`, a 112/// new file will be created if the specified path doesn't exist. Defaults to 113/// `false`. 114/// * `truncate` : Whether to truncate the file to zero length when opening. When 115/// `true`, any existing content will be discarded. Defaults to `false`. 116/// * `exclusive` : Whether to fail if the file already exists when creating. 117/// When `true` and `create` is also `true`, the operation will fail if the 118/// file already exists. Defaults to `false`. 119/// 120/// Returns an `OpenFlags` instance configured for read-write access with the 121/// specified behaviors. 122/// 123/// Example: 124/// 125/// ```moonbit 126/// let uv = @uv.Loop::new() 127/// // Create a new file for reading and writing, fail if it exists 128/// let flags = @uv.OpenFlags::read_write(create=true, exclusive=true) 129/// let path = "new_file.txt" 130/// let file = uv.fs_open_sync(path, flags, 0o644) 131/// uv.fs_close_sync(file) 132/// uv.fs_unlink_sync(path) 133/// uv.close() 134/// ``` 135pub fn
type OpenFlags Int
OpenFlags
::
(append~ : Bool = .., create~ : Bool = .., truncate~ : Bool = .., exclusive~ : Bool = ..) -> OpenFlags

Creates file open flags for read-write access with configurable behaviors.

Parameters:

  • append : Whether to append to the file instead of overwriting. When true, writes will be positioned at the end of the file. Defaults to false.
  • create : Whether to create the file if it doesn't exist. When true, a new file will be created if the specified path doesn't exist. Defaults to false.
  • truncate : Whether to truncate the file to zero length when opening. When true, any existing content will be discarded. Defaults to false.
  • exclusive : Whether to fail if the file already exists when creating. When true and create is also true, the operation will fail if the file already exists. Defaults to false.

Returns an OpenFlags instance configured for read-write access with the specified behaviors.

Example:

let uv = @uv.Loop::new()
// Create a new file for reading and writing, fail if it exists
let flags = @uv.OpenFlags::read_write(create=true, exclusive=true)
let path = "new_file.txt"
let file = uv.fs_open_sync(path, flags, 0o644)
uv.fs_close_sync(file)
uv.fs_unlink_sync(path)
uv.close()
read_write
(
136
Bool
append
~ :
Bool
Bool
= false,
137
Bool
create
~ :
Bool
Bool
= false,
138
Bool
truncate
~ :
Bool
Bool
= false,
139
Bool
exclusive
~ :
Bool
Bool
= false
140) ->
type OpenFlags Int
OpenFlags
{
141 let mut
Int
flags
=
Int
_O_RDWR
142 if
Bool
append
{
143
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_APPEND
144 } 145 if
Bool
create
{
146
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_CREAT
147 } 148 if
Bool
truncate
{
149
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_TRUNC
150 } 151 if
Bool
exclusive
{
152
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_O_EXCL
153 } 154
Int
flags
155} 156 157///| 158extern "c" fn uv_fs_O_RDONLY() ->
Int
Int
= "moonbit_uv_fs_O_RDONLY"
159 160///| 161let
Int
_O_RDONLY
:
Int
Int
=
() -> Int
uv_fs_O_RDONLY
()
162 163///| 164extern "c" fn uv_fs_O_WRONLY() ->
Int
Int
= "moonbit_uv_fs_O_WRONLY"
165 166///| 167let
Int
_O_WRONLY
:
Int
Int
=
() -> Int
uv_fs_O_WRONLY
()
168 169///| 170extern "c" fn uv_fs_O_RDWR() ->
Int
Int
= "moonbit_uv_fs_O_RDWR"
171 172///| 173let
Int
_O_RDWR
:
Int
Int
=
() -> Int
uv_fs_O_RDWR
()
174 175///| 176extern "c" fn uv_fs_O_CREAT() ->
Int
Int
= "moonbit_uv_fs_O_CREAT"
177 178///| 179let
Int
_O_CREAT
:
Int
Int
=
() -> Int
uv_fs_O_CREAT
()
180 181///| 182extern "c" fn uv_fs_O_TRUNC() ->
Int
Int
= "moonbit_uv_fs_O_TRUNC"
183 184///| 185let
Int
_O_TRUNC
:
Int
Int
=
() -> Int
uv_fs_O_TRUNC
()
186 187///| 188extern "c" fn uv_fs_O_APPEND() ->
Int
Int
= "moonbit_uv_fs_O_APPEND"
189 190///| 191let
Int
_O_APPEND
:
Int
Int
=
() -> Int
uv_fs_O_APPEND
()
192 193///| 194extern "c" fn uv_fs_O_EXCL() ->
Int
Int
= "moonbit_uv_fs_O_EXCL"
195 196///| 197let
Int
_O_EXCL
:
Int
Int
=
() -> Int
uv_fs_O_EXCL
()
198 199///| 200extern "c" fn uv_fs_open( 201 uv :
type Loop Bytes
Loop
,
202 req :
type Fs
Fs
,
203 path :
Bytes
Bytes
,
204 flags :
Int
Int
,
205 mode :
Int
Int
,
206 cb : (
type Fs
Fs
) ->
Unit
Unit
207) ->
Int
Int
= "moonbit_uv_fs_open"
208 209///| 210/// Convertible to null-terminated byte strings (NTBS). 211trait
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
{
212
(Self) -> Bytes
to_ntbs
(

type parameter Self

Self
) ->
Bytes
Bytes
213} 214 215///| 216pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
for
String
String
with
(self : String) -> Bytes
to_ntbs
(
String
self
:
String
String
) ->
Bytes
Bytes
{
217 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:

  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
@buffer.new
()
218
(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:

let buf = @buffer.new()
let text = "Hello, world"
@encoding.encode_to(text, buf, encoding=UTF16LE)
inspect(buf.to_string(), content="Hello, world")
@encoding.encode_to
(
String
self
,
@buffer.T
buffer
,
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
)
219
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(0)
220 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
221} 222 223///| 224pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
for
Bytes
Bytes
with
(self : Bytes) -> Bytes
to_ntbs
(
Bytes
self
:
Bytes
Bytes
) ->
Bytes
Bytes
{
225 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:

  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
@buffer.new
()
226
@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:

  let buf = @buffer.new()
  buf.write_bytes(b"Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x65\x73\x74"
    ,
  )
write_bytes
(
Bytes
self
)
227
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(0)
228 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
229} 230 231///| 232pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

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
{
233
@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.charcodes(start = str.offset_of_nth_char(0).unwrap(),end   = str.offset_of_nth_char(5).unwrap()) // "Hello"
  inspect(view.to_string(), content="Hello")
to_string
() |>
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
(self : String) -> Bytes
::to_ntbs
()
234} 235 236///| 237pub impl
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

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
{
238 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:

  let buf = @buffer.new(size_hint=10)
  inspect(buf.length(), content="0")
  buf.write_string("test")
  inspect(buf.length(), content="8")
@buffer.new
()
239
@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:

  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
)
240
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(0)
241 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
242} 243 244///| 245/// Asynchronously opens a file at the specified path with the given flags and 246/// mode. 247/// 248/// This function initiates a file open operation that will be handled 249/// asynchronously by the event loop. When the operation completes, either the 250/// success callback or error callback will be invoked depending on the result. 251/// 252/// Parameters: 253/// 254/// * `self` : The event loop instance to schedule the operation on. 255/// * `path` : The file path to open. Can be any type that implements `ToNtbs` 256/// (convertible to null-terminated byte string), such as `String`, `Bytes`, 257/// `@string.View`, or `@bytes.View`. 258/// * `flags` : The file access mode and behavior flags. Use 259/// `OpenFlags::read_only()`, `OpenFlags::write_only()`, or 260/// `OpenFlags::read_write()` with optional parameters for additional 261/// behaviors like append, create, truncate, or exclusive access. 262/// * `mode` : The file permissions to use when creating a new file (octal 263/// notation, e.g., `0o644`). This parameter is ignored if the file already 264/// exists. 265/// * `open_cb` : Success callback function that receives the filesystem request 266/// handle and the opened file descriptor when the operation succeeds. 267/// * `error_cb` : Error callback function that receives the filesystem request 268/// handle and the error code when the operation fails. 269/// 270/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 271/// invalid parameters or system resource exhaustion). 272/// 273/// Example: 274/// 275/// ```moonbit 276/// let uv = Loop::new() 277/// uv.fs_open( 278/// "README.md", 279/// OpenFlags::read_only(), 280/// 0o644, 281/// (file) => uv.fs_close_sync(file) catch { _ => () }, 282/// (error) => println("Failed to open file: \{error}"), 283/// ) 284/// |> ignore() 285/// uv.run(Default) 286/// uv.close() 287/// ``` 288pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, flags : OpenFlags, mode : Int, open_cb : (File) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously opens a file at the specified path with the given flags and mode.

This function initiates a file open operation that will be handled asynchronously by the event loop. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • path : The file path to open. Can be any type that implements ToNtbs (convertible to null-terminated byte string), such as String, Bytes, @string.View, or @bytes.View.
  • flags : The file access mode and behavior flags. Use OpenFlags::read_only(), OpenFlags::write_only(), or OpenFlags::read_write() with optional parameters for additional behaviors like append, create, truncate, or exclusive access.
  • mode : The file permissions to use when creating a new file (octal notation, e.g., 0o644). This parameter is ignored if the file already exists.
  • open_cb : Success callback function that receives the filesystem request handle and the opened file descriptor when the operation succeeds.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid parameters or system resource exhaustion).

Example:

let uv = Loop::new()
uv.fs_open(
  "README.md",
  OpenFlags::read_only(),
  0o644,
  (file) => uv.fs_close_sync(file) catch { _ => () },
  (error) => println("Failed to open file: \{error}"),
)
|> ignore()
uv.run(Default)
uv.close()
fs_open
(
289
Loop
self
:
type Loop Bytes
Loop
,
290
Ntbs
path
:

type parameter Ntbs

Ntbs
,
291
OpenFlags
flags
:
type OpenFlags Int
OpenFlags
,
292
Int
mode
:
Int
Int
,
293
(File) -> Unit
open_cb
: (
type File Int
File
) ->
Unit
Unit
,
294
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
295) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
296 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
297 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:

  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
()
298
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
299 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
300
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
))
301 } else { 302
(File) -> Unit
open_cb
(
(Int) -> File
File
(
Int
result
))
303 } 304 } 305 306 let
Fs
req
=
() -> Fs
uv_fs_make
()
307 let
Bytes
path
=
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
()
308 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
.
(self : OpenFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
(),
Int
mode
,
(Fs) -> Unit
cb
)
309 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
310 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
311 } 312 return
Fs
req
313} 314 315///| 316extern "c" fn uv_fs_open_sync( 317 uv :
type Loop Bytes
Loop
,
318 req :
type Fs
Fs
,
319 path :
Bytes
Bytes
,
320 flags :
Int
Int
,
321 mode :
Int
Int
322) ->
Int
Int
= "moonbit_uv_fs_open_sync"
323 324///| 325/// Synchronously opens a file at the specified path with the given flags and 326/// mode. 327/// 328/// This function blocks the current thread until the file open operation 329/// completes. Unlike the asynchronous `fs_open` function, this operation does 330/// not require callbacks and returns the file descriptor immediately upon 331/// successful completion. 332/// 333/// Parameters: 334/// 335/// * `self` : The event loop instance to perform the operation on. 336/// * `path` : The file path to open. Can be any type that implements `ToNtbs` 337/// (convertible to null-terminated byte string), such as `String`, `Bytes`, 338/// `@string.View`, or `@bytes.View`. 339/// * `flags` : The file access mode and behavior flags. Use 340/// `OpenFlags::read_only()`, `OpenFlags::write_only()`, or 341/// `OpenFlags::read_write()` with optional parameters for additional behaviors 342/// like append, create, truncate, or exclusive access. 343/// * `mode` : The file permissions to use when creating a new file (octal 344/// notation, e.g., `0o644`). This parameter is ignored if the file already 345/// exists. 346/// 347/// Returns a `File` handle that can be used for subsequent file operations like 348/// reading, writing, or closing. 349/// 350/// Throws an error of type `Errno` if the file cannot be opened (e.g., file does 351/// not exist, insufficient permissions, invalid path, or system resource 352/// exhaustion). 353/// 354/// Example: 355/// 356/// ```moonbit 357/// let uv = @uv.Loop::new() 358/// let file = uv.fs_open_sync("README.md", @uv.OpenFlags::read_only(), 0o644) 359/// uv.fs_close_sync(file) 360/// uv.close() 361/// ``` 362pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, flags : OpenFlags, mode : Int) -> File raise Errno

Synchronously opens a file at the specified path with the given flags and mode.

This function blocks the current thread until the file open operation completes. Unlike the asynchronous fs_open function, this operation does not require callbacks and returns the file descriptor immediately upon successful completion.

Parameters:

  • self : The event loop instance to perform the operation on.
  • path : The file path to open. Can be any type that implements ToNtbs (convertible to null-terminated byte string), such as String, Bytes, @string.View, or @bytes.View.
  • flags : The file access mode and behavior flags. Use OpenFlags::read_only(), OpenFlags::write_only(), or OpenFlags::read_write() with optional parameters for additional behaviors like append, create, truncate, or exclusive access.
  • mode : The file permissions to use when creating a new file (octal notation, e.g., 0o644). This parameter is ignored if the file already exists.

Returns a File handle that can be used for subsequent file operations like reading, writing, or closing.

Throws an error of type Errno if the file cannot be opened (e.g., file does not exist, insufficient permissions, invalid path, or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
let file = uv.fs_open_sync("README.md", @uv.OpenFlags::read_only(), 0o644)
uv.fs_close_sync(file)
uv.close()
fs_open_sync
(
363
Loop
self
:
type Loop Bytes
Loop
,
364
Ntbs
path
:

type parameter Ntbs

Ntbs
,
365
OpenFlags
flags
:
type OpenFlags Int
OpenFlags
,
366
Int
mode
:
Int
Int
367) ->
type File Int
File
raise
type! Errno Status
Errno
{
368 let
Fs
req
=
() -> Fs
uv_fs_make
()
369 let
Bytes
path
=
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
()
370 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, flags : Int, mode : Int) -> Int
uv_fs_open_sync
(
Loop
self
,
Fs
req
,
Bytes
path
,
OpenFlags
flags
.
(self : OpenFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
(),
Int
mode
)
371 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
372 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
373 } 374
(Int) -> File
File
(
Int
status
)
375} 376 377///| 378extern "c" fn uv_fs_close( 379 uv :
type Loop Bytes
Loop
,
380 req :
type Fs
Fs
,
381 file :
type File Int
File
,
382 cb : (
type Fs
Fs
) ->
Unit
Unit
383) ->
Int
Int
= "moonbit_uv_fs_close"
384 385///| 386/// Asynchronously closes a file descriptor. 387/// 388/// This function initiates a file close operation that will be handled 389/// asynchronously by the event loop. When the operation completes, either the 390/// success callback or error callback will be invoked depending on the result. 391/// 392/// Parameters: 393/// 394/// * `self` : The event loop instance to schedule the operation on. 395/// * `file` : The file descriptor to close, typically obtained from `fs_open` or 396/// `fs_open_sync`. 397/// * `close_cb` : Success callback function that receives the filesystem request 398/// handle when the file is closed successfully. 399/// * `error_cb` : Error callback function that receives the filesystem request 400/// handle and the error code when the operation fails. 401/// 402/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 403/// invalid file descriptor or system resource exhaustion). 404/// 405/// Example: 406/// 407/// ```moonbit 408/// let uv = Loop::new() 409/// let file = uv.fs_open_sync("README.md", OpenFlags::read_only(), 0o644) 410/// uv.fs_close( 411/// file, 412/// fn() { println("File closed successfully") }, 413/// fn(error) { println("Failed to close file: \{error}") } 414/// ) 415/// |> ignore() 416/// uv.run(Default) 417/// uv.close() 418/// ``` 419pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, close_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously closes a file descriptor.

This function initiates a file close operation that will be handled asynchronously by the event loop. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • file : The file descriptor to close, typically obtained from fs_open or fs_open_sync.
  • close_cb : Success callback function that receives the filesystem request handle when the file is closed successfully.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid file descriptor or system resource exhaustion).

Example:

let uv = Loop::new()
let file = uv.fs_open_sync("README.md", OpenFlags::read_only(), 0o644)
uv.fs_close(
  file,
  fn() { println("File closed successfully") },
  fn(error) { println("Failed to close file: \{error}") }
)
|> ignore()
uv.run(Default)
uv.close()
fs_close
(
420
Loop
self
:
type Loop Bytes
Loop
,
421
File
file
:
type File Int
File
,
422
() -> Unit
close_cb
: () ->
Unit
Unit
,
423
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
424) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
425 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
426 let
Int64
result
=
Fs
req
.
(self : Fs) -> Int64
result
()
427
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
428 if
Int64
result
(self_ : Int64, other : Int64) -> Bool
<
0 {
429
(Errno) -> Unit
error_cb
(
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:

  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
()))
430 } else { 431
() -> Unit
close_cb
()
432 } 433 } 434 435 let
Fs
req
=
() -> Fs
uv_fs_make
()
436 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
)
437 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
438 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
439 } 440 return
Fs
req
441} 442 443///| 444extern "c" fn uv_fs_close_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, file :
type File Int
File
) ->
Int
Int
= "moonbit_uv_fs_close_sync"
445 446///| 447pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File) -> Unit raise Errno
fs_close_sync
(
Loop
self
:
type Loop Bytes
Loop
,
File
file
:
type File Int
File
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
448 let
Fs
req
=
() -> Fs
uv_fs_make
()
449 let
Int
status
=
(uv : Loop, req : Fs, file : File) -> Int
uv_fs_close_sync
(
Loop
self
,
Fs
req
,
File
file
)
450 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
451 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
452 } 453} 454 455///| 456extern "c" fn uv_fs_get_result(req :
type Fs
Fs
) ->
Int64
Int64
= "moonbit_uv_fs_get_result"
457 458///| 459fn
type Fs
Fs
::
(self : Fs) -> Int64
result
(
Fs
self
:
type Fs
Fs
) ->
Int64
Int64
{
460
(req : Fs) -> Int64
uv_fs_get_result
(
Fs
self
)
461} 462 463///| 464type File
Int
Int
465 466///| 467extern "c" fn uv_fs_read( 468 uv :
type Loop Bytes
Loop
,
469 req :
type Fs
Fs
,
470 file :
type File Int
File
,
471 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
472 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
473 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
474 offset :
Int64
Int64
,
475 cb : (
type Fs
Fs
) ->
Unit
Unit
476) ->
Int
Int
= "moonbit_uv_fs_read"
477 478///| 479/// Asynchronously reads data from a file into multiple buffers. 480/// 481/// This function initiates a read operation that will be handled asynchronously 482/// by the event loop. The data is read from the file into the provided array of 483/// byte views, which can be used for scatter-gather I/O operations. When the 484/// operation completes, either the success callback or error callback will be 485/// invoked depending on the result. 486/// 487/// Parameters: 488/// 489/// * `self` : The event loop instance to schedule the operation on. 490/// * `file` : The file descriptor to read from, typically obtained from 491/// `fs_open` or `fs_open_sync`. 492/// * `bufs` : An array of byte views that will receive the data read from the 493/// file. The views define both the memory locations and the amount of data to 494/// read into each buffer. 495/// * `offset` : The file position to start reading from. If `-1` (default), 496/// reads from the current file position. Otherwise, reads from the specified 497/// absolute position in the file. 498/// * `read_cb` : Success callback function that receives the filesystem request 499/// handle and the number of bytes actually read when the operation succeeds. 500/// * `error_cb` : Error callback function that receives the filesystem request 501/// handle and the error code when the operation fails. 502/// 503/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 504/// invalid file descriptor or system resource exhaustion). 505/// 506/// Example: 507/// 508/// ```moonbit 509/// let uv = @uv.Loop::new() 510/// uv.fs_read( 511/// @uv.stdin(), 512/// [Bytes::make(100, 0)], 513/// fn(bytes_read) { println("Read \{bytes_read} bytes") }, 514/// fn(error) { println("Error: \{error}") } 515/// ) 516/// |> ignore() 517/// uv.run(Default) 518/// uv.close() 519/// ``` 520pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = .., read_cb : (Int) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously reads data from a file into multiple buffers.

This function initiates a read operation that will be handled asynchronously by the event loop. The data is read from the file into the provided array of byte views, which can be used for scatter-gather I/O operations. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • file : The file descriptor to read from, typically obtained from fs_open or fs_open_sync.
  • bufs : An array of byte views that will receive the data read from the file. The views define both the memory locations and the amount of data to read into each buffer.
  • offset : The file position to start reading from. If -1 (default), reads from the current file position. Otherwise, reads from the specified absolute position in the file.
  • read_cb : Success callback function that receives the filesystem request handle and the number of bytes actually read when the operation succeeds.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid file descriptor or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
uv.fs_read(
  @uv.stdin(),
  [Bytes::make(100, 0)],
  fn(bytes_read) { println("Read \{bytes_read} bytes") },
  fn(error) { println("Error: \{error}") }
)
|> ignore()
uv.run(Default)
uv.close()
fs_read
(
521
Loop
self
:
type Loop Bytes
Loop
,
522
File
file
:
type File Int
File
,
523
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
],
524
Int64
offset
~ :
Int64
Int64
= -1,
525
(Int) -> Unit
read_cb
: (
Int
Int
) ->
Unit
Unit
,
526
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
527) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
528 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
529 let
Int64
result
=
Fs
req
.
(self : Fs) -> Int64
result
()
530
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
531 if
Int64
result
(self_ : Int64, other : Int64) -> Bool
<
0 {
532
(Errno) -> Unit
error_cb
(
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:

  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
()))
533 } else { 534
(Int) -> Unit
read_cb
(
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:

  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
())
535 } 536 } 537 538 let
Fs
req
=
() -> Fs
uv_fs_make
()
539 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
length
()
547 } 548 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
(
549
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
(Fs) -> Unit
cb
,
550 ) 551 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
552 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
553 } 554 return
Fs
req
555} 556 557///| 558extern "c" fn uv_fs_read_sync( 559 uv :
type Loop Bytes
Loop
,
560 req :
type Fs
Fs
,
561 file :
type File Int
File
,
562 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
563 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
564 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
565 offset :
Int64
Int64
566) ->
Int
Int
= "moonbit_uv_fs_read_sync"
567 568///| 569pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = ..) -> Int raise Errno
fs_read_sync
(
570
Loop
self
:
type Loop Bytes
Loop
,
571
File
file
:
type File Int
File
,
572
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
],
573
Int64
offset
~ :
Int64
Int64
= -1
574) ->
Int
Int
raise
type! Errno Status
Errno
{
575 let
Fs
req
=
() -> Fs
uv_fs_make
()
576 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
length
()
584 } 585 let
Int
status
=
(uv : Loop, req : Fs, file : File, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], offset : Int64) -> Int
uv_fs_read_sync
(
586
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
587 ) 588 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
589 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
590 } 591
Int
status
592} 593 594///| 595extern "c" fn uv_fs_write( 596 uv :
type Loop Bytes
Loop
,
597 req :
type Fs
Fs
,
598 file :
type File Int
File
,
599 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
600 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
601 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
602 offset :
Int64
Int64
,
603 write_cb : (
type Fs
Fs
) ->
Unit
Unit
604) ->
Int
Int
= "moonbit_uv_fs_write"
605 606///| 607/// Asynchronously writes data from multiple buffers to a file. 608/// 609/// This function initiates a write operation that will be handled asynchronously 610/// by the event loop. The data is written from the provided array of byte views 611/// to the file, which can be used for gather I/O operations. When the operation 612/// completes, either the success callback or error callback will be invoked 613/// depending on the result. 614/// 615/// Parameters: 616/// 617/// * `self` : The event loop instance to schedule the operation on. 618/// * `file` : The file descriptor to write to, typically obtained from `fs_open` 619/// or `fs_open_sync`. 620/// * `bufs` : An array of byte views containing the data to write to the file. 621/// The views define both the memory locations and the amount of data to write 622/// from each buffer. 623/// * `offset` : The file position to start writing to. If `-1` (default), writes 624/// to the current file position. Otherwise, writes to the specified absolute 625/// position in the file. 626/// * `write_cb` : Success callback function that receives the filesystem request 627/// handle and the number of bytes actually written when the operation succeeds. 628/// * `error_cb` : Error callback function that receives the filesystem request 629/// handle and the error code when the operation fails. 630/// 631/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 632/// invalid file descriptor or system resource exhaustion). 633/// 634/// Example: 635/// 636/// ```moonbit 637/// let uv = @uv.Loop::new() 638/// let path = "test/fixtures/doc-test.txt" 639/// let file = uv.fs_open_sync( 640/// path, 641/// @uv.OpenFlags::write_only(create=true), 642/// 0o644 643/// ) 644/// let data : Bytes = "Hello, World!" 645/// uv.fs_write( 646/// file, 647/// [data], 648/// fn(written) { println("Wrote \{written} bytes") }, 649/// fn(error) { println("Error: \{error}") } 650/// ) 651/// |> ignore() 652/// uv.run(Default) 653/// uv.fs_close_sync(file) 654/// uv.fs_unlink_sync(path) 655/// uv.close() 656/// ``` 657pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = .., write_cb : (Int) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously writes data from multiple buffers to a file.

This function initiates a write operation that will be handled asynchronously by the event loop. The data is written from the provided array of byte views to the file, which can be used for gather I/O operations. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • file : The file descriptor to write to, typically obtained from fs_open or fs_open_sync.
  • bufs : An array of byte views containing the data to write to the file. The views define both the memory locations and the amount of data to write from each buffer.
  • offset : The file position to start writing to. If -1 (default), writes to the current file position. Otherwise, writes to the specified absolute position in the file.
  • write_cb : Success callback function that receives the filesystem request handle and the number of bytes actually written when the operation succeeds.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid file descriptor or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
let path = "test/fixtures/doc-test.txt"
let file = uv.fs_open_sync(
  path,
  @uv.OpenFlags::write_only(create=true),
  0o644
)
let data : Bytes = "Hello, World!"
uv.fs_write(
  file,
  [data],
  fn(written) { println("Wrote \{written} bytes") },
  fn(error) { println("Error: \{error}") }
)
|> ignore()
uv.run(Default)
uv.fs_close_sync(file)
uv.fs_unlink_sync(path)
uv.close()
fs_write
(
658
Loop
self
:
type Loop Bytes
Loop
,
659
File
file
:
type File Int
File
,
660
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
],
661
Int64
offset
~ :
Int64
Int64
= -1,
662
(Int) -> Unit
write_cb
: (
Int
Int
) ->
Unit
Unit
,
663
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
664) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
665 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
666 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:

  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
()
667
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
668 if
Int
result
(self_ : Int, other : Int) -> Bool
<
0 {
669
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
result
))
670 } else { 671
(Int) -> Unit
write_cb
(
Int
result
)
672 } 673 } 674 675 let
Fs
req
=
() -> Fs
uv_fs_make
()
676 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
length
()
684 } 685 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
(
686
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
(Fs) -> Unit
cb
,
687 ) 688 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
689 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
690 } 691 return
Fs
req
692} 693 694///| 695extern "c" fn uv_fs_write_sync( 696 uv :
type Loop Bytes
Loop
,
697 req :
type Fs
Fs
,
698 file :
type File Int
File
,
699 bufs_base :
type FixedArray[A]
FixedArray
[
Bytes
Bytes
],
700 bufs_offset :
type FixedArray[A]
FixedArray
[
Int
Int
],
701 bufs_length :
type FixedArray[A]
FixedArray
[
Int
Int
],
702 offset :
Int64
Int64
703) ->
Int
Int
= "moonbit_uv_fs_write_sync"
704 705///| 706/// Synchronously writes data from multiple buffers to a file. 707/// 708/// This function blocks the current thread until the write operation completes. 709/// Unlike the asynchronous `fs_write` function, this operation does not require 710/// callbacks and returns immediately upon successful completion or failure. 711/// 712/// Parameters: 713/// 714/// * `self` : The event loop instance to perform the operation on. 715/// * `file` : The file descriptor to write to, typically obtained from `fs_open` 716/// or `fs_open_sync`. 717/// * `bufs` : An array of byte views containing the data to write to the file. 718/// The views define both the memory locations and the amount of data to write 719/// from each buffer. 720/// * `offset` : The file position to start writing to. If `-1` (default), writes 721/// to the current file position. Otherwise, writes to the specified absolute 722/// position in the file. 723/// 724/// Throws an error of type `Errno` if the file cannot be written to (e.g., 725/// invalid file descriptor, insufficient permissions, disk full, or system 726/// resource exhaustion). 727/// 728/// Example: 729/// 730/// ```moonbit 731/// let uv = @uv.Loop::new() 732/// let path = "test/fixtures/doc-test-sync.txt" 733/// let file = uv.fs_open_sync( 734/// path, 735/// @uv.OpenFlags::write_only(create=true), 736/// 0o644 737/// ) 738/// let data : Bytes = "Hello, World!" 739/// uv.fs_write_sync(file, [data]) 740/// uv.fs_close_sync(file) 741/// uv.fs_unlink_sync(path) 742/// uv.close() 743/// ``` 744pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, bufs : Array[@bytes.View], offset~ : Int64 = ..) -> Unit raise Errno

Synchronously writes data from multiple buffers to a file.

This function blocks the current thread until the write operation completes. Unlike the asynchronous fs_write function, this operation does not require callbacks and returns immediately upon successful completion or failure.

Parameters:

  • self : The event loop instance to perform the operation on.
  • file : The file descriptor to write to, typically obtained from fs_open or fs_open_sync.
  • bufs : An array of byte views containing the data to write to the file. The views define both the memory locations and the amount of data to write from each buffer.
  • offset : The file position to start writing to. If -1 (default), writes to the current file position. Otherwise, writes to the specified absolute position in the file.

Throws an error of type Errno if the file cannot be written to (e.g., invalid file descriptor, insufficient permissions, disk full, or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
let path = "test/fixtures/doc-test-sync.txt"
let file = uv.fs_open_sync(
  path,
  @uv.OpenFlags::write_only(create=true),
  0o644
)
let data : Bytes = "Hello, World!"
uv.fs_write_sync(file, [data])
uv.fs_close_sync(file)
uv.fs_unlink_sync(path)
uv.close()
fs_write_sync
(
745
Loop
self
:
type Loop Bytes
Loop
,
746
File
file
:
type File Int
File
,
747
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
],
748
Int64
offset
~ :
Int64
Int64
= -1
749) ->
Unit
Unit
raise
type! Errno Status
Errno
{
750 let
Fs
req
=
() -> Fs
uv_fs_make
()
751 let
Int
bufs_size
=
Array[@bytes.View]
bufs
.
(self : Array[@bytes.View]) -> Int

Returns the number of elements in the array.

Parameters:

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

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

Example:

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Throws a panic if length is negative.

Example:

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

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

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

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

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Bytes

Retrieves the underlying Bytes from a View.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Retrieves the start index of the view.

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

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

Parameters:

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

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

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
Array[@bytes.View]
bufs
(Array[@bytes.View], Int) -> @bytes.View

Retrieves an element from the array at the specified index.

Parameters:

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

Returns the element at the specified index.

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

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
length
()
759 } 760 let
Int
status
=
(uv : Loop, req : Fs, file : File, bufs_base : FixedArray[Bytes], bufs_offset : FixedArray[Int], bufs_length : FixedArray[Int], offset : Int64) -> Int
uv_fs_write_sync
(
761
Loop
self
,
Fs
req
,
File
file
,
FixedArray[Bytes]
bufs_base
,
FixedArray[Int]
bufs_offset
,
FixedArray[Int]
bufs_length
,
Int64
offset
,
762 ) 763 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
764 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
765 } 766} 767 768///| 769extern "c" fn uv_fs_ftruncate( 770 uv :
type Loop Bytes
Loop
,
771 req :
type Fs
Fs
,
772 file :
type File Int
File
,
773 length :
Int64
Int64
,
774 cb : (
type Fs
Fs
) ->
Unit
Unit
775) ->
Int
Int
= "moonbit_uv_fs_ftruncate"
776 777///| 778pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, length : Int64, k : () -> Unit, e : (Errno) -> Unit) -> Fs raise Errno
fs_ftruncate
(
779
Loop
self
:
type Loop Bytes
Loop
,
780
File
file
:
type File Int
File
,
781
Int64
length
:
Int64
Int64
,
782
() -> Unit
k
: () ->
Unit
Unit
,
783
(Errno) -> Unit
e
: (
type! Errno Status
Errno
) ->
Unit
Unit
784) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
785 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
786 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:

  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
()
787
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
788 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
789
(Errno) -> Unit
e
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
790 } else { 791
() -> Unit
k
()
792 } 793 } 794 795 let
Fs
req
=
() -> Fs
uv_fs_make
()
796 let
Int
status
=
(uv : Loop, req : Fs, file : File, length : Int64, cb : (Fs) -> Unit) -> Int
uv_fs_ftruncate
(
Loop
self
,
Fs
req
,
File
file
,
Int64
length
,
(Fs) -> Unit
cb
)
797 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
798 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
799 } 800 return
Fs
req
801} 802 803///| 804pub fn
() -> File
stdin
() ->
type File Int
File
{
805 0 806} 807 808///| 809pub fn
() -> File
stdout
() ->
type File Int
File
{
810 1 811} 812 813///| 814pub fn
() -> File
stderr
() ->
type File Int
File
{
815 2 816} 817 818///| 819extern "c" fn uv_fs_unlink( 820 uv :
type Loop Bytes
Loop
,
821 req :
type Fs
Fs
,
822 path :
Bytes
Bytes
,
823 cb : (
type Fs
Fs
) ->
Unit
Unit
824) ->
Int
Int
= "moonbit_uv_fs_unlink"
825 826///| 827pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, k : () -> Unit, e : (Error) -> Unit) -> Fs raise Errno
fs_unlink
(
828
Loop
self
:
type Loop Bytes
Loop
,
829
Ntbs
path
:

type parameter Ntbs

Ntbs
,
830
() -> Unit
k
: () ->
Unit
Unit
,
831
(Error) -> Unit
e
: (
type Error
Error
) ->
Unit
Unit
832) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
833 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
834 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:

  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
()
835
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
836 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
837
(Error) -> Unit
e
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
838 } else { 839
() -> Unit
k
()
840 } 841 } 842 843 let
Fs
req
=
() -> Fs
uv_fs_make
()
844 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
)
845 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
846 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
847 } 848 return
Fs
req
849} 850 851///| 852extern "c" fn uv_fs_unlink_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, path :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_unlink_sync"
853 854///| 855pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs) -> Unit raise Errno
fs_unlink_sync
(
856
Loop
self
:
type Loop Bytes
Loop
,
857
Ntbs
path
:

type parameter Ntbs

Ntbs
858) ->
Unit
Unit
raise
type! Errno Status
Errno
{
859 let
Fs
req
=
() -> Fs
uv_fs_make
()
860 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes) -> Int
uv_fs_unlink_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
())
861 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
862 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
863 } 864} 865 866///| 867extern "c" fn uv_fs_mkdir( 868 uv :
type Loop Bytes
Loop
,
869 req :
type Fs
Fs
,
870 path :
Bytes
Bytes
,
871 mode :
Int
Int
,
872 cb : (
type Fs
Fs
) ->
Unit
Unit
873) ->
Int
Int
= "moonbit_uv_fs_mkdir"
874 875///| 876/// Asynchronously creates a directory at the specified path with the given 877/// permissions. 878/// 879/// This function initiates a directory creation operation that will be handled 880/// asynchronously by the event loop. When the operation completes, either the 881/// success callback or error callback will be invoked depending on the result. 882/// 883/// Parameters: 884/// 885/// * `self` : The event loop instance to schedule the operation on. 886/// * `path` : The directory path to create. Can be any type that implements 887/// `ToNtbs` (convertible to null-terminated byte string), such as `String`, 888/// `Bytes`, `@string.View`, or `@bytes.View`. 889/// * `mode` : The directory permissions to use when creating the directory 890/// (octal notation, e.g., `0o755`). 891/// * `mkdir_cb` : Success callback function that receives the filesystem request 892/// handle when the directory is created successfully. 893/// * `error_cb` : Error callback function that receives the filesystem request 894/// handle and the error code when the operation fails. 895/// 896/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 897/// invalid parameters or system resource exhaustion). 898/// 899/// Example: 900/// 901/// ```moonbit 902/// let uv = @uv.Loop::new() 903/// let dir = "test/fixtures/doc-test-dir" 904/// uv.fs_mkdir( 905/// dir, 906/// 0o755, 907/// fn() { println("Directory created successfully") }, 908/// fn(error) { 909/// println("Failed to create directory: \{error} (expected)") 910/// } 911/// ) 912/// |> ignore() 913/// uv.run(Default) 914/// uv.fs_rmdir_sync(dir) 915/// uv.close() 916/// ``` 917pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, mode : Int, mkdir_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously creates a directory at the specified path with the given permissions.

This function initiates a directory creation operation that will be handled asynchronously by the event loop. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • path : The directory path to create. Can be any type that implements ToNtbs (convertible to null-terminated byte string), such as String, Bytes, @string.View, or @bytes.View.
  • mode : The directory permissions to use when creating the directory (octal notation, e.g., 0o755).
  • mkdir_cb : Success callback function that receives the filesystem request handle when the directory is created successfully.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid parameters or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
let dir = "test/fixtures/doc-test-dir"
uv.fs_mkdir(
  dir,
  0o755,
  fn() { println("Directory created successfully") },
  fn(error) {
    println("Failed to create directory: \{error} (expected)")
  }
)
|> ignore()
uv.run(Default)
uv.fs_rmdir_sync(dir)
uv.close()
fs_mkdir
(
918
Loop
self
:
type Loop Bytes
Loop
,
919
Ntbs
path
:

type parameter Ntbs

Ntbs
,
920
Int
mode
:
Int
Int
,
921
() -> Unit
mkdir_cb
: () ->
Unit
Unit
,
922
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
923) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
924 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
925 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:

  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
()
926
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
927 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
928
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
929 } else { 930
() -> Unit
mkdir_cb
()
931 } 932 } 933 934 let
Fs
req
=
() -> Fs
uv_fs_make
()
935 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
)
936 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
937 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
938 } 939 return
Fs
req
940} 941 942///| 943extern "c" fn uv_fs_mkdir_sync( 944 uv :
type Loop Bytes
Loop
,
945 req :
type Fs
Fs
,
946 path :
Bytes
Bytes
,
947 mode :
Int
Int
948) ->
Int
Int
= "moonbit_uv_fs_mkdir_sync"
949 950///| 951/// Synchronously creates a directory at the specified path with the given 952/// permissions. 953/// 954/// This function blocks the current thread until the directory creation 955/// operation completes. Unlike the asynchronous `fs_mkdir` function, this 956/// operation does not require callbacks and returns immediately upon completion 957/// or failure. 958/// 959/// Parameters: 960/// 961/// * `self` : The event loop instance to perform the operation on. 962/// * `path` : The directory path to create. Can be any type that implements 963/// `ToNtbs` (convertible to null-terminated byte string), such as `String`, 964/// `Bytes`, `@string.View`, or `@bytes.View`. 965/// * `mode` : The directory permissions to use when creating the directory 966/// (octal notation, e.g., `0o755`). 967/// 968/// Throws an error of type `Errno` if the directory cannot be created (e.g., 969/// path already exists, insufficient permissions, or invalid path). 970/// 971/// Example: 972/// 973/// ```moonbit 974/// let uv = @uv.Loop::new() 975/// uv.fs_mkdir_sync("test/fixtures", 0o755) catch { 976/// error => println("Failed to create directory: \{error} (expected)") 977/// } 978/// uv.close() 979/// ``` 980pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, mode : Int) -> Unit raise Errno

Synchronously creates a directory at the specified path with the given permissions.

This function blocks the current thread until the directory creation operation completes. Unlike the asynchronous fs_mkdir function, this operation does not require callbacks and returns immediately upon completion or failure.

Parameters:

  • self : The event loop instance to perform the operation on.
  • path : The directory path to create. Can be any type that implements ToNtbs (convertible to null-terminated byte string), such as String, Bytes, @string.View, or @bytes.View.
  • mode : The directory permissions to use when creating the directory (octal notation, e.g., 0o755).

Throws an error of type Errno if the directory cannot be created (e.g., path already exists, insufficient permissions, or invalid path).

Example:

let uv = @uv.Loop::new()
uv.fs_mkdir_sync("test/fixtures", 0o755) catch {
  error => println("Failed to create directory: \{error} (expected)")
}
uv.close()
fs_mkdir_sync
(
981
Loop
self
:
type Loop Bytes
Loop
,
982
Ntbs
path
:

type parameter Ntbs

Ntbs
,
983
Int
mode
:
Int
Int
984) ->
Unit
Unit
raise
type! Errno Status
Errno
{
985 let
Fs
req
=
() -> Fs
uv_fs_make
()
986 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, mode : Int) -> Int
uv_fs_mkdir_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
Int
mode
)
987 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
988 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
989 } 990} 991 992///| 993extern "c" fn uv_fs_rmdir( 994 uv :
type Loop Bytes
Loop
,
995 req :
type Fs
Fs
,
996 path :
Bytes
Bytes
,
997 cb : (
type Fs
Fs
) ->
Unit
Unit
998) ->
Int
Int
= "moonbit_uv_fs_rmdir"
999 1000///| 1001pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, rmdir_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_rmdir
(
1002
Loop
self
:
type Loop Bytes
Loop
,
1003
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1004
() -> Unit
rmdir_cb
: () ->
Unit
Unit
,
1005
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1006) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1007 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1008 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:

  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
()
1009
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1010 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1011
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1012 } else { 1013
() -> Unit
rmdir_cb
()
1014 } 1015 } 1016 1017 let
Fs
req
=
() -> Fs
uv_fs_make
()
1018 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
)
1019 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1020 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1021 } 1022 return
Fs
req
1023} 1024 1025///| 1026extern "c" fn uv_fs_rmdir_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, path :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_rmdir_sync"
1027 1028///| 1029pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs) -> Unit raise Errno
fs_rmdir_sync
(
1030
Loop
self
:
type Loop Bytes
Loop
,
1031
Ntbs
path
:

type parameter Ntbs

Ntbs
1032) ->
Unit
Unit
raise
type! Errno Status
Errno
{
1033 let
Fs
req
=
() -> Fs
uv_fs_make
()
1034 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes) -> Int
uv_fs_rmdir_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
())
1035 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1036 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1037 } 1038} 1039 1040///| 1041extern "c" fn uv_fs_scandir( 1042 uv :
type Loop Bytes
Loop
,
1043 req :
type Fs
Fs
,
1044 path :
Bytes
Bytes
,
1045 flags :
Int
Int
,
1046 cb : (
type Fs
Fs
) ->
Unit
Unit
1047) ->
Int
Int
= "moonbit_uv_fs_scandir"
1048 1049///| 1050pub struct Dirent { 1051
String
name
:
String
String
1052
DirentType
type_
:
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}

Type of directory entry, indicating the file system object kind.

DirentType
1053} derive(
trait ToJson {
  to_json(Self) -> Json
}

Trait for types that can be converted to Json

ToJson
(fields(type_(rename="type"))))
1054 1055///| 1056#deprecated("Access field directly, e.g., `dirent.name`") 1057pub fn
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
::
(self : Dirent) -> String
name
(
Dirent
self
:
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
) ->
String
String
{
1058
Dirent
self
.
String
name
1059} 1060 1061///| 1062#deprecated("Access field directly, e.g., `dirent.type_`") 1063pub fn
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
::
(self : Dirent) -> DirentType
type_
(
Dirent
self
:
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
) ->
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}

Type of directory entry, indicating the file system object kind.

DirentType
{
1064
Dirent
self
.
DirentType
type_
1065} 1066 1067///| 1068/// Type of directory entry, indicating the file system object kind. 1069pub enum DirentType { 1070 /// Unknown or unsupported file type 1071
DirentType

Unknown or unsupported file type

Unknown
1072 /// Regular file 1073
DirentType

Regular file

File
1074 /// Directory 1075
DirentType

Directory

Dir
1076 /// Symbolic link 1077
DirentType

Symbolic link

Link
1078 /// Named pipe (FIFO) 1079
DirentType

Named pipe (FIFO)

Fifo
1080 /// Socket file 1081
DirentType

Socket file

Socket
1082 /// Character device 1083
DirentType

Character device

Char
1084 /// Block device 1085
DirentType

Block device

Block
1086} derive(
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
)
1087 1088///| 1089pub 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
}

Type of directory entry, indicating the file system object kind.

DirentType
with
(self : DirentType) -> Json
to_json
(
DirentType
self
:
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}

Type of directory entry, indicating the file system object kind.

DirentType
) ->
enum Json {
  Null
  True
  False
  Number(Double)
  String(String)
  Array(Array[Json])
  Object(Map[String, Json])
}
Json
{
1090 match
DirentType
self
{
1091
DirentType

Unknown or unsupported file type

Unknown
=> "unknown"
1092
DirentType

Regular file

File
=> "file"
1093
DirentType

Directory

Dir
=> "dir"
1094
DirentType

Symbolic link

Link
=> "link"
1095
DirentType

Named pipe (FIFO)

Fifo
=> "fifo"
1096
DirentType

Socket file

Socket
=> "socket"
1097
DirentType

Character device

Char
=> "char"
1098
DirentType

Block device

Block
=> "block"
1099 } 1100} 1101 1102///| 1103extern "c" fn uv_dirent_make(n :
Int
Int
) ->
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
] = "moonbit_uv_dirent_make"
1104 1105///| 1106impl
trait @c.Load {
  load(@c.Pointer[Self], Int) -> Self
}
@c.Load
for
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
with
(pointer : @c.Pointer[Dirent], index : Int) -> Dirent
load
(
@c.Pointer[Dirent]
pointer
:
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
],
Int
index
:
Int
Int
) ->
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
{
1107 let
@c.Pointer[Byte]
name
=
(dirent : @c.Pointer[Dirent], offset : Int) -> @c.Pointer[Byte]
uv_dirent_get_name
(
@c.Pointer[Dirent]
pointer
,
Int
index
)
1108 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:

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

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

Parameters:

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

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

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1110
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(
@c.Pointer[Byte]
name
(@c.Pointer[Byte], Int) -> Byte
[
i])
1111 } 1112 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
1113 let
String
name
=
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
)
1114 let
DirentType
type_
=
(dirent : @c.Pointer[Dirent], offset : Int) -> DirentType
uv_dirent_get_type
(
@c.Pointer[Dirent]
pointer
,
Int
index
)
1115 {
String
name
,
DirentType
type_
}
1116} 1117 1118///| 1119extern "c" fn uv_dirent_get_name( 1120 dirent :
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
],
1121 offset :
Int
Int
1122) ->
type @c.Pointer[_]
@c.Pointer
[
Byte
Byte
] = "moonbit_uv_dirent_get_name"
1123 1124///| 1125extern "c" fn uv_dirent_get_type( 1126 dirent :
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
],
1127 offset :
Int
Int
1128) ->
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}

Type of directory entry, indicating the file system object kind.

DirentType
= "moonbit_uv_dirent_get_type"
1129 1130///| 1131extern "c" fn uv_dirent_delete(dirent :
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
]) = "moonbit_uv_dirent_delete"
1132 1133///| 1134extern "c" fn uv_fs_scandir_next(req :
type Fs
Fs
, ent :
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
]) ->
Int
Int
= "moonbit_uv_fs_scandir_next"
1135 1136///| 1137type Scandir
type Fs
Fs
1138 1139///| 1140pub fn
type Scandir Fs
Scandir
::
(self : Scandir) -> Dirent raise Errno
next
(
Scandir
self
:
type Scandir Fs
Scandir
) ->
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
raise
type! Errno Status
Errno
{
1141 let
Fs
req
=
Scandir
self
.
(self : Scandir) -> Fs

Convert newtype to its underlying type, automatically derived.

inner
()
1142 let
@c.Pointer[Dirent]
uv_dirent
=
(n : Int) -> @c.Pointer[Dirent]
uv_dirent_make
(1)
1143 let
Int
status
=
(req : Fs, ent : @c.Pointer[Dirent]) -> Int
uv_fs_scandir_next
(
Fs
req
,
@c.Pointer[Dirent]
uv_dirent
)
1144 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1145
(dirent : @c.Pointer[Dirent]) -> Unit
uv_dirent_delete
(
@c.Pointer[Dirent]
uv_dirent
)
1146
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1147 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1148 } 1149 let
Dirent
dirent
=
@c.Pointer[Dirent]
uv_dirent
[0]
1150
(dirent : @c.Pointer[Dirent]) -> Unit
uv_dirent_delete
(
@c.Pointer[Dirent]
uv_dirent
)
1151
Dirent
dirent
1152} 1153 1154///| 1155pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, flags : Int, scandir_cb : (Scandir) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_scandir
(
1156
Loop
self
:
type Loop Bytes
Loop
,
1157
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1158
Int
flags
:
Int
Int
,
1159
(Scandir) -> Unit
scandir_cb
: (
type Scandir Fs
Scandir
) ->
Unit
Unit
,
1160
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1161) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1162 let
Fs
req
=
() -> Fs
uv_fs_make
()
1163 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
) {
1164 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:

  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
()
1165 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1166
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1167
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1168 } else { 1169
(Scandir) -> Unit
scandir_cb
(
Fs
req
)
1170 } 1171 }) 1172 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1173 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1174 } 1175 return
Fs
req
1176} 1177 1178///| 1179extern "c" fn uv_fs_scandir_sync( 1180 uv :
type Loop Bytes
Loop
,
1181 req :
type Fs
Fs
,
1182 path :
Bytes
Bytes
,
1183 flags :
Int
Int
1184) ->
Int
Int
= "moonbit_uv_fs_scandir_sync"
1185 1186///| 1187pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
(self : Loop, path : Ntbs, flags : Int) -> Scandir raise Errno
fs_scandir_sync
(
1188
Loop
self
:
type Loop Bytes
Loop
,
1189
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1190
Int
flags
:
Int
Int
1191) ->
type Scandir Fs
Scandir
raise
type! Errno Status
Errno
{
1192 let
Fs
req
=
() -> Fs
uv_fs_make
()
1193 let
Bytes
path
=
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
()
1194 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, flags : Int) -> Int
uv_fs_scandir_sync
(
Loop
self
,
Fs
req
,
Bytes
path
,
Int
flags
)
1195 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1196
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1197 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1198 } 1199
Fs
req
1200} 1201 1202///| 1203extern "c" fn uv_fs_rename( 1204 uv :
type Loop Bytes
Loop
,
1205 req :
type Fs
Fs
,
1206 path :
Bytes
Bytes
,
1207 new_path :
Bytes
Bytes
,
1208 cb : (
type Fs
Fs
) ->
Unit
Unit
1209) ->
Int
Int
= "moonbit_uv_fs_rename"
1210 1211///| 1212pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, new_path : Ntbs, rename_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_rename
(
1213
Loop
self
:
type Loop Bytes
Loop
,
1214
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1215
Ntbs
new_path
:

type parameter Ntbs

Ntbs
,
1216
() -> Unit
rename_cb
: () ->
Unit
Unit
,
1217
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1218) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1219 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1220 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:

  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
()
1221
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1222 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1223
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1224 } else { 1225
() -> Unit
rename_cb
()
1226 } 1227 } 1228 1229 let
Fs
req
=
() -> Fs
uv_fs_make
()
1230 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
)
1231 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1232 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1233 } 1234 return
Fs
req
1235} 1236 1237///| 1238type CopyFileFlags
Int
Int
1239 1240///| 1241extern "c" fn uv_fs_COPYFILE_EXCL() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_EXCL"
1242 1243///| 1244extern "c" fn uv_fs_COPYFILE_FICLONE() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_FICLONE"
1245 1246///| 1247extern "c" fn uv_fs_COPYFILE_FICLONE_FORCE() ->
Int
Int
= "moonbit_uv_fs_COPYFILE_FICLONE_FORCE"
1248 1249///| 1250pub(all) enum CopyOnWrite { 1251
CopyOnWrite
False
1252
CopyOnWrite
True
1253
CopyOnWrite
Force
1254} 1255 1256///| 1257pub fn
type CopyFileFlags Int
CopyFileFlags
::
(allow_exists~ : Bool = .., copy_on_write~ : CopyOnWrite = ..) -> CopyFileFlags
new
(
1258
Bool
allow_exists
~ :
Bool
Bool
= true,
1259
CopyOnWrite
copy_on_write
~ :
enum CopyOnWrite {
  False
  True
  Force
}
CopyOnWrite
=
CopyOnWrite
False
1260) ->
type CopyFileFlags Int
CopyFileFlags
{
1261 let mut
Int
flags
= if
Bool
allow_exists
{ 0 } else {
() -> Int
uv_fs_COPYFILE_EXCL
() }
1262 match
CopyOnWrite
copy_on_write
{
1263
CopyOnWrite
False
=> ()
1264
CopyOnWrite
True
=>
Int
flags
=
Int
flags
(Int, Int) -> Int
|
() -> Int
uv_fs_COPYFILE_FICLONE
()
1265
CopyOnWrite
Force
=>
Int
flags
=
Int
flags
(Int, Int) -> Int
|
() -> Int
uv_fs_COPYFILE_FICLONE_FORCE
()
1266 } 1267 return
Int
flags
1268} 1269 1270///| 1271extern "c" fn uv_fs_copyfile( 1272 uv :
type Loop Bytes
Loop
,
1273 req :
type Fs
Fs
,
1274 path :
Bytes
Bytes
,
1275 new_path :
Bytes
Bytes
,
1276 flags :
Int
Int
,
1277 cb : (
type Fs
Fs
) ->
Unit
Unit
1278) ->
Int
Int
= "moonbit_uv_fs_copyfile"
1279 1280///| 1281pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, new_path : Ntbs, flags : CopyFileFlags, copy_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_copyfile
(
1282
Loop
self
:
type Loop Bytes
Loop
,
1283
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1284
Ntbs
new_path
:

type parameter Ntbs

Ntbs
,
1285
CopyFileFlags
flags
:
type CopyFileFlags Int
CopyFileFlags
,
1286
() -> Unit
copy_cb
: () ->
Unit
Unit
,
1287
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1288) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1289 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1290 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:

  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
()
1291
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1292 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1293
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1294 } else { 1295
() -> Unit
copy_cb
()
1296 } 1297 } 1298 1299 let
Fs
req
=
() -> Fs
uv_fs_make
()
1300 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, new_path : Bytes, flags : Int, cb : (Fs) -> Unit) -> Int
uv_fs_copyfile
(
1301
Loop
self
,
1302
Fs
req
,
1303
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
1304
Ntbs
new_path
.
(Ntbs) -> Bytes
to_ntbs
(),
1305
CopyFileFlags
flags
.
(self : CopyFileFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
(),
1306
(Fs) -> Unit
cb
,
1307 ) 1308 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1309 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1310 } 1311 return
Fs
req
1312} 1313 1314///| 1315extern "c" fn uv_fs_copyfile_sync( 1316 uv :
type Loop Bytes
Loop
,
1317 req :
type Fs
Fs
,
1318 path :
Bytes
Bytes
,
1319 new_path :
Bytes
Bytes
,
1320 flags :
Int
Int
1321) ->
Int
Int
= "moonbit_uv_fs_copyfile_sync"
1322 1323///| 1324pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, new_path : Ntbs, flags : CopyFileFlags) -> Unit raise Errno
fs_copyfile_sync
(
1325
Loop
self
:
type Loop Bytes
Loop
,
1326
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1327
Ntbs
new_path
:

type parameter Ntbs

Ntbs
,
1328
CopyFileFlags
flags
:
type CopyFileFlags Int
CopyFileFlags
1329) ->
Unit
Unit
raise
type! Errno Status
Errno
{
1330 let
Fs
req
=
() -> Fs
uv_fs_make
()
1331 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, new_path : Bytes, flags : Int) -> Int
uv_fs_copyfile_sync
(
1332
Loop
self
,
1333
Fs
req
,
1334
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
1335
Ntbs
new_path
.
(Ntbs) -> Bytes
to_ntbs
(),
1336
CopyFileFlags
flags
.
(self : CopyFileFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
(),
1337 ) 1338 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1339 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1340 } 1341} 1342 1343///| 1344extern "c" fn uv_cwd(buffer :
Bytes
Bytes
, length :
type FixedArray[A]
FixedArray
[
Int
Int
]) ->
Int
Int
= "moonbit_uv_cwd"
1345 1346///| 1347pub fn
() -> String raise Errno
cwd
() ->
String
String
raise
type! Errno Status
Errno
{
1348 let mut
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:

  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
(64, 0)
1349 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:

  let bytes = b"\x01\x02\x03"
  inspect(bytes.length(), content="3")
  let empty = b""
  inspect(empty.length(), content="0")
length
()]
1350 let mut
Int
status
=
(buffer : Bytes, length : FixedArray[Int]) -> Int
uv_cwd
(
Bytes
buffer
,
FixedArray[Int]
length
)
1351 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:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Int
_ENOBUFS
{
1352
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:

  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)
1353
Int
status
=
(buffer : Bytes, length : FixedArray[Int]) -> Int
uv_cwd
(
Bytes
buffer
,
FixedArray[Int]
length
)
1354 } 1355 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1356 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1357 } 1358 return
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
buffer
[:
FixedArray[Int]
length
[0]])
1359} 1360 1361///| 1362extern "c" fn uv_fs_get_ptr(req :
type Fs
Fs
) ->
type @c.Pointer[_]
@c.Pointer
[
Unit
Unit
] = "moonbit_uv_fs_get_ptr"
1363 1364///| 1365extern "c" fn uv_fs_get_dir(req :
type Fs
Fs
) ->
type Dir
Dir
= "moonbit_uv_fs_get_dir"
1366 1367///| 1368extern "c" fn uv_fs_realpath( 1369 uv :
type Loop Bytes
Loop
,
1370 req :
type Fs
Fs
,
1371 path :
Bytes
Bytes
,
1372 cb : (
type Fs
Fs
) ->
Unit
Unit
1373) ->
Int
Int
= "moonbit_uv_fs_realpath"
1374 1375///| 1376type Stat
Bytes
Bytes
1377 1378///| 1379extern "c" fn moonbit_uv_stat_get_dev(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_dev"
1380 1381///| 1382extern "c" fn moonbit_uv_stat_get_mode(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_mode"
1383 1384///| 1385extern "c" fn moonbit_uv_stat_get_nlink(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_nlink"
1386 1387///| 1388extern "c" fn moonbit_uv_stat_get_uid(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_uid"
1389 1390///| 1391extern "c" fn moonbit_uv_stat_get_gid(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_gid"
1392 1393///| 1394extern "c" fn moonbit_uv_stat_get_rdev(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_rdev"
1395 1396///| 1397extern "c" fn moonbit_uv_stat_get_ino(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_ino"
1398 1399///| 1400extern "c" fn moonbit_uv_stat_get_size(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_size"
1401 1402///| 1403extern "c" fn moonbit_uv_stat_get_blksize(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_blksize"
1404 1405///| 1406extern "c" fn moonbit_uv_stat_get_blocks(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_blocks"
1407 1408///| 1409extern "c" fn moonbit_uv_stat_get_flags(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_flags"
1410 1411///| 1412extern "c" fn moonbit_uv_stat_get_gen(stat :
type Stat Bytes
Stat
) ->
UInt64
UInt64
= "moonbit_uv_stat_get_gen"
1413 1414///| 1415extern "c" fn moonbit_uv_stat_get_atim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_atim_sec"
1416 1417///| 1418extern "c" fn moonbit_uv_stat_get_atim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_atim_nsec"
1419 1420///| 1421extern "c" fn moonbit_uv_stat_get_mtim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_mtim_sec"
1422 1423///| 1424extern "c" fn moonbit_uv_stat_get_mtim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_mtim_nsec"
1425 1426///| 1427extern "c" fn moonbit_uv_stat_get_ctim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_ctim_sec"
1428 1429///| 1430extern "c" fn moonbit_uv_stat_get_ctim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_ctim_nsec"
1431 1432///| 1433extern "c" fn moonbit_uv_stat_get_birthtim_sec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_birthtim_sec"
1434 1435///| 1436extern "c" fn moonbit_uv_stat_get_birthtim_nsec(stat :
type Stat Bytes
Stat
) ->
Int64
Int64
= "moonbit_uv_stat_get_birthtim_nsec"
1437 1438///| 1439pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
dev
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1440
(stat : Stat) -> UInt64
moonbit_uv_stat_get_dev
(
Stat
self
)
1441} 1442 1443///| 1444pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
mode
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1445
(stat : Stat) -> UInt64
moonbit_uv_stat_get_mode
(
Stat
self
)
1446} 1447 1448///| 1449pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
nlink
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1450
(stat : Stat) -> UInt64
moonbit_uv_stat_get_nlink
(
Stat
self
)
1451} 1452 1453///| 1454pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
uid
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1455
(stat : Stat) -> UInt64
moonbit_uv_stat_get_uid
(
Stat
self
)
1456} 1457 1458///| 1459pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
gid
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1460
(stat : Stat) -> UInt64
moonbit_uv_stat_get_gid
(
Stat
self
)
1461} 1462 1463///| 1464pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
rdev
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1465
(stat : Stat) -> UInt64
moonbit_uv_stat_get_rdev
(
Stat
self
)
1466} 1467 1468///| 1469pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
ino
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1470
(stat : Stat) -> UInt64
moonbit_uv_stat_get_ino
(
Stat
self
)
1471} 1472 1473///| 1474pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
size
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1475
(stat : Stat) -> UInt64
moonbit_uv_stat_get_size
(
Stat
self
)
1476} 1477 1478///| 1479pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
blksize
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1480
(stat : Stat) -> UInt64
moonbit_uv_stat_get_blksize
(
Stat
self
)
1481} 1482 1483///| 1484pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
blocks
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1485
(stat : Stat) -> UInt64
moonbit_uv_stat_get_blocks
(
Stat
self
)
1486} 1487 1488///| 1489pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
flags
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1490
(stat : Stat) -> UInt64
moonbit_uv_stat_get_flags
(
Stat
self
)
1491} 1492 1493///| 1494pub fn
type Stat Bytes
Stat
::
(self : Stat) -> UInt64
gen
(
Stat
self
:
type Stat Bytes
Stat
) ->
UInt64
UInt64
{
1495
(stat : Stat) -> UInt64
moonbit_uv_stat_get_gen
(
Stat
self
)
1496} 1497 1498///| 1499pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
atim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1500
(stat : Stat) -> Int64
moonbit_uv_stat_get_atim_sec
(
Stat
self
)
1501} 1502 1503///| 1504pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
atim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1505
(stat : Stat) -> Int64
moonbit_uv_stat_get_atim_nsec
(
Stat
self
)
1506} 1507 1508///| 1509pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
mtim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1510
(stat : Stat) -> Int64
moonbit_uv_stat_get_mtim_sec
(
Stat
self
)
1511} 1512 1513///| 1514pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
mtim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1515
(stat : Stat) -> Int64
moonbit_uv_stat_get_mtim_nsec
(
Stat
self
)
1516} 1517 1518///| 1519pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
ctim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1520
(stat : Stat) -> Int64
moonbit_uv_stat_get_ctim_sec
(
Stat
self
)
1521} 1522 1523///| 1524pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
ctim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1525
(stat : Stat) -> Int64
moonbit_uv_stat_get_ctim_nsec
(
Stat
self
)
1526} 1527 1528///| 1529pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
birthtim_sec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1530
(stat : Stat) -> Int64
moonbit_uv_stat_get_birthtim_sec
(
Stat
self
)
1531} 1532 1533///| 1534pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Int64
birthtim_nsec
(
Stat
self
:
type Stat Bytes
Stat
) ->
Int64
Int64
{
1535
(stat : Stat) -> Int64
moonbit_uv_stat_get_birthtim_nsec
(
Stat
self
)
1536} 1537 1538///| 1539pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_file
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1540 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFREG
1541} 1542 1543///| 1544pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_regular
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1545 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFREG
1546} 1547 1548///| 1549pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_directory
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1550 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFDIR
1551} 1552 1553///| 1554pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_symlink
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1555 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFLNK
1556} 1557 1558///| 1559pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool raise Errno
is_socket
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
raise
type! Errno Status
Errno
{
1560 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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
@uint64.max_value
{
1561 raise
(Status) -> Errno
Errno
(
Status
ENOSYS
)
1562 } 1563 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFSOCK
1564} 1565 1566///| 1567#deprecated("Use Stat::is_fifo instead") 1568pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_pipe
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1569 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFIFO
1570} 1571 1572///| 1573pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_fifo
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1574 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFIFO
1575} 1576 1577///| 1578pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool raise Errno
is_block_device
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
raise
type! Errno Status
Errno
{
1579 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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
@uint64.max_value
{
1580 raise
(Status) -> Errno
Errno
(
Status
ENOSYS
)
1581 } 1582 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFBLK
1583} 1584 1585///| 1586pub fn
type Stat Bytes
Stat
::
(self : Stat) -> Bool
is_character_device
(
Stat
self
:
type Stat Bytes
Stat
) ->
Bool
Bool
{
1587 (
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:

  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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFCHR
1588} 1589 1590///| 1591pub fn
type Stat Bytes
Stat
::
(self : Stat) -> DirentType
type_
(
Stat
self
:
type Stat Bytes
Stat
) ->
enum DirentType {
  Unknown
  File
  Dir
  Link
  Fifo
  Socket
  Char
  Block
}

Type of directory entry, indicating the file system object kind.

DirentType
{
1592 let
UInt64
type_
=
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:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
UInt64
_S_IFMT
1593 if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFREG
{
1594 DirentType::
DirentType

Regular file

File
1595 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFDIR
{
1596 DirentType::
DirentType

Directory

Dir
1597 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFLNK
{
1598 DirentType::
DirentType

Symbolic link

Link
1599 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFSOCK
{
1600 DirentType::
DirentType

Socket file

Socket
1601 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFIFO
{
1602 DirentType::
DirentType

Named pipe (FIFO)

Fifo
1603 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFCHR
{
1604 DirentType::
DirentType

Character device

Char
1605 } else if
UInt64
type_
(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:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
UInt64
_S_IFBLK
{
1606 DirentType::
DirentType

Block device

Block
1607 } else { 1608 DirentType::
DirentType

Unknown or unsupported file type

Unknown
1609 } 1610} 1611 1612///| 1613extern "c" fn uv_fs_S_IFMT() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFMT"
1614 1615///| 1616let
UInt64
_S_IFMT
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFMT
()
1617 1618///| 1619extern "c" fn uv_fs_S_IFREG() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFREG"
1620 1621///| 1622let
UInt64
_S_IFREG
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFREG
()
1623 1624///| 1625extern "c" fn uv_fs_S_IFDIR() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFDIR"
1626 1627///| 1628let
UInt64
_S_IFDIR
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFDIR
()
1629 1630///| 1631extern "c" fn uv_fs_S_IFLNK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFLNK"
1632 1633///| 1634let
UInt64
_S_IFLNK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFLNK
()
1635 1636///| 1637extern "c" fn uv_fs_S_IFSOCK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFSOCK"
1638 1639///| 1640let
UInt64
_S_IFSOCK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFSOCK
()
1641 1642///| 1643extern "c" fn uv_fs_S_IFIFO() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFIFO"
1644 1645///| 1646let
UInt64
_S_IFIFO
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFIFO
()
1647 1648///| 1649extern "c" fn uv_fs_S_IFBLK() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFBLK"
1650 1651///| 1652let
UInt64
_S_IFBLK
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFBLK
()
1653 1654///| 1655extern "c" fn uv_fs_S_IFCHR() ->
UInt64
UInt64
= "moonbit_uv_fs_S_IFCHR"
1656 1657///| 1658let
UInt64
_S_IFCHR
:
UInt64
UInt64
=
() -> UInt64
uv_fs_S_IFCHR
()
1659 1660///| 1661extern "c" fn uv_fs_get_statbuf(req :
type Fs
Fs
) ->
type Stat Bytes
Stat
= "moonbit_uv_fs_get_statbuf"
1662 1663///| 1664extern "c" fn uv_fs_stat( 1665 uv :
type Loop Bytes
Loop
,
1666 req :
type Fs
Fs
,
1667 path :
Bytes
Bytes
,
1668 cb : (
type Fs
Fs
) ->
Unit
Unit
1669) ->
Int
Int
= "moonbit_uv_fs_stat"
1670 1671///| 1672pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, stat_cb : (Stat) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_stat
(
1673
Loop
self
:
type Loop Bytes
Loop
,
1674
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1675
(Stat) -> Unit
stat_cb
: (
type Stat Bytes
Stat
) ->
Unit
Unit
,
1676
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1677) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1678 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1679 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:

  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
()
1680 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1681
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1682
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1683 } else { 1684 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
1685
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1686
(Stat) -> Unit
stat_cb
(
Stat
statbuf
)
1687 } 1688 } 1689 1690 let
Fs
req
=
() -> Fs
uv_fs_make
()
1691 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
)
1692 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1693 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1694 } 1695 return
Fs
req
1696} 1697 1698///| 1699extern "c" fn uv_fs_stat_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, path :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_stat_sync"
1700 1701///| 1702pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs) -> Stat raise Errno
fs_stat_sync
(
1703
Loop
self
:
type Loop Bytes
Loop
,
1704
Ntbs
path
:

type parameter Ntbs

Ntbs
1705) ->
type Stat Bytes
Stat
raise
type! Errno Status
Errno
{
1706 let
Fs
req
=
() -> Fs
uv_fs_make
()
1707 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes) -> Int
uv_fs_stat_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
())
1708 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1709
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1710 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1711 } 1712 let
Stat
stat
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
1713
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1714 return
Stat
stat
1715} 1716 1717///| 1718extern "c" fn uv_fs_lstat( 1719 uv :
type Loop Bytes
Loop
,
1720 req :
type Fs
Fs
,
1721 path :
Bytes
Bytes
,
1722 cb : (
type Fs
Fs
) ->
Unit
Unit
1723) ->
Int
Int
= "moonbit_uv_fs_lstat"
1724 1725///| 1726pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, lstat_cb : (Stat) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_lstat
(
1727
Loop
self
:
type Loop Bytes
Loop
,
1728
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1729
(Stat) -> Unit
lstat_cb
: (
type Stat Bytes
Stat
) ->
Unit
Unit
,
1730
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1731) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1732 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1733 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:

  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
()
1734 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1735
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1736
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1737 } else { 1738 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
1739
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1740
(Stat) -> Unit
lstat_cb
(
Stat
statbuf
)
1741 } 1742 } 1743 1744 let
Fs
req
=
() -> Fs
uv_fs_make
()
1745 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
)
1746 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1747 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1748 } 1749 return
Fs
req
1750} 1751 1752///| 1753extern "c" fn uv_fs_fstat( 1754 uv :
type Loop Bytes
Loop
,
1755 req :
type Fs
Fs
,
1756 file :
type File Int
File
,
1757 cb : (
type Fs
Fs
) ->
Unit
Unit
1758) ->
Int
Int
= "moonbit_uv_fs_fstat"
1759 1760///| 1761pub fn
type Loop Bytes
Loop
::
(self : Loop, file : File, fstat_cb : (Stat) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_fstat
(
1762
Loop
self
:
type Loop Bytes
Loop
,
1763
File
file
:
type File Int
File
,
1764
(Stat) -> Unit
fstat_cb
: (
type Stat Bytes
Stat
) ->
Unit
Unit
,
1765
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1766) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1767 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1768 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:

  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
()
1769 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1770
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1771
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1772 } else { 1773 let
Stat
statbuf
=
(req : Fs) -> Stat
uv_fs_get_statbuf
(
Fs
req
)
1774
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1775
(Stat) -> Unit
fstat_cb
(
Stat
statbuf
)
1776 } 1777 } 1778 1779 let
Fs
req
=
() -> Fs
uv_fs_make
()
1780 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
)
1781 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1782 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1783 } 1784 return
Fs
req
1785} 1786 1787///| 1788/// Asynchronously resolves a file path to its absolute canonical form. 1789/// 1790/// This function initiates a path resolution operation that will be handled 1791/// asynchronously by the event loop. The resolved path eliminates symbolic 1792/// links, relative path components (like `.` and `..`), and redundant 1793/// separators to produce an absolute canonical path. When the operation 1794/// completes, either the success callback or error callback will be invoked 1795/// depending on the result. 1796/// 1797/// Parameters: 1798/// 1799/// * `self` : The event loop instance to schedule the operation on. 1800/// * `path` : The file path to resolve. Can be any type that implements 1801/// `ToNtbs` (convertible to null-terminated byte string), such as `String`, 1802/// `Bytes`, `@string.View`, or `@bytes.View`. 1803/// * `path_cb` : Success callback function that receives the filesystem request 1804/// handle and the resolved absolute path when the operation succeeds. 1805/// * `error_cb` : Error callback function that receives the filesystem request 1806/// handle and the error code when the operation fails. 1807/// 1808/// Throws an error of type `Errno` if the operation cannot be initiated (e.g., 1809/// invalid parameters or system resource exhaustion). 1810/// 1811/// Example: 1812/// 1813/// ```moonbit 1814/// let uv = @uv.Loop::new() 1815/// uv.fs_realpath( 1816/// "test/fixtures/example.txt", 1817/// realpath => println("Resolved: \{realpath}"), 1818/// error => println("Failed to resolve path: \{error}") 1819/// ) 1820/// |> ignore() 1821/// uv.run(Default) 1822/// uv.close() 1823/// ``` 1824pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, path_cb : (String) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno

Asynchronously resolves a file path to its absolute canonical form.

This function initiates a path resolution operation that will be handled asynchronously by the event loop. The resolved path eliminates symbolic links, relative path components (like . and ..), and redundant separators to produce an absolute canonical path. When the operation completes, either the success callback or error callback will be invoked depending on the result.

Parameters:

  • self : The event loop instance to schedule the operation on.
  • path : The file path to resolve. Can be any type that implements ToNtbs (convertible to null-terminated byte string), such as String, Bytes, @string.View, or @bytes.View.
  • path_cb : Success callback function that receives the filesystem request handle and the resolved absolute path when the operation succeeds.
  • error_cb : Error callback function that receives the filesystem request handle and the error code when the operation fails.

Throws an error of type Errno if the operation cannot be initiated (e.g., invalid parameters or system resource exhaustion).

Example:

let uv = @uv.Loop::new()
uv.fs_realpath(
  "test/fixtures/example.txt",
  realpath => println("Resolved: \{realpath}"),
  error => println("Failed to resolve path: \{error}")
)
|> ignore()
uv.run(Default)
uv.close()
fs_realpath
(
1825
Loop
self
:
type Loop Bytes
Loop
,
1826
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1827
(String) -> Unit
path_cb
: (
String
String
) ->
Unit
Unit
,
1828
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1829) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1830 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1831 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:

  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
()
1832 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1833
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1834
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1835 } else { 1836 let
@c.Pointer[Byte]
ptr
:
type @c.Pointer[_]
@c.Pointer
[
Byte
Byte
] =
(req : Fs) -> @c.Pointer[Unit]
uv_fs_get_ptr
(
Fs
req
).
(self : @c.Pointer[Unit]) -> @c.Pointer[Byte]
cast
()
1837 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:

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

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

Parameters:

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

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

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1839
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(
@c.Pointer[Byte]
ptr
(@c.Pointer[Byte], Int) -> Byte
[
i])
1840 } 1841 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
1842
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1843
(String) -> Unit
path_cb
(
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
))
1844 } 1845 } 1846 1847 let
Fs
req
=
() -> Fs
uv_fs_make
()
1848 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
)
1849 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1850 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1851 } 1852 return
Fs
req
1853} 1854 1855///| 1856extern "c" fn uv_fs_realpath_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, path :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_realpath_sync"
1857 1858///| 1859pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs) -> String raise Errno
fs_realpath_sync
(
1860
Loop
self
:
type Loop Bytes
Loop
,
1861
Ntbs
path
:

type parameter Ntbs

Ntbs
1862) ->
String
String
raise
type! Errno Status
Errno
{
1863 let
Fs
req
=
() -> Fs
uv_fs_make
()
1864 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes) -> Int
uv_fs_realpath_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
())
1865 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1866 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1867 } 1868 let
@c.Pointer[Byte]
ptr
:
type @c.Pointer[_]
@c.Pointer
[
Byte
Byte
] =
(req : Fs) -> @c.Pointer[Unit]
uv_fs_get_ptr
(
Fs
req
).
(self : @c.Pointer[Unit]) -> @c.Pointer[Byte]
cast
()
1869 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:

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

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

Parameters:

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

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

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1871
@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:

  let buf = @buffer.new()
  buf.write_byte(b'\x41')
  inspect(buf.contents(), content="b\"\\x41\"")
write_byte
(
@c.Pointer[Byte]
ptr
(@c.Pointer[Byte], Int) -> Byte
[
i])
1872 } 1873
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1874 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:

  let buf = @buffer.new()
  buf.write_string("Test")
  inspect(
    buf.contents(),
    content=
      #|b"\x54\x00\x65\x00\x73\x00\x74\x00"
    ,
  )
contents
()
1875
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
)
1876} 1877 1878///| 1879extern "c" fn uv_F_OK() ->
Int
Int
= "moonbit_uv_F_OK"
1880 1881///| 1882let
Int
_F_OK
:
Int
Int
=
() -> Int
uv_F_OK
()
1883 1884///| 1885extern "c" fn uv_R_OK() ->
Int
Int
= "moonbit_uv_R_OK"
1886 1887///| 1888let
Int
_R_OK
:
Int
Int
=
() -> Int
uv_R_OK
()
1889 1890///| 1891extern "c" fn uv_W_OK() ->
Int
Int
= "moonbit_uv_W_OK"
1892 1893///| 1894let
Int
_W_OK
:
Int
Int
=
() -> Int
uv_W_OK
()
1895 1896///| 1897extern "c" fn uv_X_OK() ->
Int
Int
= "moonbit_uv_X_OK"
1898 1899///| 1900let
Int
_X_OK
:
Int
Int
=
() -> Int
uv_X_OK
()
1901 1902///| 1903type AccessFlags
Int
Int
1904 1905///| 1906pub fn
type AccessFlags Int
AccessFlags
::
(read~ : Bool = .., write~ : Bool = .., execute~ : Bool = ..) -> AccessFlags
new
(
1907
Bool
read
~ :
Bool
Bool
= false,
1908
Bool
write
~ :
Bool
Bool
= false,
1909
Bool
execute
~ :
Bool
Bool
= false
1910) ->
type AccessFlags Int
AccessFlags
{
1911 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:

  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:

  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:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
execute
) {
1912 return
Int
_F_OK
1913 } 1914 let mut
Int
flags
= 0
1915 if
Bool
read
{
1916
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_R_OK
1917 } 1918 if
Bool
write
{
1919
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_W_OK
1920 } 1921 if
Bool
execute
{
1922
Int
flags
=
Int
flags
(Int, Int) -> Int
|
Int
_X_OK
1923 } 1924 return
Int
flags
1925} 1926 1927///| 1928extern "c" fn uv_fs_access( 1929 uv :
type Loop Bytes
Loop
,
1930 req :
type Fs
Fs
,
1931 path :
Bytes
Bytes
,
1932 mode :
Int
Int
,
1933 cb : (
type Fs
Fs
) ->
Unit
Unit
1934) ->
Int
Int
= "moonbit_uv_fs_access"
1935 1936///| 1937pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, mode : AccessFlags, access_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_access
(
1938
Loop
self
:
type Loop Bytes
Loop
,
1939
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1940
AccessFlags
mode
:
type AccessFlags Int
AccessFlags
,
1941
() -> Unit
access_cb
: () ->
Unit
Unit
,
1942
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
1943) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
1944 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
1945 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:

  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
()
1946
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
1947 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1948
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
1949 } else { 1950
() -> Unit
access_cb
()
1951 } 1952 } 1953 1954 let
Fs
req
=
() -> Fs
uv_fs_make
()
1955 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
.
(self : AccessFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
(),
(Fs) -> Unit
cb
)
1956 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1957 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1958 } 1959 return
Fs
req
1960} 1961 1962///| 1963extern "c" fn uv_fs_access_sync( 1964 uv :
type Loop Bytes
Loop
,
1965 req :
type Fs
Fs
,
1966 path :
Bytes
Bytes
,
1967 mode :
Int
Int
1968) ->
Int
Int
= "moonbit_uv_fs_access_sync"
1969 1970///| 1971pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, mode : AccessFlags) -> Unit raise Errno
fs_access_sync
(
1972
Loop
self
:
type Loop Bytes
Loop
,
1973
Ntbs
path
:

type parameter Ntbs

Ntbs
,
1974
AccessFlags
mode
:
type AccessFlags Int
AccessFlags
1975) ->
Unit
Unit
raise
type! Errno Status
Errno
{
1976 let
Fs
req
=
() -> Fs
uv_fs_make
()
1977 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, mode : Int) -> Int
uv_fs_access_sync
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
AccessFlags
mode
.
(self : AccessFlags) -> Int

Convert newtype to its underlying type, automatically derived.

inner
())
1978 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
1979 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
1980 } 1981} 1982 1983///| 1984extern "c" fn uv_fs_get_path(req :
type Fs
Fs
) ->
Bytes
Bytes
= "moonbit_uv_fs_get_path"
1985 1986///| 1987fn
type Fs
Fs
::
(req : Fs) -> String
get_path
(
Fs
req
:
type Fs
Fs
) ->
String
String
{
1988 let
Bytes
bytes
=
(req : Fs) -> Bytes
uv_fs_get_path
(
Fs
req
)
1989 return
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
bytes
)
1990} 1991 1992///| 1993extern "c" fn uv_fs_mkdtemp( 1994 uv :
type Loop Bytes
Loop
,
1995 req :
type Fs
Fs
,
1996 template :
Bytes
Bytes
,
1997 cb : (
type Fs
Fs
) ->
Unit
Unit
1998) ->
Int
Int
= "moonbit_uv_fs_mkdtemp"
1999 2000///| 2001pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, template : Ntbs, mkdtemp_cb : (String) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_mkdtemp
(
2002
Loop
self
:
type Loop Bytes
Loop
,
2003
Ntbs
template
:

type parameter Ntbs

Ntbs
,
2004
(String) -> Unit
mkdtemp_cb
: (
String
String
) ->
Unit
Unit
,
2005
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
2006) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
2007 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
2008 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:

  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
()
2009 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2010
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2011
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
2012 } else { 2013 let
String
path
=
Fs
req
.
(req : Fs) -> String
get_path
()
2014
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2015
(String) -> Unit
mkdtemp_cb
(
String
path
)
2016 } 2017 } 2018 2019 let
Fs
req
=
() -> Fs
uv_fs_make
()
2020 let
Int
status
=
(uv : Loop, req : Fs, template : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_mkdtemp
(
Loop
self
,
Fs
req
,
Ntbs
template
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
2021 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2022 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2023 } 2024 return
Fs
req
2025} 2026 2027///| 2028extern "c" fn uv_fs_mkdtemp_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, template :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_mkdtemp_sync"
2029 2030///| 2031pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, template : Ntbs) -> String raise Errno
fs_mkdtemp_sync
(
2032
Loop
self
:
type Loop Bytes
Loop
,
2033
Ntbs
template
:

type parameter Ntbs

Ntbs
2034) ->
String
String
raise
type! Errno Status
Errno
{
2035 let
Fs
req
=
() -> Fs
uv_fs_make
()
2036 let
Int
status
=
(uv : Loop, req : Fs, template : Bytes) -> Int
uv_fs_mkdtemp_sync
(
Loop
self
,
Fs
req
,
Ntbs
template
.
(Ntbs) -> Bytes
to_ntbs
())
2037 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2038 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2039 } 2040 let
String
path
=
Fs
req
.
(req : Fs) -> String
get_path
()
2041
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2042 return
String
path
2043} 2044 2045///| 2046extern "c" fn uv_fs_mkstemp( 2047 uv :
type Loop Bytes
Loop
,
2048 req :
type Fs
Fs
,
2049 template :
Bytes
Bytes
,
2050 cb : (
type Fs
Fs
) ->
Unit
Unit
2051) ->
Int
Int
= "moonbit_uv_fs_mkstemp"
2052 2053///| 2054pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, template : Ntbs, mkstemp_cb : (String) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_mkstemp
(
2055
Loop
self
:
type Loop Bytes
Loop
,
2056
Ntbs
template
:

type parameter Ntbs

Ntbs
,
2057
(String) -> Unit
mkstemp_cb
: (
String
String
) ->
Unit
Unit
,
2058
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
2059) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
2060 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
2061 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:

  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
()
2062 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2063
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
2064 } else { 2065 let
String
path
=
Fs
req
.
(req : Fs) -> String
get_path
()
2066
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2067
(String) -> Unit
mkstemp_cb
(
String
path
)
2068 } 2069 } 2070 2071 let
Fs
req
=
() -> Fs
uv_fs_make
()
2072 let
Int
status
=
(uv : Loop, req : Fs, template : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_mkstemp
(
Loop
self
,
Fs
req
,
Ntbs
template
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
2073 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2074 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2075 } 2076 return
Fs
req
2077} 2078 2079///| 2080extern "c" fn uv_fs_mkstemp_sync(uv :
type Loop Bytes
Loop
, req :
type Fs
Fs
, template :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_fs_mkstemp_sync"
2081 2082///| 2083pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, template : Ntbs) -> String raise Errno
fs_mkstemp_sync
(
2084
Loop
self
:
type Loop Bytes
Loop
,
2085
Ntbs
template
:

type parameter Ntbs

Ntbs
2086) ->
String
String
raise
type! Errno Status
Errno
{
2087 let
Fs
req
=
() -> Fs
uv_fs_make
()
2088 let
Int
status
=
(uv : Loop, req : Fs, template : Bytes) -> Int
uv_fs_mkstemp_sync
(
Loop
self
,
Fs
req
,
Ntbs
template
.
(Ntbs) -> Bytes
to_ntbs
())
2089 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2090
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2091 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2092 } 2093 let
String
path
=
Fs
req
.
(req : Fs) -> String
get_path
()
2094
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2095 return
String
path
2096} 2097 2098///| 2099#external 2100type Dir 2101 2102///| 2103extern "c" fn uv_dir_set(dir :
type Dir
Dir
, entries :
type @c.Pointer[_]
@c.Pointer
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
], n :
Int
Int
) = "moonbit_uv_dir_set"
2104 2105///| 2106extern "c" fn uv_fs_opendir( 2107 uv :
type Loop Bytes
Loop
,
2108 req :
type Fs
Fs
,
2109 path :
Bytes
Bytes
,
2110 cb : (
type Fs
Fs
) ->
Unit
Unit
2111) ->
Int
Int
= "moonbit_uv_fs_opendir"
2112 2113///| 2114pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
type Loop Bytes
Loop
::
(self : Loop, path : Ntbs, opendir_cb : (Dir) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_opendir
(
2115
Loop
self
:
type Loop Bytes
Loop
,
2116
Ntbs
path
:

type parameter Ntbs

Ntbs
,
2117
(Dir) -> Unit
opendir_cb
: (
type Dir
Dir
) ->
Unit
Unit
,
2118
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
2119) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
2120 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
2121 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:

  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
()
2122 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2123
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2124
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
2125 } else { 2126 let
Dir
dir
=
(req : Fs) -> Dir
uv_fs_get_dir
(
Fs
req
)
2127
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2128
(Dir) -> Unit
opendir_cb
(
Dir
dir
)
2129 } 2130 } 2131 2132 let
Fs
req
=
() -> Fs
uv_fs_make
()
2133 let
Int
status
=
(uv : Loop, req : Fs, path : Bytes, cb : (Fs) -> Unit) -> Int
uv_fs_opendir
(
Loop
self
,
Fs
req
,
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
(),
(Fs) -> Unit
cb
)
2134 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2135 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2136 } 2137 return
Fs
req
2138} 2139 2140///| 2141extern "c" fn uv_fs_readdir( 2142 uv :
type Loop Bytes
Loop
,
2143 req :
type Fs
Fs
,
2144 dir :
type Dir
Dir
,
2145 cb : (
type Fs
Fs
) ->
Unit
Unit
2146) ->
Int
Int
= "moonbit_uv_fs_readdir"
2147 2148///| 2149pub fn
type Loop Bytes
Loop
::
(self : Loop, dir : Dir, n : Int, readdir_cb : (Array[Dirent]) -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_readdir
(
2150
Loop
self
:
type Loop Bytes
Loop
,
2151
Dir
dir
:
type Dir
Dir
,
2152
Int
n
:
Int
Int
,
2153
(Array[Dirent]) -> Unit
readdir_cb
: (
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Dirent {
  name: String
  type_: DirentType
}
Dirent
]) ->
Unit
Unit
,
2154
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
2155) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
2156 let
@c.Pointer[Dirent]
uv_dirents
=
(n : Int) -> @c.Pointer[Dirent]
uv_dirent_make
(
Int
n
)
2157 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
2158 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:

  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
()
2159 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2160
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2161
(dir : Dir, entries : @c.Pointer[Dirent], n : Int) -> Unit
uv_dir_set
(
Dir
dir
,
() -> @c.Pointer[Dirent]
@c.null
(), 0)
2162
(dirent : @c.Pointer[Dirent]) -> Unit
uv_dirent_delete
(
@c.Pointer[Dirent]
uv_dirents
)
2163
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
2164 } else { 2165 let
Array[Dirent]
dirents
=
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
::
(Int, (Int) -> Dirent) -> Array[Dirent]

Creates a new array of the specified length, where each element is initialized using an index-based initialization function.

Parameters:

  • length : The length of the new array. If length is less than or equal to 0, returns an empty array.
  • initializer : A function that takes an index (starting from 0) and returns a value of type T. This function is called for each index to initialize the corresponding element.

Returns a new array of type Array[T] with the specified length, where each element is initialized using the provided function.

Example:

  let arr = Array::makei(3, fn(i) { i * 2 })
  inspect(arr, content="[0, 2, 4]")
makei
(
Int
status
,
@c.Pointer[Dirent]
uv_dirents
[_])
2166
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2167
(dir : Dir, entries : @c.Pointer[Dirent], n : Int) -> Unit
uv_dir_set
(
Dir
dir
,
() -> @c.Pointer[Dirent]
@c.null
(), 0)
2168
(dirent : @c.Pointer[Dirent]) -> Unit
uv_dirent_delete
(
@c.Pointer[Dirent]
uv_dirents
)
2169
(Array[Dirent]) -> Unit
readdir_cb
(
Array[Dirent]
dirents
)
2170 } 2171 } 2172 2173 let
Fs
req
=
() -> Fs
uv_fs_make
()
2174
(dir : Dir, entries : @c.Pointer[Dirent], n : Int) -> Unit
uv_dir_set
(
Dir
dir
,
@c.Pointer[Dirent]
uv_dirents
,
Int
n
)
2175 let
Int
status
=
(uv : Loop, req : Fs, dir : Dir, cb : (Fs) -> Unit) -> Int
uv_fs_readdir
(
Loop
self
,
Fs
req
,
Dir
dir
,
(Fs) -> Unit
cb
)
2176 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2177 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2178 } 2179 return
Fs
req
2180} 2181 2182///| 2183extern "c" fn uv_fs_closedir( 2184 uv :
type Loop Bytes
Loop
,
2185 req :
type Fs
Fs
,
2186 dir :
type Dir
Dir
,
2187 cb : (
type Fs
Fs
) ->
Unit
Unit
2188) ->
Int
Int
= "moonbit_uv_fs_closedir"
2189 2190///| 2191pub fn
type Loop Bytes
Loop
::
(self : Loop, dir : Dir, closedir_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Fs raise Errno
fs_closedir
(
2192
Loop
self
:
type Loop Bytes
Loop
,
2193
Dir
dir
:
type Dir
Dir
,
2194
() -> Unit
closedir_cb
: () ->
Unit
Unit
,
2195
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
2196) ->
type Fs
Fs
raise
type! Errno Status
Errno
{
2197 fn
(Fs) -> Unit
cb
(
Fs
req
:
type Fs
Fs
) {
2198 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:

  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
()
2199
(req : Fs) -> Unit
uv_fs_req_cleanup
(
Fs
req
)
2200 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2201
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
2202 } else { 2203
() -> Unit
closedir_cb
()
2204 } 2205 } 2206 2207 let
Fs
req
=
() -> Fs
uv_fs_make
()
2208 let
Int
status
=
(uv : Loop, req : Fs, dir : Dir, cb : (Fs) -> Unit) -> Int
uv_fs_closedir
(
Loop
self
,
Fs
req
,
Dir
dir
,
(Fs) -> Unit
cb
)
2209 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
2210 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
2211 } 2212 return
Fs
req
2213} 2214