Skip to content

Migrate p2 support to a modular backend in sys/#134

Open
yoshuawuyts wants to merge 10 commits into
mainfrom
yoshuawuyts/yoshuawuyts-move-p2-impl-to-sys
Open

Migrate p2 support to a modular backend in sys/#134
yoshuawuyts wants to merge 10 commits into
mainfrom
yoshuawuyts/yoshuawuyts-move-p2-impl-to-sys

Conversation

@yoshuawuyts

Copy link
Copy Markdown
Member

This is a reimplementation of #128, this time with an easier to track git history by leveraging git mv. This moves the existing wasip2 code to a new sys/p2 sub-module. I've validated the API surface didn't change using cargo public-api:

❯ cargo public-api --target wasm32-wasip2 -p wstd diff origin/main..HEAD
 Documenting wstd v0.6.7
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.93s
    Blocking waiting for file lock on build directory
 Documenting wstd v0.6.7
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.58s
Removed items from the public API
=================================
(none)

Changed items in the public API
===============================
(none)

Added items to the public API
=============================
(none)

While I have used the Copilot AI agent in this PR, I've mostly done that because the change itself is fairly mechanical: move all existing code to a new submodule, and then re-export that from behind identical shims. Perhaps this would have been fast to type by hand, perhaps not. But either way: I think this PR is good to go now. Thanks!

yoshuawuyts and others added 9 commits June 9, 2026 02:44
This is a pure `git mv` relocation with no content changes, so git
records clean renames and `git blame`/`--follow` lineage is preserved.
The build is intentionally red after this commit; backend wiring and
crate-root re-export shims are added in the following commit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restores a green build after the pure relocation. The crate root now
selects a platform backend via a single cfg-if in src/sys/mod.rs, and
each moved module is re-exported through a thin shim so the public API
is preserved exactly:

* add `mod sys` plus src/sys/mod.rs (cfg-if) and src/sys/p2/mod.rs
* add crate-root shims: http.rs, net.rs, rand.rs, runtime.rs, time.rs
* re-export the moved stdio/streams via src/sys/p2/io and io/mod.rs
* repoint the few internal `super::`/private-module paths the move broke
* add the cfg-if dependency

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Spell out, in `src/sys/mod.rs`, the duck-typed contract each `src/sys`
backend must satisfy so the crate-root modules can be target-agnostic
facades. Add a private `const _` block that statically checks the parts
the facades depend on (the IO stream traits today), so backend drift
fails fast with a clear message instead of surfacing deep inside a
facade.

No behavior or public-API change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Promote the portable time types — `Duration`, `Instant`, `Interval`,
`interval`, `Timer`, `Wait`, and the crate-internal `utils::timeout_err`
— out of the wasip2 backend and into the `crate::time` facade, where
they are written once with no `#[cfg]` and own all of their own
arithmetic.

The wasip2 backend (`src/sys/p2/time`) keeps only the primitives that
genuinely depend on the WASI 0.2 clocks: the `MonotonicInstant` /
`MonotonicDuration` nanosecond aliases, `now`, `SystemTime`, and a
`Sleep` future built by `sleep_until`. `Timer` now records its deadline
at construction (`TimerKind::{Never, At}`) and builds a fresh backend
`Sleep` on each `wait`, so a second backend (p3) only has to supply a
different `Sleep`/`sleep_until` rather than re-implementing the facade.

Extend the backend-contract assertions in `src/sys/mod.rs` accordingly.
Public API is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The length guard and slice copy in `get_random_bytes` /
`get_insecure_random_bytes` are identical across backends; only the host
RNG call differs. Move the two public functions up into the `crate::rand`
facade and reduce the wasip2 backend to `random_bytes` /
`insecure_random_bytes` primitives that return the raw bytes.

Public API is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`spawn` and the `async_task::Task` re-export are identical across
backends; only `block_on` and the `Reactor` internals are platform
specific. Move `spawn` and `Task` up into the `crate::runtime` facade
and re-export `block_on`/`Reactor`/`AsyncPollable`/`WaitFor` from the
wasip2 backend. `REACTOR` and `Reactor` stay in the backend.

Public API is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Convert `src/http.rs` from a blanket `pub use crate::sys::http::*` shim into a
real facade: it owns the portable surface (`StatusCode`, `Uri`/`Authority`/
`PathAndQuery`, the `error` module of `anyhow`/`http`-crate error types, and the
`body`/`request`/`response`/`server` public submodule paths) and re-exports the
backend's concrete types (`Client`, `Method`, `Scheme`, `HeaderMap`, `Body`,
...) through `crate::sys::http::*`. This keeps every public path identical while
moving the target-agnostic shape out of the backend, so a future p3 backend only
has to supply the platform pieces.

The backend `sys::p2::http` now exposes just the platform impl: submodules with
`pub`/`pub(crate)` visibility plus `pub use wasip2::http::types::{ErrorCode,
HeaderError}`. Its impl files import the public error/value types from the facade
(`crate::http::*`) instead of the removed backend-level re-exports, and the
public `server` module doc moves up to the facade.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`src/net.rs` was a blanket `pub use crate::sys::net::*`. Make it a real
facade: declare the crate's public network surface -- `TcpStream`,
`TcpListener`, `ReadHalf`, `WriteHalf`, `Incoming` -- as thin wrappers at the
crate root that delegate every operation to the selected backend's
implementation under `crate::sys::net`.

The one genuinely portable piece, `TcpStream::connect`'s `ToSocketAddrs`
resolve-and-retry loop, is written once here against the backend's
`connect_addr` primitive (removed from the backend), so a second backend
inherits it for free. Socket-shutdown cleanup still runs through the inner
backend handles' own `Drop`; the facade keeps explicit `Drop` impls so the
public drop contract is unchanged.

Public API is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Now that `wstd` and `wstd-axum` only compile for `wasm32-wasip2` (the `sys`
backend is cfg-gated and other targets hit `compile_error!`), the CI steps that
built them for the host target fail to resolve `crate::sys::*`. Scope those
steps to the target:

- `check` and `Clippy` build `wstd`/`wstd-axum` with `--target wasm32-wasip2
  --all-targets` and keep the `test-programs` host harness on the host.
- `Docs` builds with `--target wasm32-wasip2`.
- `ci/publish.rs` passes `--target wasm32-wasip2` when packaging `wstd` and
  `wstd-axum`.

`--all-targets` newly lints the `#[cfg(test)]` code, so regroup the digit
literals in the `time` duration tests to satisfy `inconsistent_digit_grouping`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant