Skip to content

Commit 0ca73c2

Browse files
committed
Fix tests, cargo fmt
1 parent 979bae8 commit 0ca73c2

File tree

8 files changed

+59
-57
lines changed

8 files changed

+59
-57
lines changed

src/delay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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;
67
use std::fmt;
78
use std::future::Future;
89
use std::pin::Pin;
@@ -11,7 +12,6 @@ use std::sync::atomic::Ordering::SeqCst;
1112
use std::sync::{Arc, Mutex};
1213
use std::task::{Context, Poll};
1314
use std::time::Duration;
14-
use crate::Instant;
1515

1616
use crate::arc_list::Node;
1717
use crate::AtomicWaker;

src/global_native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::Instant;
12
use std::future::Future;
23
use std::io;
34
use std::mem::{self, ManuallyDrop};
@@ -7,7 +8,6 @@ use std::sync::Arc;
78
use std::task::{Context, RawWaker, RawWakerVTable, Waker};
89
use std::thread;
910
use std::thread::Thread;
10-
use crate::Instant;
1111

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

src/global_wasm.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::pin::Pin;
66
use std::sync::Arc;
77
use std::task::Context;
88
use std::time::Duration;
9-
use wasm_bindgen::{JsCast, closure::Closure};
9+
use wasm_bindgen::{closure::Closure, JsCast};
1010

1111
use crate::{Instant, Timer, TimerHandle};
1212

@@ -16,62 +16,67 @@ use crate::{Instant, Timer, TimerHandle};
1616
/// > any `forget()` method, as the task is automatically considered
1717
/// > as "forgotten".
1818
pub(crate) fn run() -> TimerHandle {
19-
let timer = Timer::new();
20-
let handle = timer.handle();
21-
schedule_callback(Arc::new(Mutex::new(timer)), Duration::new(0, 0));
22-
handle
19+
let timer = Timer::new();
20+
let handle = timer.handle();
21+
schedule_callback(Arc::new(Mutex::new(timer)), Duration::new(0, 0));
22+
handle
2323
}
2424

2525
/// Calls `Window::setTimeout` with the given `Duration`. The callback wakes up the timer and
2626
/// processes everything.
2727
fn schedule_callback(timer: Arc<Mutex<Timer>>, when: Duration) {
28-
let window = web_sys::window().expect("Unable to access Window");
29-
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(
30-
&Closure::once_into_js(move || {
31-
let mut timer_lock = timer.lock();
28+
let window = web_sys::window().expect("Unable to access Window");
29+
let _ = window
30+
.set_timeout_with_callback_and_timeout_and_arguments_0(
31+
&Closure::once_into_js(move || {
32+
let mut timer_lock = timer.lock();
3233

33-
// We start by polling the timer. If any new `Delay` is created, the waker will be used
34-
// to wake up this task pre-emptively. As such, we pass a `Waker` that calls
35-
// `schedule_callback` with a delay of `0`.
36-
let waker = task::waker(Arc::new(Waker { timer: timer.clone() }));
37-
let _ = Future::poll(Pin::new(&mut *timer_lock), &mut Context::from_waker(&waker));
34+
// We start by polling the timer. If any new `Delay` is created, the waker will be used
35+
// to wake up this task pre-emptively. As such, we pass a `Waker` that calls
36+
// `schedule_callback` with a delay of `0`.
37+
let waker = task::waker(Arc::new(Waker {
38+
timer: timer.clone(),
39+
}));
40+
let _ = Future::poll(Pin::new(&mut *timer_lock), &mut Context::from_waker(&waker));
3841

39-
// Notify the timers that are ready.
40-
let now = Instant::now();
41-
timer_lock.advance_to(now);
42+
// Notify the timers that are ready.
43+
let now = Instant::now();
44+
timer_lock.advance_to(now);
4245

43-
// Each call to `schedule_callback` calls `schedule_callback` again, but also leaves
44-
// the possibility for `schedule_callback` to be called in parallel. Since we don't
45-
// want too many useless callbacks, we...
46-
// TODO: ugh, that's a hack
47-
if Arc::strong_count(&timer) > 20 {
48-
return;
49-
}
46+
// Each call to `schedule_callback` calls `schedule_callback` again, but also leaves
47+
// the possibility for `schedule_callback` to be called in parallel. Since we don't
48+
// want too many useless callbacks, we...
49+
// TODO: ugh, that's a hack
50+
if Arc::strong_count(&timer) > 20 {
51+
return;
52+
}
5053

51-
// We call `schedule_callback` again for the next event.
52-
let sleep_dur = timer_lock.next_event()
53-
.map(|next_event| {
54-
if next_event > now {
55-
next_event - now
56-
} else {
57-
Duration::new(0, 0)
58-
}
59-
})
60-
.unwrap_or(Duration::from_secs(5));
61-
drop(timer_lock);
62-
schedule_callback(timer, sleep_dur);
63-
64-
}).unchecked_ref(),
65-
i32::try_from(when.as_millis()).unwrap_or(0)
66-
).unwrap();
54+
// We call `schedule_callback` again for the next event.
55+
let sleep_dur = timer_lock
56+
.next_event()
57+
.map(|next_event| {
58+
if next_event > now {
59+
next_event - now
60+
} else {
61+
Duration::new(0, 0)
62+
}
63+
})
64+
.unwrap_or(Duration::from_secs(5));
65+
drop(timer_lock);
66+
schedule_callback(timer, sleep_dur);
67+
})
68+
.unchecked_ref(),
69+
i32::try_from(when.as_millis()).unwrap_or(0),
70+
)
71+
.unwrap();
6772
}
6873

