Skip to content

Commit e64a499

Browse files
authored
Merge pull request #37 from async-rs/remove-error-from-poll
Remove error from poll
2 parents 9b14a49 + 93b73be commit e64a499

File tree

4 files changed

+14
-73
lines changed

4 files changed

+14
-73
lines changed

src/delay.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
use std::fmt;
77
use std::future::Future;
8-
use std::io;
98
use std::pin::Pin;
109
use std::sync::atomic::AtomicUsize;
1110
use std::sync::atomic::Ordering::SeqCst;
@@ -51,8 +50,7 @@ impl Delay {
5150
///
5251
/// The returned instance of `Delay` will be bound to the timer specified by
5352
/// the `handle` argument.
54-
#[doc(hidden)]
55-
pub fn new_handle(at: Instant, handle: TimerHandle) -> Delay {
53+
pub(crate) fn new_handle(at: Instant, handle: TimerHandle) -> Delay {
5654
let inner = match handle.inner.upgrade() {
5755
Some(i) => i,
5856
None => {
@@ -138,19 +136,16 @@ impl Delay {
138136
}
139137

140138
impl Future for Delay {
141-
type Output = io::Result<()>;
139+
type Output = ();
142140

143141
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
144142
let state = match self.state {
145143
Some(ref state) => state,
146-
None => {
147-
let err = Err(io::Error::new(io::ErrorKind::Other, "timer has gone away"));
148-
return Poll::Ready(err);
149-
}
144+
None => panic!("timer has gone away"),
150145
};
151146

152147
if state.state.load(SeqCst) & 1 != 0 {
153-
return Poll::Ready(Ok(()));
148+
return Poll::Ready(());
154149
}
155150

156151
state.waker.register(&cx.waker());
@@ -159,11 +154,8 @@ impl Future for Delay {
159154
// state. If we've fired the first bit is set, and if we've been
160155
// invalidated the second bit is set.
161156
match state.state.load(SeqCst) {
162-
n if n & 0b01 != 0 => Poll::Ready(Ok(())),
163-
n if n & 0b10 != 0 => Poll::Ready(Err(io::Error::new(
164-
io::ErrorKind::Other,
165-
"timer has gone away",
166-
))),
157+
n if n & 0b01 != 0 => Poll::Ready(()),
158+
n if n & 0b10 != 0 => panic!("timer has gone away"),
167159
_ => Poll::Pending,
168160
}
169161
}

src/lib.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ mod timer;
2727
use arc_list::{ArcList, Node};
2828
use heap::{Heap, Slot};
2929
use heap_timer::HeapTimer;
30-
use timer::{ScheduledTimer, TimerHandle};
31-
32-
#[doc(hidden)]
33-
pub use timer::Timer;
30+
use timer::{ScheduledTimer, Timer, TimerHandle};
3431

3532
pub use self::delay::Delay;

tests/smoke.rs

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
use std::time::{Duration, Instant};
2-
3-
use futures::future::poll_fn;
4-
use futures_timer::{Delay, Timer};
5-
61
use std::error::Error;
72
use std::pin::Pin;
8-
use std::task::Poll;
9-
10-
use futures::prelude::*;
3+
use std::time::{Duration, Instant};
114

12-
fn far_future() -> Instant {
13-
Instant::now() + Duration::new(5000, 0)
14-
}
5+
use futures_timer::Delay;
156

167
#[runtime::test]
178
async fn works() {
@@ -21,59 +12,20 @@ async fn works() {
2112
assert!(i.elapsed() > dur);
2213
}
2314

24-
#[runtime::test]
25-
async fn error_after_inert() {
26-
let t = Timer::new();
27-
let handle = t.handle();
28-
drop(t);
29-
let res = Delay::new_handle(far_future(), handle).await;
30-
assert!(res.is_err());
31-
}
32-
33-
#[runtime::test]
34-
async fn drop_makes_inert() {
35-
let t = Timer::new();
36-
let handle = t.handle();
37-
let timeout = Delay::new_handle(far_future(), handle);
38-
drop(t);
39-
let res = timeout.await;
40-
assert!(res.is_err());
41-
}
42-
4315
#[runtime::test]
4416
async fn reset() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
4517
let i = Instant::now();
4618
let dur = Duration::from_millis(100);
4719
let mut d = Delay::new(dur);
4820

4921
// Allow us to re-use a future
50-
Pin::new(&mut d).await?;
22+
Pin::new(&mut d).await;
5123

5224
assert!(i.elapsed() > dur);
5325

5426
let i = Instant::now();
5527
d.reset(dur);
56-
d.await?;
28+
d.await;
5729
assert!(i.elapsed() > dur);
5830
Ok(())
5931
}
60-
61-
#[runtime::test]
62-
async fn drop_timer_wakes() {
63-
let t = Timer::new();
64-
let handle = t.handle();
65-
let mut timeout = Delay::new_handle(far_future(), handle);
66-
let mut t = Some(t);
67-
let f = poll_fn(move |cx| {
68-
let timeout = unsafe { Pin::new_unchecked(&mut timeout) };
69-
match TryFuture::try_poll(timeout, cx) {
70-
Poll::Pending => {}
71-
other => return other,
72-
}
73-
drop(t.take());
74-
Poll::Pending
75-
});
76-
77-
let res = f.await;
78-
assert_eq!("timer has gone away", res.unwrap_err().description());
79-
}

tests/timeout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use futures_timer::Delay;
77
async fn smoke() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
88
let dur = Duration::from_millis(10);
99
let start = Instant::now();
10-
Delay::new(dur).await?;
10+
Delay::new(dur).await;
1111
assert!(start.elapsed() >= (dur / 2));
1212
Ok(())
1313
}
1414

1515
#[runtime::test]
1616
async fn two() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
1717
let dur = Duration::from_millis(10);
18-
Delay::new(dur).await?;
19-
Delay::new(dur).await?;
18+
Delay::new(dur).await;
19+
Delay::new(dur).await;
2020
Ok(())
2121
}

0 commit comments

Comments
 (0)