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
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,32 @@ jobs:
CARGO_TARGET_WASM32_WASIP1_THREADS_RUNNER: "wasmtime run -W bulk-memory=y -W threads=y -W shared-memory=y -S threads=y --"
RUSTFLAGS: --cfg tokio_unstable -Dwarnings -C target-feature=+atomics,+bulk-memory -C link-args=--max-memory=67108864

wasm32-wasip2:
name: test tokio for wasm32-wasip2
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- name: Install Rust ${{ env.rust_stable }}
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ env.rust_stable }}
targets: wasm32-wasip2

- name: Install cargo-nextest, wasmtime
uses: taiki-e/install-action@v2
with:
tool: cargo-nextest,wasmtime-cli

- uses: Swatinem/rust-cache@v2

- name: test tokio --target wasm32-wasip2
run: cargo nextest run --target wasm32-wasip2 --features net,macros,rt,io-util
working-directory: tokio
env:
RUSTFLAGS: --cfg tokio_unstable
CARGO_TARGET_WASM32_WASIP2_RUNNER: wasmtime run -Sinherit-network

check-external-types:
name: check-external-types (${{ matrix.os }})
needs: basics
Expand Down
3 changes: 2 additions & 1 deletion spellcheck.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
313
314
&
+
<
Expand Down Expand Up @@ -310,5 +310,6 @@ wakers
Wakers
wakeup
wakeups
WASI
workstealing
ZST
11 changes: 7 additions & 4 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ pin-project-lite = "0.2.11"

