1///|
2type Pipe
3
4///|
5pub fn type Pipe
Pipe::to_handle(self : type Pipe
Pipe) -> type Handle
Handle = "%identity"
6
7///|
8pub fn type Pipe
Pipe::to_stream(self : type Pipe
Pipe) -> type Stream
Stream = "%identity"
9
10///|
11pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle for type Pipe
Pipe with (self : Pipe) -> Handle
to_handle(Pipe
self) {
12 Pipe
self.(self : Pipe) -> Handle
to_handle()
13}
14
15///|
16pub impl trait ToHandle {
to_handle(Self) -> Handle
of_handle(Handle) -> Self
close(Self, (Self) -> Unit) -> Unit
is_closing(Self) -> Bool
}
ToHandle for type Pipe
Pipe with of_handle(self) = "%identity"
17
18///|
19pub impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream for type Pipe
Pipe with (self : Pipe) -> Stream
to_stream(Pipe
self) {
20 Pipe
self.(self : Pipe) -> Stream
to_stream()
21}
22
23///|
24pub impl trait ToStream {
to_stream(Self) -> Stream
of_stream(Stream) -> Self
read_start(Self, (Self, Int) -> @bytes.View, (Self, Int, @bytes.View) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
read_stop(Self) -> Unit!Errno
write(Self, Array[@bytes.View], (Write) -> Unit, (Write, Errno) -> Unit) -> Unit!Errno
listen(Self, Int, (Self) -> Unit, (Self, Errno) -> Unit) -> Unit!Errno
is_readable(Self) -> Bool
is_writable(Self) -> Bool
shutdown(Self, (Shutdown) -> Unit, (Shutdown, Errno) -> Unit) -> Unit!Errno
}
ToStream for type Pipe
Pipe with of_stream(self) = "%identity"
25
26///|
27extern "c" fn uv_pipe_make() -> type Pipe
Pipe = "moonbit_uv_pipe_make"
28
29///|
30#borrow(uv, pipe)
31extern "c" fn uv_pipe_init(uv : type Loop Bytes
Loop, pipe : type Pipe
Pipe, ipc : Bool
Bool) -> Int
Int = "moonbit_uv_pipe_init"
32
33///|
34extern "c" fn uv_pipe_bind(pipe : type Pipe
Pipe, name : Bytes
Bytes, flags : UInt
UInt) -> Int
Int = "moonbit_uv_pipe_bind"
35
36///|
37extern "c" fn uv_pipe(
38 fds : type FixedArray[A]
FixedArray[Int
Int],
39 read_flags : Int
Int,
40 write_flags : Int
Int
41) -> Int
Int = "moonbit_uv_pipe"
42
43///|
44pub fn type Pipe
Pipe::(self : Loop, ipc~ : Bool = ..) -> Pipe!Errno
new(Loop
self : type Loop Bytes
Loop, Bool
ipc~ : Bool
Bool = false) -> type Pipe
Pipe!type! Errno Status
Errno {
45 let Pipe
pipe = () -> Pipe
uv_pipe_make()
46 let Int
status = (uv : Loop, pipe : Pipe, ipc : Bool) -> Int
uv_pipe_init(Loop
self, Pipe
pipe, Bool
ipc)
47 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
48 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
49 }
50 return Pipe
pipe
51}
52
53///|
54type PipeBindFlags UInt
UInt
55
56///|
57const UInt
PIPE_NO_TRUNCATE : UInt
UInt = 1U (self : UInt, shift : Int) -> UInt
Performs a left shift operation on an unsigned 32-bit integer. Each bit in
the integer is shifted left by the specified number of positions, and zeros
are filled in from the right.
Parameters:
self : The unsigned 32-bit integer to be shifted.
shift : The number of positions to shift. Only the least significant 5
bits are used, effectively making the shift count always between 0 and 31.
Returns a new unsigned 32-bit integer that is the result of shifting self
left by shift positions.
Example:
test "UInt::op_shl" {
let x = 1U
inspect(x << 3, content="8") // Binary: 1 -> 1000
let y = 0xFFFFFFFFU
inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
}
<< 0
58
59///|
60pub fn type PipeBindFlags UInt
PipeBindFlags::(truncate~ : Bool = ..) -> PipeBindFlags
new(Bool
truncate~ : Bool
Bool = true) -> type PipeBindFlags UInt
PipeBindFlags {
61 let mut UInt
flags = 0U
62 if (x : Bool) -> Bool
Performs logical negation on a boolean value.
Parameters:
value : The boolean value to negate.
Returns the logical NOT of the input value: true if the input is false,
and false if the input is true.
Example:
test "not" {
inspect(not(true), content="false")
inspect(not(false), content="true")
}
not(Bool
truncate) {
63 UInt
flags = UInt
flags (UInt, UInt) -> UInt
| UInt
PIPE_NO_TRUNCATE
64 }
65 return UInt
flags
66}
67
68///|
69pub fn[Ntbs : trait ToNtbs {
to_ntbs(Self) -> Bytes
}
ToNtbs] type Pipe
Pipe::(self : Pipe, name : Ntbs, flags : PipeBindFlags) -> Unit!Errno
bind(
70 Pipe
self : type Pipe
Pipe,
71 Ntbs
name : type parameter Ntbs
Ntbs,
72 PipeBindFlags
flags : type PipeBindFlags UInt
PipeBindFlags
73) -> Unit
Unit!type! Errno Status
Errno {
74 let Bytes
name = Ntbs
name.(Ntbs) -> Bytes
to_ntbs()
75 let Int
status = (pipe : Pipe, name : Bytes, flags : UInt) -> Int
uv_pipe_bind(Pipe
self, Bytes
name, PipeBindFlags
flags.UInt
_)
76 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
77 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
78 }
79}
80
81///|
82pub fn (read_flags : Int, write_flags : Int) -> (File, File)!Errno
pipe(Int
read_flags : Int
Int, Int
write_flags : Int
Int) -> (type File Int
File, type File Int
File)!type! Errno Status
Errno {
83 let FixedArray[Int]
fds : type FixedArray[A]
FixedArray[Int
Int] = [0, 0]
84 let Int
status = (fds : FixedArray[Int], read_flags : Int, write_flags : Int) -> Int
uv_pipe(FixedArray[Int]
fds, Int
read_flags, Int
write_flags)
85 if Int
status (self_ : Int, other : Int) -> Bool
< 0 {
86 raise type! Errno Status
Errno::(errno : Int) -> Errno
of_int(Int
status)
87 }
88 return ((Int) -> File
File(FixedArray[Int]
fds[0]), (Int) -> File
File(FixedArray[Int]
fds[1]))
89}
90