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///|
16extern "c" fn uv_os_uname(buffer : 
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_os_uname"
17 18///| 19struct Utsname { 20
String
sysname
:
String
String
21
String
release
:
String
String
22
String
version
:
String
String
23
String
machine
:
String
String
24} 25 26///| 27pub fn
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
::
(self : Utsname) -> String
sysname
(
Utsname
self
:
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
) ->
String
String
{
28
Utsname
self
.
String
sysname
29} 30 31///| 32pub fn
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
::
(self : Utsname) -> String
release
(
Utsname
self
:
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
) ->
String
String
{
33
Utsname
self
.
String
release
34} 35 36///| 37pub fn
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
::
(self : Utsname) -> String
version
(
Utsname
self
:
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
) ->
String
String
{
38
Utsname
self
.
String
version
39} 40 41///| 42pub fn
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
::
(self : Utsname) -> String
machine
(
Utsname
self
:
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
) ->
String
String
{
43
Utsname
self
.
String
machine
44} 45 46///| 47/// Retrieves system information including the system name, release, version, and 48/// machine architecture. 49/// 50/// Returns a `Utsname` structure containing system identification information. 51/// 52/// Throws an error of type `Errno` if the underlying system call fails. 53/// 54/// Example: 55/// 56/// ```moonbit 57/// let info = @uv.os_uname() 58/// println(info.sysname()) 59/// println(info.release()) 60/// println(info.machine()) 61/// ``` 62pub fn
() -> Utsname raise Errno

Retrieves system information including the system name, release, version, and machine architecture.

Returns a Utsname structure containing system identification information.

Throws an error of type Errno if the underlying system call fails.

Example:

let info = @uv.os_uname()
println(info.sysname())
println(info.release())
println(info.machine())
os_uname
() ->
struct Utsname {
  sysname: String
  release: String
  version: String
  machine: String
}
Utsname
raise
type! Errno Status
Errno
{
63 fn
(@bytes.View) -> String
decode
(
@bytes.View
bytes
:
type @bytes.View

A @bytes.View represents a view into a section of a Bytes without copying the data.

Example

  let bs = b"\x00\x01\x02\x03\x04\x05"
  let bv = bs[1:4]
  assert_eq(bv.length(), 3)
  assert_eq(bv[0], b'\x01')
  assert_eq(bv[1], b'\x02')
  assert_eq(bv[2], b'\x03')
@bytes.View
) ->
String
String
{
64 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
()
65 for
Int
i
in
Int
0
..<
@bytes.View
bytes
.
(self : @bytes.View) -> Int

Returns the number of bytes in the view.

Parameters:

  • bytes_view : The view of a byte sequence.

Returns an integer representing the length of the view.

Example:

  let bytes = b"\x00\x01\x02\x03\x04"
  let view = bytes[2:4]
  inspect(view.length(), content="2")
length
() {
66 let
Byte
byte
=
@bytes.View
bytes
(@bytes.View, Int) -> Byte

Retrieves a byte from the view at the specified index.

Parameters:

  • self : The bytes view to retrieve the byte from.
  • index : The position in the view from which to retrieve the byte.

Returns the byte at the specified index if the index is valid.

Example:

  let bytes = b"\x01\x02\x03\x04\x05"
  let view = bytes[1:4] // view contains [0x02, 0x03, 0x04]
  inspect(view[1], content="b'\\x03'")
[
i]
67 if
Byte
byte
(self : Byte, that : Byte) -> Bool

Compares two Byte values for equality.

Parameters:

  • self : The first Byte value to compare.
  • that : The second Byte value to compare.

Returns true if the two Byte values are equal, otherwise false.

==
0 {
68 break 69 } 70
@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
(
Byte
byte
)
71 } 72 return
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
@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
())
73 } 74 75 let
Bytes
buffer
=
(Int, Byte) -> Bytes
Bytes::
(len : Int, init : Byte) -> Bytes

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

Parameters:

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

Example:

  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
(256
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

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

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
4, 0)
76 let
Int
status
=
(buffer : Bytes) -> Int
uv_os_uname
(
Bytes
buffer
)
77 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
78 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
79 } 80 let
String
sysname
=
(@bytes.View) -> String
decode
([..
Bytes
buffer
[0:256]])
81 let
String
release
=
(@bytes.View) -> String
decode
([..
Bytes
buffer
[256:512]])
82 let
String
version
=
(@bytes.View) -> String
decode
([..
Bytes
buffer
[512:768]])
83 let
String
machine
=
(@bytes.View) -> String
decode
([..
Bytes
buffer
[768:1024]])
84 {
String
sysname
,
String
release
,
String
version
,
String
machine
}
85} 86 87///| 88extern "c" fn uv_os_tmpdir(bytes :
Bytes
Bytes
, size :
type FixedArray[A]
FixedArray
[
Int
Int
]) ->
Int
Int
= "moonbit_uv_os_tmpdir"
89 90///| 91/// Retrieves the path to the system's temporary directory. 92/// 93/// Returns a `String` containing the path to the temporary directory where 94/// temporary files can be created. 95/// 96/// Throws an error of type `Errno` if the underlying system call fails or if 97/// there are insufficient resources to complete the operation. 98/// 99/// Example: 100/// 101/// ```moonbit 102/// println(@uv.os_tmpdir()) 103/// ``` 104pub fn
() -> String raise Errno

