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 Work
17
18///|
19extern "c" fn uv_work_make() -> 
type Work
Work
= "moonbit_uv_work_make"
20 21///| 22extern "c" fn uv_queue_work( 23 uv :
type Loop Bytes
Loop
,
24 req :
type Work
Work
,
25 work_cb : (
type Work
Work
) ->
Unit
Unit
,
26 after_cb : (
type Work
Work
,
Int
Int
) ->
Unit
Unit
27) ->
Int
Int
= "moonbit_uv_queue_work"
28 29///| 30pub fn
type Loop Bytes
Loop
::
(self : Loop, work_cb : () -> Unit, after_cb : () -> Unit, error_cb : (Errno) -> Unit) -> Work raise Errno
queue_work
(
31
Loop
self
:
type Loop Bytes
Loop
,
32
() -> Unit
work_cb
: () ->
Unit
Unit
,
33
() -> Unit
after_cb
: () ->
Unit
Unit
,
34
(Errno) -> Unit
error_cb
: (
type! Errno Status
Errno
) ->
Unit
Unit
35) ->
type Work
Work
raise
type! Errno Status
Errno
{
36 let
Work
req
=
() -> Work
uv_work_make
()
37 let
Int
status
=
(uv : Loop, req : Work, work_cb : (Work) -> Unit, after_cb : (Work, Int) -> Unit) -> Int
uv_queue_work
(
Loop
self
,
Work
req
, fn(_) {
() -> Unit
work_cb
() }, fn(_,
Int
status
) {
38 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
39
(Errno) -> Unit
error_cb
(
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
))
40 } else { 41
() -> Unit
after_cb
()
42 } 43 }) 44 if
Int
status
(self_ : Int, other : Int) -> Bool
<
0 {
45 raise
type! Errno Status
Errno
::
(errno : Int) -> Errno
of_int
(
Int
status
)
46 } 47
Work
req
48} 49 50///| 51pub impl
trait ToReq {
  to_req(Self) -> Req
}
ToReq
for
type Work
Work
with to_req(self :
type Work
Work
) ->
type Req
Req
= "%identity"
52 53///| 54pub impl
trait Cancelable {
  cancel(Self) -> Unit raise Errno
}
Cancelable
for
type Work
Work
55