Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ env:
CARGO_TERM_COLOR: always
RUST_BACKTRACE: full
CI: true
WASMTIME_VERSION: 41.0.3

permissions:
contents: read # to fetch code (actions/checkout)
Expand Down Expand Up @@ -83,6 +84,21 @@ jobs:
- name: Check
# We only run check allowing us to use newer features in tests.
run: cargo check --all-features
TestWASI:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- name: Setup `wasmtime`
run: |
curl -OL https://github.com/bytecodealliance/wasmtime/releases/download/v${WASMTIME_VERSION}/wasmtime-v${WASMTIME_VERSION}-x86_64-linux.tar.xz
tar xf wasmtime-v${WASMTIME_VERSION}-x86_64-linux.tar.xz
echo CARGO_TARGET_WASM32_WASIP2_RUNNER="$(pwd)/wasmtime-v${WASMTIME_VERSION}-x86_64-linux/wasmtime run -Sinherit-network" >> $GITHUB_ENV
- name: Install target
run: rustup target add wasm32-wasip2
- name: Tests
run: cargo test --target wasm32-wasip2 --all-features
Nightly:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down
8 changes: 6 additions & 2 deletions src/io_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ impl<T> DerefMut for IoSource<T> {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(
unix,
target_os = "hermit",
all(target_os = "wasi", not(target_env = "p1"))
))]
impl<T> event::Source for IoSource<T>
where
T: AsRawFd,
Expand Down Expand Up @@ -175,7 +179,7 @@ where
}
}

#[cfg(target_os = "wasi")]
#[cfg(all(target_os = "wasi", target_env = "p1"))]
impl<T> event::Source for IoSource<T>
where
T: AsRawFd,
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ pub mod hermit {
pub use crate::sys::SourceFd;
}

#[cfg(all(target_os = "wasi", not(target_env = "p1"), feature = "os-ext"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "wasi", feature = "os-ext"))))]
pub mod wasi {
//! WASI-only extensions.

pub use crate::sys::SourceFd;
}