Retrieves the path to the system's temporary directory.

Returns a String containing the path to the temporary directory where temporary files can be created.

Throws an error of type Errno if the underlying system call fails or if there are insufficient resources to complete the operation.

Example:

println(@uv.os_tmpdir())
os_tmpdir
() ->
String
String
raise
type! Errno Status
Errno
{
105 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)
106 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
()]
107 let mut
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_tmpdir
(
Bytes
buffer
,
FixedArray[Int]
length
)
108 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
{
109
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]
(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, 0)
110
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_tmpdir
(
Bytes
buffer
,
FixedArray[Int]
length
)
111 } 112 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
113 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
114 } 115
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
buffer
[:
FixedArray[Int]
length
[0]])
116} 117 118///| 119extern "c" fn uv_exepath(bytes :
Bytes
Bytes
, size :
type FixedArray[A]
FixedArray
[
Int
Int
]) ->
Int
Int
= "moonbit_uv_exepath"
120 121///| 122pub fn
() -> String raise Errno
exepath
() ->
String
String
raise
type! Errno Status
Errno
{
123 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
(256, 0)
124 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
()]
125 let mut
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_exepath
(
Bytes
buffer
,
FixedArray[Int]
length
)
126 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
{
127
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)
128
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_exepath
(
Bytes
buffer
,
FixedArray[Int]
length
)
129 } 130 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
131 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
132 } 133
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
buffer
[:
FixedArray[Int]
length
[0]])
134} 135 136///| 137extern "c" fn uv_os_homedir(bytes :
Bytes
Bytes
, size :
type FixedArray[A]
FixedArray
[
Int
Int
]) ->
Int
Int
= "moonbit_uv_os_homedir"
138 139///| 140pub fn
() -> String raise Errno
os_homedir
() ->
String
String
raise
type! Errno Status
Errno
{
141 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
(256, 0)
142 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
()]
143 let mut
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_homedir
(
Bytes
buffer
,
FixedArray[Int]
length
)
144 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
{
145
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)
146
Int
status
=
(bytes : Bytes, size : FixedArray[Int]) -> Int
uv_os_homedir
(
Bytes
buffer
,
FixedArray[Int]
length
)
147 } 148 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
149 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
150 } 151
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
Bytes
buffer
[:
FixedArray[Int]
length
[0]])
152} 153 154///| 155extern "c" fn uv_chdir(path :
Bytes
Bytes
) ->
Int
Int
= "moonbit_uv_chdir"
156 157///| 158pub fn[Ntbs :
trait ToNtbs {
  to_ntbs(Self) -> Bytes
}

Convertible to null-terminated byte strings (NTBS).