6974
struct Waker {
70-
timer: Arc<Mutex<Timer>>,
75+
timer: Arc<Mutex<Timer>>,
7176
}
7277

7378
impl ArcWake for Waker {
74-
fn wake_by_ref(arc_self: &Arc<Self>) {
75-
schedule_callback(arc_self.timer.clone(), Duration::new(0, 0));
76-
}
79+
fn wake_by_ref(arc_self: &Arc<Self>) {
80+
schedule_callback(arc_self.timer.clone(), Duration::new(0, 0));
81+
}
7782
}

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;
12
use std::cmp::Ordering;
23
use std::sync::Arc;
3-
use crate::Instant;
44

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

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
mod arc_list;
2020
mod atomic_waker;
2121
mod delay;
22-
mod heap;
23-
mod heap_timer;
24-
mod timer;
2522
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
2623
mod global_native;
2724
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
2825
mod global_wasm;
26+
mod heap;
27+
mod heap_timer;
28+
mod timer;
2929

3030
use arc_list::{ArcList, Node};
3131
use atomic_waker::AtomicWaker;

src/timer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use crate::Instant;
12
use std::fmt;
3+
use std::future::Future;
24
use std::mem;
35
use std::pin::Pin;
46
use std::sync::atomic::AtomicUsize;
57
use std::sync::atomic::Ordering::SeqCst;
68
use std::sync::{Arc, Mutex, Weak};
79
use std::task::{Context, Poll};
8-
use crate::Instant;
9-
use std::future::Future;
1010

1111
use crate::AtomicWaker;
1212
use crate::{ArcList, Heap, HeapTimer, Node, Slot};
@@ -284,7 +284,6 @@ impl Default for TimerHandle {
284284
Err(_) => return TimerHandle { inner: Weak::new() },
285285
};
286286

287-
288287
// If we successfully set ourselves as the actual fallback then we
289288
// want to `forget` the helper thread to ensure that it persists
290289
// globally. If we fail to set ourselves as the fallback that means

tests/smoke.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::error::Error;
22
use std::pin::Pin;
3-
use std::time::Duration;
4-
use crate::Instant;
3+
use std::time::{Duration, Instant};
54

65
use futures_timer::Delay;
76

tests/timeout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::error::Error;
2-
use std::time::Duration;
3-
use crate::Instant;
2+
use std::time::{Duration, Instant};
43

54
use futures_timer::Delay;
65

0 commit comments

Comments
 (0)