Skip to content

Support for wasm32-unknown-unknown #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
abf64b7
Support wasm
expenses Dec 14, 2019
2f1984a
Error instead
expenses Dec 14, 2019
17b80a1
Revert "Error instead"
expenses Dec 14, 2019
10722a7
add wasm global
expenses Dec 15, 2019
979bae8
src/global.rs -> src/global_native.rs
expenses Dec 15, 2019
0ca73c2
Fix tests, cargo fmt
expenses Dec 15, 2019
11baa0e
Switch to wasm-timer for Instant, don't use parking_lot
expenses Dec 17, 2019
e394596
Fmt
expenses Dec 17, 2019
bfbe40d
Use feature flag instead
expenses Jan 2, 2020
832cfa2
Merge remote-tracking branch 'origin/master' into wasm-support
expenses Jan 2, 2020
0b5fe5e
Update Cargo.toml
expenses Jan 2, 2020
00d43f3
Merge branch 'wasm-support' of github.com:expenses/futures-timer into…
expenses Jan 2, 2020
e8e60aa
Revert "Update Cargo.toml"
expenses Jan 2, 2020
ab267cc
Fix native build
expenses Jan 7, 2020
94cb276
Use global_native when not on wasm32
expenses Jan 7, 2020
db64f45
Appease clippy a bit
expenses Jan 8, 2020
ae4a837
Switch to gloo-timers
expenses Jan 15, 2020
2250577
Fix pinning
expenses Jan 15, 2020
5ced38b
Add send_wrapper
expenses Jan 15, 2020
1c66b18
Add a reset stub
expenses Jan 15, 2020
2746e4d
Change reset behavious to panic
expenses Jan 15, 2020
5d737ca
Remove 'when' from Delay, switch reset to take a Duration
expenses Jan 21, 2020
4cf9125
run cargo fmt
expenses Jan 21, 2020
034ea15
Update src/delay_wasm.rs
expenses Jan 27, 2020
0190692
Update src/delay_wasm.rs
expenses Jan 27, 2020
511b0a1
Update src/delay.rs
expenses Jan 27, 2020
ef8a441
fix clippy, nitpicks
expenses Jan 27, 2020
06d1bd8
Merge branch 'wasm-support' of github.com:expenses/futures-timer into…
expenses Jan 27, 2020
9f620d7
delay -> dur
expenses Jan 27, 2020
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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/futures-timer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ Timeouts for futures.
"""

[dependencies]
gloo-timers = { version = "0.2.0", features = ["futures"], optional = true }
send_wrapper = { version = "0.4.0", optional = true }

[dev-dependencies]
async-std = { version = "1.0.1", features = ["attributes"] }
futures = "0.3.1"

[features]
wasm-bindgen = [
"gloo-timers",
"send_wrapper"
]
35 changes: 8 additions & 27 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::{ScheduledTimer, TimerHandle};
/// at.
pub struct Delay {
state: Option<Arc<Node<ScheduledTimer>>>,
when: Instant,
}

impl Delay {
Expand All @@ -38,25 +37,14 @@ impl Delay {
Delay::new_handle(Instant::now() + dur, Default::default())
}

/// Return the `Instant` when this delay will fire.
#[inline]
pub fn when(&self) -> Instant {
self.when
}

/// Creates a new future which will fire at the time specified by `at`.
///
/// The returned instance of `Delay` will be bound to the timer specified by
/// the `handle` argument.
pub(crate) fn new_handle(at: Instant, handle: TimerHandle) -> Delay {
let inner = match handle.inner.upgrade() {
Some(i) => i,
None => {
return Delay {
state: None,
when: at,
}
}
None => return Delay { state: None },
};
let state = Arc::new(Node::new(ScheduledTimer {
at: Mutex::new(Some(at)),
Expand All @@ -70,30 +58,23 @@ impl Delay {
// timer, meaning that we'll want to immediately return an error from
// `poll`.
if inner.list.push(&state).is_err() {
return Delay {
state: None,
when: at,
};
return Delay { state: None };
}

inner.waker.wake();
Delay {
state: Some(state),
when: at,
}
Delay { state: Some(state) }
}

/// Resets this timeout to an new timeout which will fire at the time
/// specified by `at`.
#[inline]
pub fn reset(&mut self, at: Instant) {
self.when = at;
if self._reset(self.when).is_err() {
pub fn reset(&mut self, dur: Duration) {
if self._reset(dur).is_err() {
self.state = None
}
}

fn _reset(&mut self, at: Instant) -> Result<(), ()> {
fn _reset(&mut self, dur: Duration) -> Result<(), ()> {
let state = match self.state {
Some(ref state) => state,
None => return Err(()),
Expand All @@ -111,7 +92,7 @@ impl Delay {
Err(s) => bits = s,
}
}
*state.at.lock().unwrap() = Some(at);
*state.at.lock().unwrap() = Some(Instant::now() + dur);
// If we fail to push our node then we've become an inert timer, so
// we'll want to clear our `state` field accordingly
timeouts.list.push(state)?;
Expand Down Expand Up @@ -165,6 +146,6 @@ impl Drop for Delay {

impl fmt::Debug for Delay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("Delay").field("when", &self.when).finish()
f.debug_struct("Delay").finish()
}
}
36 changes: 36 additions & 0 deletions src/delay_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! A version of `Delay` that works on wasm.

use gloo_timers::future::TimeoutFuture;
use send_wrapper::SendWrapper;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
time::Duration,
};

/// A version of `Delay` that works on wasm.
#[derive(Debug)]
pub struct Delay(SendWrapper<TimeoutFuture>);

impl Delay {
/// Creates a new future which will fire at `dur` time into the future.
#[inline]
pub fn new(dur: Duration) -> Delay {
Self(SendWrapper::new(TimeoutFuture::new(dur.as_millis() as u32)))
}

/// Resets the timeout.
#[inline]
pub fn reset(&mut self, dur: Duration) {
*self = Delay::new(at);
}
}

impl Future for Delay {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
Pin::new(&mut *Pin::into_inner(self).0).poll(cx)
}
}
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
//!
//! # Examples
//!
//! ```no_run
//! # #[async_std::main]
//! # async fn main() {
//! ```
//! use std::time::Duration;
//! use futures_timer::Delay;
//! use futures::executor::block_on;
//!
//! let now = Delay::new(Duration::from_secs(3)).await;
//! let now = block_on(Delay::new(Duration::from_secs(3)));
//! println!("waited for 3 secs");
//! # }
//! ```

#![deny(missing_docs)]
#![warn(missing_debug_implementations)]

mod arc_list;
mod atomic_waker;
#[cfg(not(all(target_arch = "wasm32", feature = "wasm-bindgen")))]
mod delay;
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
mod delay_wasm;
mod global;
mod heap;
mod heap_timer;
Expand All @@ -30,4 +31,7 @@ use heap::{Heap, Slot};
use heap_timer::HeapTimer;
use timer::{ScheduledTimer, Timer, TimerHandle};

#[cfg(not(all(target_arch = "wasm32", feature = "wasm-bindgen")))]
pub use self::delay::Delay;
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
pub use self::delay_wasm::Delay;
2 changes: 1 addition & 1 deletion tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn reset() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
assert!(i.elapsed() > dur);

let i = Instant::now();
d.reset(Instant::now() + dur);
d.reset(dur);
d.await;
assert!(i.elapsed() > dur);
Ok(())
Expand Down