Skip to content

attach #valtype to more types and expose Coroutine struct fields#372

Draft
bobzhang wants to merge 1 commit into
mainfrom
feature/valtype-more-types
Draft

attach #valtype to more types and expose Coroutine struct fields#372
bobzhang wants to merge 1 commit into
mainfrom
feature/valtype-more-types

Conversation

@bobzhang
Copy link
Copy Markdown
Contributor

@bobzhang bobzhang commented May 16, 2026

Note this is not plan to be merged.

I am thinking we need an attribute like

#allow_in_valtype
struct Coroutine {
   ...
}

cc @Yu-zh

Summary

This PR extends the use of #valtype to more types and exposes additional struct fields in the public API.

Changes

  • Task[X] — Added #valtype, enabling the struct to be passed as values (two reference fields: value: Ref[X?] and coro: Coroutine)
  • Cond (cond_var) — Added #valtype; this struct has a single non-mutable field (waiters: Deque) so it qualifies
  • Coroutine (internal/coroutine) — Changed from opaque type Coroutine to pub struct Coroutine, exposing its fields (coro_id, state, shielded, cancelled, ready, downstream) for downstream use and reflection
  • State enum — Removed priv so it is accessible as part of the public Coroutine struct definition
  • Regenerated pkg.generated.mbti for the coroutine package

Why not more types?

Several candidates were evaluated but cannot be #valtype due to language constraints:

  • Semaphore, Queue[X] — have mut fields; #valtype disallows mutable fields
  • Tcp, TcpServer, UdpClient, UdpServer, RawFd — all hold a @event_loop.IoHandle field which is an abstract type; #valtype disallows abstract type fields

Testing

moon check passes with no errors.

- 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>
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 312

Coverage remained the same at 79.202%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4183
Covered Lines: 3313
Line Coverage: 79.2%
Coverage Strength: 59549.82 hits per line

💛 - Coveralls

@Guest0x0
Copy link
Copy Markdown
Collaborator

If the intention of making Coroutine pub is to embed it in other value types, the fields should be made priv for the sake of minimal change, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants