Skip to content

Commit 2f1984a

Browse files
committed
Error instead
1 parent abf64b7 commit 2f1984a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/delay.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::fmt;
77
use std::future::Future;
8+
use std::io;
89
use std::pin::Pin;
910
use std::sync::atomic::AtomicUsize;
1011
use std::sync::atomic::Ordering::SeqCst;
@@ -125,16 +126,19 @@ impl Delay {
125126
}
126127

127128
impl Future for Delay {
128-
type Output = ();
129+
type Output = io::Result<()>;
129130

130131
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
131132
let state = match self.state {
132133
Some(ref state) => state,
133-
None => panic!("timer has gone away"),
134+
None => {
135+
let err = Err(io::Error::new(io::ErrorKind::Other, "timer has gone away"));
136+
return Poll::Ready(err);
137+
}
134138
};
135139

136140
if state.state.load(SeqCst) & 1 != 0 {
137-
return Poll::Ready(());
141+
return Poll::Ready(Ok(()));
138142
}
139143

140144
state.waker.register(&cx.waker());
@@ -143,8 +147,11 @@ impl Future for Delay {
143147
// state. If we've fired the first bit is set, and if we've been
144148
// invalidated the second bit is set.
145149
match state.state.load(SeqCst) {
146-
n if n & 0b01 != 0 => Poll::Ready(()),
147-
n if n & 0b10 != 0 => panic!("timer has gone away"),
150+
n if n & 0b01 != 0 => Poll::Ready(Ok(())),
151+
n if n & 0b10 != 0 => Poll::Ready(Err(io::Error::new(
152+
io::ErrorKind::Other,
153+
"timer has gone away",
154+
))),
148155
_ => Poll::Pending,
149156
}
150157
}

0 commit comments

Comments
 (0)