Skip to content

Commit

Permalink
Add the impl for wasi environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 19, 2023
1 parent f8493f3 commit 395c868
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ futures = { version = "0.3", default-features = false, features = [
] }
pin-project = "1.0.11"
pinned = "0.1.0"
once_cell = "1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = { version = "0.4" }
gloo = { version = "0.8" }

once_cell = { version = "1", optional = true }
tokio_wasi = { version = "1", features = ["rt", "time"], optional = true }
tokio-stream_wasi = { version = "0.1", features = ["time"], optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
num_cpus = "1.13"
once_cell = "1"
tokio = { version = "1", features = ["rt", "time"] }
tokio-stream = { version = "0.1", features = ["time"] }

Expand All @@ -38,7 +37,7 @@ tokio = { version = "1", features = ["full"] }

[features]
default = []
wasi = ["dep:once_cell", "dep:tokio_wasi", "dep:tokio-stream_wasi"]
wasi = ["dep:tokio_wasi", "dep:tokio-stream_wasi"]

[package.metadata.docs.rs]
all-features = true
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub mod time;
#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))]
#[path = "rt_wasm_bindgen/mod.rs"]
mod imp;
#[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))]
#[cfg(all(target_arch = "wasm32", feature = "wasi"))]
#[path = "rt_tokio_wasi/mod.rs"]
mod imp;
#[cfg(not(target_arch = "wasm32"))]
#[path = "rt_tokio/mod.rs"]
mod imp;

Expand Down
71 changes: 71 additions & 0 deletions src/rt_tokio_wasi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::future::Future;
use std::io;
use std::marker::PhantomData;

use once_cell::sync::Lazy;

static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
});

pub(crate) mod time;

pub(crate) fn get_default_runtime_size() -> usize {
0
}

#[inline(always)]
pub(super) fn spawn_local<F>(f: F)
where
F: Future<Output = ()> + 'static,
{
RUNTIME.block_on(async { f.await })
}

#[derive(Debug, Clone, Default)]
pub(crate) struct Runtime {}

impl Runtime {
pub fn new(_size: usize) -> io::Result<Self> {
Ok(Self {})
}

pub fn spawn_pinned<F, Fut>(&self, create_task: F)
where
F: FnOnce() -> Fut,
F: Send + 'static,
Fut: Future<Output = ()> + 'static,
{
RUNTIME.block_on(async { create_task().await })
}
}

#[derive(Debug, Clone)]
pub(crate) struct LocalHandle {
// This type is not send or sync.
_marker: PhantomData<*const ()>,
}

impl LocalHandle {
pub fn try_current() -> Option<Self> {
Some(Self {
_marker: PhantomData,
})
}

pub fn current() -> Self {
Self {
_marker: PhantomData,
}
}

pub fn spawn_local<F>(&self, f: F)
where
F: Future<Output = ()> + 'static,
{
RUNTIME.block_on(async { f.await })
}
}
14 changes: 14 additions & 0 deletions src/rt_tokio_wasi/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::future::Future;
use std::time::Duration;

use futures::stream::{Stream, StreamExt};
use tokio_stream::wrappers::IntervalStream;

#[inline(always)]
pub(crate) fn sleep(dur: Duration) -> impl Future<Output = ()> {
tokio::time::sleep(dur)
}

pub(crate) fn interval(dur: Duration) -> impl Stream<Item = ()> {
IntervalStream::new(tokio::time::interval(dur)).then(|_| async {})
}

0 comments on commit 395c868

Please sign in to comment.