From 07eaba73cccbf2f57bd519ca91470c58b947e2f5 Mon Sep 17 00:00:00 2001 From: Hongbo Zhang Date: Sat, 16 May 2026 17:03:29 +0800 Subject: [PATCH] attach #valtype to more types and expose Coroutine struct fields - Add `#valtype` to `Task[X]` in task.mbt for efficient value passing - Add `#valtype` to `Cond` in cond_var/cond_var.mbt - Make `Coroutine` struct `pub` in internal/coroutine, exposing its fields (coro_id, state, shielded, cancelled, ready, downstream) - Remove `priv` from `State` enum in coroutine.mbt so it is visible as part of the public Coroutine struct definition - Regenerate pkg.generated.mbti for coroutine package Types like Semaphore, Queue, and socket handle types (Tcp, TcpServer, etc.) cannot be #valtype due to mutable fields or abstract IoHandle field type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/cond_var/cond_var.mbt | 1 + src/internal/coroutine/coroutine.mbt | 4 ++-- src/internal/coroutine/pkg.generated.mbti | 12 +++++++++++- src/task.mbt | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/cond_var/cond_var.mbt b/src/cond_var/cond_var.mbt index 4de58309..c3d09b19 100644 --- a/src/cond_var/cond_var.mbt +++ b/src/cond_var/cond_var.mbt @@ -30,6 +30,7 @@ priv struct Waiter { ///| /// A condition variable that can be used for synchronization between tasks. +#valtype struct Cond { waiters : @deque.Deque[Waiter] } diff --git a/src/internal/coroutine/coroutine.mbt b/src/internal/coroutine/coroutine.mbt index 64f9832a..33994480 100644 --- a/src/internal/coroutine/coroutine.mbt +++ b/src/internal/coroutine/coroutine.mbt @@ -13,7 +13,7 @@ // limitations under the License. ///| -priv enum State { +enum State { Done Fail(Error) Running @@ -21,7 +21,7 @@ priv enum State { } ///| -struct Coroutine { +pub struct Coroutine { coro_id : Int mut state : State mut shielded : Bool diff --git a/src/internal/coroutine/pkg.generated.mbti b/src/internal/coroutine/pkg.generated.mbti index 5b50dcd2..2aa8e2a6 100644 --- a/src/internal/coroutine/pkg.generated.mbti +++ b/src/internal/coroutine/pkg.generated.mbti @@ -3,6 +3,7 @@ package "moonbitlang/async/internal/coroutine" import { "moonbitlang/core/debug", + "moonbitlang/core/set", } // Values @@ -32,7 +33,14 @@ pub(all) suberror Cancelled derive(@debug.Debug) pub impl Show for Cancelled // Types and methods -type Coroutine +pub struct Coroutine { + coro_id : Int + mut state : State + mut shielded : Bool + mut cancelled : Bool + mut ready : Bool + downstream : @set.Set[Coroutine] +} pub fn Coroutine::cancel(Self) -> Unit pub fn Coroutine::check_error(Self) -> Unit raise pub fn Coroutine::unwrap(Self) -> Unit raise @@ -41,6 +49,8 @@ pub fn Coroutine::wake(Self) -> Unit pub impl Eq for Coroutine pub impl Hash for Coroutine +type State + // Type aliases // Traits diff --git a/src/task.mbt b/src/task.mbt index c83282fb..48f90cc1 100644 --- a/src/task.mbt +++ b/src/task.mbt @@ -15,6 +15,7 @@ ///| /// `Task[X]` represents a running task with result type `X`, /// it can be used to wait and retrieve the result value of the task. +#valtype struct Task[X] { value : Ref[X?] coro : @coroutine.Coroutine