Skip to content

Commit ae4a837

Browse files
committed
Switch to gloo-timers
1 parent db64f45 commit ae4a837

File tree

8 files changed

+54
-137
lines changed

8 files changed

+54
-137
lines changed

Cargo.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@ Timeouts for futures.
1313
"""
1414

1515
[dependencies]
16-
wasm-timer = { version = "0.2.4", optional = true }
17-
wasm-bindgen-crate = { package = "wasm-bindgen", version = "0.2.55", optional = true }
18-
futures = { version = "0.3.1", optional = true }
19-
web-sys = { version = "0.3.32", optional = true }
16+
gloo-timers = { version = "0.2.0", features = ["futures"], optional = true }
2017

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

2522
[features]
2623
wasm-bindgen = [
27-
"wasm-timer",
28-
"wasm-bindgen-crate",
29-
"futures",
30-
"web-sys"
24+
"gloo-timers"
3125
]

src/delay.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
//! This module contains the `Delay` type which is a future that will resolve
44
//! at a particular point in the future.
55
6-
use crate::Instant;
76
use std::fmt;
87
use std::future::Future;
98
use std::pin::Pin;
109
use std::sync::atomic::AtomicUsize;
1110
use std::sync::atomic::Ordering::SeqCst;
1211
use std::sync::{Arc, Mutex};
1312
use std::task::{Context, Poll};
14-
use std::time::Duration;
13+
use std::time::{Duration, Instant};
1514

1615
use crate::arc_list::Node;
1716
use crate::AtomicWaker;

src/delay_wasm.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! A version of `Delay` that works on wasm.
2+
3+
use gloo_timers::future::TimeoutFuture;
4+
use std::{time::Duration, pin::Pin, task::{Context, Poll}, future::Future};
5+
6+
#[derive(Debug)]
7+
pub struct Delay(TimeoutFuture);
8+
9+
impl Delay {
10+
#[inline]
11+
pub fn new(dur: Duration) -> Delay {
12+
Self(TimeoutFuture::new(dur.as_millis() as u32))
13+
}
14+
}
15+
16+
impl Future for Delay {
17+
type Output = ();
18+
19+
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
20+
Pin::new(*self.0).poll(cx)
21+
}
22+
}

src/global_native.rs renamed to src/global.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::Instant;
21
use std::future::Future;
32
use std::io;
43
use std::mem::{self, ManuallyDrop};
@@ -8,6 +7,7 @@ use std::sync::Arc;
87
use std::task::{Context, RawWaker, RawWakerVTable, Waker};
98
use std::thread;
109
use std::thread::Thread;
10+
use std::time::Instant;
1111

1212
use crate::{Timer, TimerHandle};
1313

@@ -84,7 +84,7 @@ static VTABLE: RawWakerVTable = RawWakerVTable::new(raw_clone, raw_wake, raw_wak
8484

8585
fn raw_clone(ptr: *const ()) -> RawWaker {
8686
let me = ManuallyDrop::new(unsafe { Arc::from_raw(ptr as *const Thread) });
87-
mem::forget(me);
87+
mem::forget(me.clone());
8888
RawWaker::new(ptr, &VTABLE)
8989
}
9090

src/global_wasm.rs

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/heap_timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::Instant;
21
use std::cmp::Ordering;
32
use std::sync::Arc;
3+
use std::time::Instant;
44

55
use crate::{Node, ScheduledTimer};
66

src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
mod arc_list;
2020
mod atomic_waker;
21+
#[cfg(not(all(target_arch = "wasm32", feature = "wasm-bindgen")))]
2122
mod delay;
22-
#[cfg(not(target_arch = "wasm32"))]
23-
mod global_native;
24-
#[cfg(feature = "wasm-bindgen")]
25-
mod global_wasm;
23+
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
24+
mod delay_wasm;
25+
mod global;
2626
mod heap;
2727
mod heap_timer;
2828
mod timer;
@@ -33,8 +33,7 @@ use heap::{Heap, Slot};
3333
use heap_timer::HeapTimer;
3434
use timer::{ScheduledTimer, Timer, TimerHandle};
3535

36+
#[cfg(not(all(target_arch = "wasm32", feature = "wasm-bindgen")))]
3637
pub use self::delay::Delay;
37-
#[cfg(not(feature = "wasm-bindgen"))]
38-
use std::time::Instant;
39-
#[cfg(feature = "wasm-bindgen")]
40-
use wasm_timer::Instant;
38+
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
39+
pub use self::delay_wasm::Delay;

src/timer.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use crate::Instant;
21
use std::fmt;
3-
use std::future::Future;
42
use std::mem;
53
use std::pin::Pin;
64
use std::sync::atomic::AtomicUsize;
75
use std::sync::atomic::Ordering::SeqCst;
86
use std::sync::{Arc, Mutex, Weak};
97
use std::task::{Context, Poll};
8+
use std::time::Instant;
9+
10+
use std::future::Future;
1011

1112
use crate::AtomicWaker;
12-
use crate::{ArcList, Heap, HeapTimer, Node, Slot};
13+
use crate::{global, ArcList, Heap, HeapTimer, Node, Slot};
1314

1415
/// A "timer heap" used to power separately owned instances of `Delay`.
1516
///
@@ -277,39 +278,23 @@ impl Default for TimerHandle {
277278
// handle which will return errors when timer objects are attempted to
278279
// be associated.
279280
if fallback == 0 {
280-
#[cfg(not(target_arch = "wasm32"))]
281-
{
282-
let helper = match crate::global_native::HelperThread::new() {
283-
Ok(helper) => helper,
284-
Err(_) => return TimerHandle { inner: Weak::new() },
285-
};
286-
287-
// If we successfully set ourselves as the actual fallback then we
288-
// want to `forget` the helper thread to ensure that it persists
289-
// globally. If we fail to set ourselves as the fallback that means
290-
// that someone was racing with this call to
291-
// `TimerHandle::default`. They ended up winning so we'll destroy
292-
// our helper thread (which shuts down the thread) and reload the
293-
// fallback.
294-
if helper.handle().set_as_global_fallback().is_ok() {
295-
let ret = helper.handle();
296-
helper.forget();
297-
return ret;
298-
}
299-
}
300-
301-
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
302-
{
303-
let handle = crate::global_wasm::run();
281+
let helper = match global::HelperThread::new() {
282+
Ok(helper) => helper,
283+
Err(_) => return TimerHandle { inner: Weak::new() },
284+
};
304285

305-
// Same as above.
306-
if handle.clone().set_as_global_fallback().is_ok() {
307-
return handle;
308-
}
286+
// If we successfully set ourselves as the actual fallback then we
287+
// want to `forget` the helper thread to ensure that it persists
288+
// globally. If we fail to set ourselves as the fallback that means
289+
// that someone was racing with this call to
290+
// `TimerHandle::default`. They ended up winning so we'll destroy
291+
// our helper thread (which shuts down the thread) and reload the
292+
// fallback.
293+
if helper.handle().set_as_global_fallback().is_ok() {
294+
let ret = helper.handle();
295+
helper.forget();
296+
return ret;
309297
}
310-
#[cfg(all(not(feature = "wasm-bindgen"), target_arch = "wasm32"))]
311-
compile_error!("The `wasm-bindgen` feature must be used when compiling to wasm.");
312-
313298
fallback = HANDLE_FALLBACK.load(SeqCst);
314299
}
315300

0 commit comments

Comments
 (0)