#[cfg(all(windows, feature = "os-ext"))]
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "os-ext"))))]
pub mod windows {
Expand Down
4 changes: 2 additions & 2 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
mod tcp;
pub use self::tcp::{TcpListener, TcpStream};

#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
mod udp;
#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
pub use self::udp::UdpSocket;

#[cfg(unix)]
Expand Down
12 changes: 8 additions & 4 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ use std::{fmt, io};

use crate::io_source::IoSource;
use crate::net::TcpStream;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(
unix,
target_os = "hermit",
all(target_os = "wasi", not(target_env = "p1"))
))]
use crate::sys::tcp::set_reuseaddr;
#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
use crate::sys::{
tcp::{bind, listen, new_for_addr},
LISTEN_BACKLOG_SIZE,
Expand Down Expand Up @@ -62,10 +66,10 @@ impl TcpListener {
/// 2. Set the `SO_REUSEADDR` option on the socket on Unix.
/// 3. Bind the socket to the specified address.
/// 4. Calls `listen` on the socket to prepare it to receive new connections.
#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
let listener = unsafe { TcpListener::from_raw_fd(socket) };
#[cfg(windows)]
let listener = unsafe { TcpListener::from_raw_socket(socket as _) };
Expand Down
6 changes: 3 additions & 3 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::os::windows::io::{
};

use crate::io_source::IoSource;
#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
use crate::sys::tcp::{connect, new_for_addr};
use crate::{event, Interest, Registry, Token};

Expand Down Expand Up @@ -87,10 +87,10 @@ impl TcpStream {
/// entries in the routing cache.
///
/// [write interest]: Interest::WRITABLE
#[cfg(not(target_os = "wasi"))]
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
let stream = unsafe { TcpStream::from_raw_fd(socket) };
#[cfg(windows)]
let stream = unsafe { TcpStream::from_raw_socket(socket as _) };
Expand Down
6 changes: 6 additions & 0 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use crate::{event, sys, Interest, Registry, Token};
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # // Temporarily disabled on WASI pending https://github.com/WebAssembly/wasi-libc/pull/740:
/// # if cfg!(target_os = "wasi") { return Ok(()) }
/// // An Echo program:
/// // SENDER -> sends a message.
/// // ECHOER -> listens and prints the message received.
Expand Down Expand Up @@ -344,6 +346,8 @@ impl UdpSocket {
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # // WASI does not yet support broadcast.
/// # if cfg!(target_os = "wasi") { return Ok(()) }
/// use mio::net::UdpSocket;
///
/// let broadcast_socket = UdpSocket::bind("127.0.0.1:0".parse()?)?;
Expand Down Expand Up @@ -374,6 +378,8 @@ impl UdpSocket {
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # // WASI does not yet support broadcast.
/// # if cfg!(target_os = "wasi") { return Ok(()) }
/// use mio::net::UdpSocket;
///
/// let broadcast_socket = UdpSocket::bind("127.0.0.1:0".parse()?)?;
Expand Down
2 changes: 2 additions & 0 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ impl Poll {
#[cfg_attr(not(all(feature = "os-poll", feature = "net")), doc = "```ignore")]
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// # // WASI does not yet support multithreading:
/// # if cfg!(target_os = "wasi") { return Ok(()) }
/// use mio::{Events, Poll, Interest, Token};
/// use mio::net::TcpStream;
///
Expand Down
14 changes: 9 additions & 5 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ cfg_os_poll! {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(any(
unix,
target_os = "hermit",
all(target_os = "wasi", not(target_env = "p1"))
))]
cfg_os_poll! {
mod unix;
#[allow(unused_imports)]
Expand All @@ -63,10 +67,10 @@ cfg_os_poll! {
pub use self::windows::*;
}

#[cfg(target_os = "wasi")]
#[cfg(all(target_os = "wasi", target_env = "p1"))]
cfg_os_poll! {
mod wasi;
pub(crate) use self::wasi::*;
mod wasip1;
pub(crate) use self::wasip1::*;
}

cfg_not_os_poll! {
Expand Down Expand Up @@ -115,7 +119,7 @@ pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 128;
/// is not implemented for them. Feel free to update this if the `libc` crate
/// changes.
#[allow(dead_code)]
#[cfg(target_os = "hermit")]
#[cfg(any(target_os = "hermit", target_os = "wasi"))]
pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 1024;

#[allow(dead_code)]
Expand Down
7 changes: 5 additions & 2 deletions src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ cfg_os_poll! {
target_os = "solaris",
target_os = "vita",
target_os = "cygwin",
target_os = "wasi",
), path = "selector/poll.rs")]
mod selector;
pub(crate) use self::selector::*;
Expand Down Expand Up @@ -103,7 +104,9 @@ cfg_os_poll! {
target_os = "solaris",
target_os = "vita",
target_os = "cygwin",
all(target_os = "wasi", target_env = "p1")
), path = "waker/pipe.rs")]
#[cfg_attr(all(target_os = "wasi", not(target_env = "p1")), path = "waker/single_threaded.rs")]
mod waker;
// NOTE: the `Waker` type is expected in the selector module as the
// `poll(2)` implementation needs to do some special stuff.
Expand All @@ -118,7 +121,7 @@ cfg_os_poll! {

pub(crate) mod tcp;
pub(crate) mod udp;
#[cfg(not(target_os = "hermit"))]
#[cfg(not(any(target_os = "hermit", target_os = "wasi")))]
pub(crate) mod uds;
}

Expand Down Expand Up @@ -153,8 +156,8 @@ cfg_os_poll! {
target_os = "vita",
target_os = "cygwin",
),
// Hermit doesn't support pipes.
not(target_os = "hermit"),
not(target_os = "wasi"),
))]
pub(crate) mod pipe;
}
Expand Down
6 changes: 5 additions & 1 deletion src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub(crate) fn new_socket(domain: libc::c_int, socket_type: libc::c_int) -> io::R
target_os = "cygwin",
))]
let socket_type = socket_type | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC;
// WASI doesn't have the concept of `fork`ing or `exec`ing processes, so
// `SOCK_CLOEXEC` neither exists nor is relevant:
#[cfg(target_os = "wasi")]
let socket_type = socket_type | libc::SOCK_NONBLOCK;
#[cfg(target_os = "nto")]
let socket_type = socket_type | libc::SOCK_CLOEXEC;

Expand Down Expand Up @@ -108,7 +112,7 @@ pub(crate) fn socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, libc::socklen_
sin_family: libc::AF_INET as libc::sa_family_t,
sin_port: addr.port().to_be(),
sin_addr,
#[cfg(not(any(target_os = "haiku", target_os = "vita")))]
#[cfg(not(any(target_os = "haiku", target_os = "vita", target_os = "wasi")))]
sin_zero: [0; 8],
#[cfg(target_os = "haiku")]
sin_zero: [0; 24],
Expand Down
Loading