ToNtbs
]
(path : Ntbs) -> Unit raise Errno
chdir
(
Ntbs
path
:

type parameter Ntbs

Ntbs
) ->
Unit
Unit
raise
type! Errno Status
Errno
{
159 let
Int
status
=
(path : Bytes) -> Int
uv_chdir
(
Ntbs
path
.
(Ntbs) -> Bytes
to_ntbs
())
160 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
161 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
162 } 163} 164 165///| 166pub struct CpuInfo { 167
String
model
:
String
String
168
Int
speed
:
Int
Int
169
CpuTimes
cpu_times
:
struct CpuTimes {
  user: UInt64
  nice: UInt64
  sys: UInt64
  idle: UInt64
  irq: UInt64
}
CpuTimes
170} derive(
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
,
trait ToJson {
  to_json(Self) -> Json
}

Trait for types that can be converted to Json

ToJson
)
171 172///| 173pub struct CpuTimes { 174
UInt64
user
:
UInt64
UInt64
175
UInt64
nice
:
UInt64
UInt64
176
UInt64
sys
:
UInt64
UInt64
177
UInt64
idle
:
UInt64
UInt64
178
UInt64
irq
:
UInt64
UInt64
179} derive(
trait Show {
  output(Self, &Logger) -> Unit
  to_string(Self) -> String
}

Trait for types that can be converted to String

Show
,
trait ToJson {
  to_json(Self) -> Json
}

Trait for types that can be converted to Json

ToJson
)
180 181///| 182extern "c" fn uv_cpu_info( 183 cpu_infos :
type FixedArray[A]
FixedArray
[@c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
]],
184 count :
type FixedArray[A]
FixedArray
[
Int
Int
]
185) ->
Int
Int
= "moonbit_uv_cpu_info"
186 187///| 188extern "c" fn uv_cpu_info_get_model( 189 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
190 index :
Int
Int
191) -> @c.Pointer[
Byte
Byte
] = "moonbit_uv_cpu_info_get_model"
192 193///| 194extern "c" fn uv_cpu_info_get_speed( 195 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
196 index :
Int
Int
197) ->
Int
Int
= "moonbit_uv_cpu_info_get_speed"
198 199///| 200extern "c" fn uv_cpu_info_get_cpu_times_user( 201 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
202 index :
Int
Int
203) ->
UInt64
UInt64
= "moonbit_uv_cpu_info_get_cpu_times_user"
204 205///| 206extern "c" fn uv_cpu_info_get_cpu_times_nice( 207 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
208 index :
Int
Int
209) ->
UInt64
UInt64
= "moonbit_uv_cpu_info_get_cpu_times_nice"
210 211///| 212extern "c" fn uv_cpu_info_get_cpu_times_sys( 213 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
214 index :
Int
Int
215) ->
UInt64
UInt64
= "moonbit_uv_cpu_info_get_cpu_times_sys"
216 217///| 218extern "c" fn uv_cpu_info_get_cpu_times_idle( 219 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
220 index :
Int
Int
221) ->
UInt64
UInt64
= "moonbit_uv_cpu_info_get_cpu_times_idle"
222 223///| 224extern "c" fn uv_cpu_info_get_cpu_times_irq( 225 cpu_info : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
226 index :
Int
Int
227) ->
UInt64
UInt64
= "moonbit_uv_cpu_info_get_cpu_times_irq"
228 229///| 230impl @c.Load for
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
with
(?, Int) -> CpuInfo
load
(
?
self
: @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
],
Int
index
:
Int
Int
) ->
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
{
231 let
String
model
= {
232 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
()
233 let
?
pointer
=
(cpu_info : ?, index : Int) -> ?
uv_cpu_info_get_model
(
?
self
,
Int
index
)
234 for
Int
i
= 0;
?
pointer
(Int) -> Int
[
i]
(x : Int, y : Int) -> Bool
!=
0;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

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

Parameters:

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

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

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
235
@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
(
?
pointer
(Int) -> Byte
[
i])
236 } 237
(encoding~ : @encoding.Encoding, input : @bytes.View) -> String
@encoding.decode_lossy
(
@encoding.Encoding
encoding
=
@encoding.Encoding
UTF8
,
@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
())
238 } 239 let
Int
speed
=
(cpu_info : ?, index : Int) -> Int
uv_cpu_info_get_speed
(
?
self
,
Int
index
)
240 let
CpuTimes
cpu_times
= {
241
UInt64
user
:
(cpu_info : ?, index : Int) -> UInt64
uv_cpu_info_get_cpu_times_user
(
?
self
,
Int
index
),
242
UInt64
nice
:
(cpu_info : ?, index : Int) -> UInt64
uv_cpu_info_get_cpu_times_nice
(
?
self
,
Int
index
),
243
UInt64
sys
:
(cpu_info : ?, index : Int) -> UInt64
uv_cpu_info_get_cpu_times_sys
(
?
self
,
Int
index
),
244
UInt64
idle
:
(cpu_info : ?, index : Int) -> UInt64
uv_cpu_info_get_cpu_times_idle
(
?
self
,
Int
index
),
245
UInt64
irq
:
(cpu_info : ?, index : Int) -> UInt64
uv_cpu_info_get_cpu_times_irq
(
?
self
,
Int
index
),
246 } 247 {
String
model
,
Int
speed
,
CpuTimes
cpu_times
}
248} 249 250///| 251extern "c" fn uv_free_cpu_info(cpu_infos : @c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
], count :
Int
Int
) = "moonbit_uv_free_cpu_info"
252 253///| 254/// Returns current CPU information. 255/// 256/// Example: 257/// 258/// ```moonbit 259/// let cpu_infos = @uv.cpu_info() 260/// println(cpu_infos) 261/// ``` 262pub fn
() -> Array[CpuInfo] raise Errno

Returns current CPU information.

Example:

let cpu_infos = @uv.cpu_info()
println(cpu_infos)
cpu_info
() ->
type Array[T]

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

Array
[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
] raise
type! Errno Status
Errno
{
263 let
FixedArray[?]
cpu_infos
FixedArray[?]
:
type FixedArray[A]
FixedArray
FixedArray[?]
[@c.Pointer[
struct CpuInfo {
  model: String
  speed: Int
  cpu_times: CpuTimes
}
CpuInfo
FixedArray[?]
]]
= [
() -> ?
@c.null
()]
264 let
FixedArray[Int]
count
:
type FixedArray[A]
FixedArray
[
Int
Int
] = [0]
265 let
Int
status
=
(cpu_infos : FixedArray[?], count : FixedArray[Int]) -> Int
uv_cpu_info
(
FixedArray[?]
cpu_infos
,
FixedArray[Int]
count
)
266 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
267 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
268 } 269 let
?
cpu_infos
=
FixedArray[?]
cpu_infos
[0]
270 let
Int
count
=
FixedArray[Int]
count
[0]
271 let
Array[CpuInfo]
result
=
type Array[T]

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

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

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
count
,
?
cpu_infos
[_])
272
(cpu_infos : ?, count : Int) -> Unit
uv_free_cpu_info
(
?
cpu_infos
,
Int
count
)
273
Array[CpuInfo]
result
274} 275