# Everything else is optional...
bytes = { version = "1.2.1", optional = true }
mio = { version = "1.0.1", optional = true, default-features = false }
mio = { version = "1.2.0", optional = true, default-features = false }
parking_lot = { version = "0.12.0", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
socket2 = { version = "0.6.0", optional = true, features = ["all"] }
[target.'cfg(any(not(target_family = "wasm"), all(target_os = "wasi", not(target_env = "p1"))))'.dependencies]
socket2 = { version = "0.6.3", optional = true, features = ["all"] }

# Currently unstable. The API exposed by these features may be broken at any time.
# Requires `--cfg tokio_unstable` to enable.
Expand All @@ -112,14 +112,17 @@ tracing = { version = "0.1.29", default-features = false, features = ["std"], op
[target.'cfg(all(tokio_unstable, target_os = "linux"))'.dependencies]
io-uring = { version = "0.7.11", default-features = false, optional = true }
libc = { version = "0.2.168", optional = true }
mio = { version = "1.0.1", default-features = false, features = ["os-poll", "os-ext"], optional = true }
mio = { version = "1.2.0", default-features = false, features = ["os-poll", "os-ext"], optional = true }
slab = { version = "0.4.9", optional = true }
backtrace = { version = "0.3.58", optional = true }

[target.'cfg(unix)'.dependencies]
libc = { version = "0.2.168", optional = true }
signal-hook-registry = { version = "1.1.1", optional = true }

[target.'cfg(target_os = "wasi")'.dependencies]
libc = { version = "0.2.168", optional = true }

[target.'cfg(unix)'.dev-dependencies]
libc = { version = "0.2.168" }
nix = { version = "0.29.0", default-features = false, features = ["aio", "fs", "socket"] }
Expand Down
21 changes: 21 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ macro_rules! cfg_unix {
}
}

/// Enables Unix-specific code, including WASI.
/// Use this macro instead of `cfg(any(unix, target_os = "wasi"))` to generate docs properly.
macro_rules! cfg_unix_or_wasi {
($($item:item)*) => {
$(
#[cfg(any(all(doc, docsrs), unix, target_os = "wasi"))]
#[cfg_attr(docsrs, doc(cfg(any(unix, target_os = "wasi"))))]
$item
)*
}
}

/// Enables unstable Windows-specific code.
/// Use this macro instead of `cfg(windows)` to generate docs properly.
macro_rules! cfg_unstable_windows {
Expand Down Expand Up @@ -674,6 +686,15 @@ macro_rules! cfg_not_wasi {
}
}

macro_rules! cfg_not_wasip1 {
($($item:item)*) => {
$(
#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
$item
)*
}
}

macro_rules! cfg_is_wasm_not_wasi {
($($item:item)*) => {
$(
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! [`AsyncFd`]: crate::io::unix::AsyncFd

mod addr;
cfg_not_wasi! {
cfg_not_wasip1! {
#[cfg(feature = "net")]
pub(crate) use addr::to_socket_addrs;
}
Expand All @@ -42,7 +42,7 @@ cfg_net! {
pub mod tcp;
pub use tcp::listener::TcpListener;
pub use tcp::stream::TcpStream;
cfg_not_wasi! {
cfg_not_wasip1! {
pub use tcp::socket::TcpSocket;

mod udp;
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::io::{Interest, PollEvented};
use crate::net::tcp::TcpStream;
use crate::util::check_socket_for_blocking;

cfg_not_wasi! {
cfg_not_wasip1! {
use crate::net::{to_socket_addrs, ToSocketAddrs};
}

Expand Down Expand Up @@ -60,7 +60,7 @@ cfg_net! {
}

impl TcpListener {
cfg_not_wasi! {
cfg_not_wasip1! {
/// Creates a new `TcpListener`, which will be bound to the specified address.
///
/// The returned listener is ready for accepting connections.
Expand Down Expand Up @@ -292,15 +292,15 @@ impl TcpListener {

#[cfg(target_os = "wasi")]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
use std::os::fd::{FromRawFd, IntoRawFd};
self.io
.into_inner()
.map(|io| io.into_raw_fd())
.map(|raw_fd| unsafe { std::net::TcpListener::from_raw_fd(raw_fd) })
}
}

cfg_not_wasi! {
cfg_not_wasip1! {
pub(crate) fn new(listener: mio::net::TcpListener) -> io::Result<TcpListener> {
let io = PollEvented::new(listener)?;
Ok(TcpListener { io })
Expand Down Expand Up @@ -427,7 +427,7 @@ cfg_unstable! {
#[cfg(target_os = "wasi")]
mod sys {
use super::TcpListener;
use std::os::wasi::prelude::*;
use std::os::fd::{RawFd, BorrowedFd, AsRawFd, AsFd};

impl AsRawFd for TcpListener {
fn as_raw_fd(&self) -> RawFd {
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/net/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pub(crate) mod listener;

cfg_not_wasi! {
cfg_not_wasip1! {
pub(crate) mod socket;
}

Expand Down
52 changes: 31 additions & 21 deletions tokio/src/net/tcp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::fmt;
use std::io;
use std::net::SocketAddr;

#[cfg(unix)]
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(not(windows))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::time::Duration;

cfg_windows! {
Expand Down Expand Up @@ -170,7 +170,8 @@ impl TcpSocket {
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "wasi",
))]
let ty = ty.nonblocking();
let inner = socket2::Socket::new(domain, ty, Some(socket2::Protocol::TCP))?;
Expand All @@ -182,7 +183,8 @@ impl TcpSocket {
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"
target_os = "openbsd",
target_os = "wasi",
)))]
inner.set_nonblocking(true)?;
Ok(TcpSocket { inner })
Expand Down Expand Up @@ -597,7 +599,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
)))]
#[cfg_attr(
docsrs,
Expand All @@ -606,7 +609,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
))))
)]
pub fn tos_v4(&self) -> io::Result<u32> {
Expand All @@ -625,7 +629,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
)))]
#[cfg_attr(
docsrs,
Expand All @@ -634,7 +639,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
))))
)]
pub fn tos(&self) -> io::Result<u32> {
Expand All @@ -657,7 +663,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
)))]
#[cfg_attr(
docsrs,
Expand All @@ -666,7 +673,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
))))
)]
pub fn set_tos_v4(&self, tos: u32) -> io::Result<()> {
Expand All @@ -685,7 +693,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
)))]
#[cfg_attr(
docsrs,
Expand All @@ -694,7 +703,8 @@ impl TcpSocket {
target_os = "redox",
target_os = "solaris",
target_os = "illumos",
target_os = "haiku"
target_os = "haiku",
target_os = "wasi",
))))
)]
pub fn set_tos(&self, tos: u32) -> io::Result<()> {
Expand Down Expand Up @@ -826,7 +836,7 @@ impl TcpSocket {
/// ```
pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
if let Err(err) = self.inner.connect(&addr.into()) {
#[cfg(unix)]
#[cfg(not(windows))]
if err.raw_os_error() != Some(libc::EINPROGRESS) {
return Err(err);
}
Expand All @@ -835,9 +845,9 @@ impl TcpSocket {
return Err(err);
}
}
#[cfg(unix)]
#[cfg(not(windows))]
let mio = {
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::os::fd::{FromRawFd, IntoRawFd};

let raw_fd = self.inner.into_raw_fd();
unsafe { mio::net::TcpStream::from_raw_fd(raw_fd) }
Expand Down Expand Up @@ -891,9 +901,9 @@ impl TcpSocket {
/// ```
pub fn listen(self, backlog: u32) -> io::Result<TcpListener> {
self.inner.listen(backlog as i32)?;
#[cfg(unix)]
#[cfg(not(windows))]
let mio = {
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::os::fd::{FromRawFd, IntoRawFd};

let raw_fd = self.inner.into_raw_fd();
unsafe { mio::net::TcpListener::from_raw_fd(raw_fd) }
Expand Down Expand Up @@ -945,9 +955,9 @@ impl TcpSocket {
/// }
/// ```
pub fn from_std_stream(std_stream: std::net::TcpStream) -> TcpSocket {
#[cfg(unix)]
#[cfg(not(windows))]
{
use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::os::fd::{FromRawFd, IntoRawFd};

let raw_fd = std_stream.into_raw_fd();
unsafe { TcpSocket::from_raw_fd(raw_fd) }
Expand Down Expand Up @@ -981,8 +991,8 @@ impl fmt::Debug for TcpSocket {

// These trait implementations can't be build on Windows, so we completely
// ignore them, even when building documentation.
#[cfg(unix)]
cfg_unix! {
#[cfg(any(unix, target_os = "wasi"))]
cfg_unix_or_wasi! {
impl AsRawFd for TcpSocket {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
Expand Down
11 changes: 7 additions & 4 deletions tokio/src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cfg_not_wasi! {
use std::time::Duration;
}

cfg_not_wasip1! {
use crate::net::{to_socket_addrs, ToSocketAddrs};
use std::future::poll_fn;
use std::time::Duration;
}

use crate::io::{AsyncRead, AsyncWrite, Interest, PollEvented, ReadBuf, Ready};
Expand Down Expand Up @@ -73,7 +76,7 @@ cfg_net! {
}

impl TcpStream {
cfg_not_wasi! {
cfg_not_wasip1! {
/// Opens a TCP connection to a remote host.
///
/// `addr` is an address of the remote host. Anything which implements the
Expand Down Expand Up @@ -272,7 +275,7 @@ impl TcpStream {

#[cfg(target_os = "wasi")]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
use std::os::fd::{FromRawFd, IntoRawFd};
self.io
.into_inner()
.map(|io| io.into_raw_fd())
Expand Down Expand Up @@ -1564,7 +1567,7 @@ cfg_windows! {
#[cfg(all(tokio_unstable, target_os = "wasi"))]
mod sys {
use super::TcpStream;
use std::os::wasi::prelude::*;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};

impl AsRawFd for TcpStream {
fn as_raw_fd(&self) -> RawFd {
Expand Down
Loading
Loading