Skip to content

Commit 2f73a82

Browse files
committed
Fuse the damn oneshot receiver
todo: hide the enums after all
1 parent d6ecf83 commit 2f73a82

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ pub mod channel {
6969
pub mod oneshot {
7070
use std::{fmt::Debug, future::Future, io, pin::Pin, task};
7171

72+
use crate::util::FusedOneshotReceiver;
73+
7274
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
7375
let (tx, rx) = tokio::sync::oneshot::channel();
7476
(tx.into(), rx.into())
@@ -152,7 +154,7 @@ pub mod channel {
152154
impl<T> crate::Sender for Sender<T> {}
153155

154156
pub enum Receiver<T> {
155-
Tokio(tokio::sync::oneshot::Receiver<T>),
157+
Tokio(FusedOneshotReceiver<T>),
156158
Boxed(BoxedReceiver<T>),
157159
}
158160

@@ -172,7 +174,7 @@ pub mod channel {
172174
/// Convert a tokio oneshot receiver to a receiver for this crate
173175
impl<T> From<tokio::sync::oneshot::Receiver<T>> for Receiver<T> {
174176
fn from(rx: tokio::sync::oneshot::Receiver<T>) -> Self {
175-
Self::Tokio(rx)
177+
Self::Tokio(FusedOneshotReceiver(rx))
176178
}
177179
}
178180

src/util.rs

+20
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,23 @@ mod varint_util {
290290
}
291291
}
292292
pub use varint_util::{AsyncReadVarintExt, WriteVarintExt};
293+
294+
mod fuse_wrapper {
295+
use std::{future::Future, pin::Pin, task::{Context, Poll}};
296+
297+
pub struct FusedOneshotReceiver<T>(pub tokio::sync::oneshot::Receiver<T>);
298+
299+
impl<T> Future for FusedOneshotReceiver<T> {
300+
type Output = std::result::Result<T, tokio::sync::oneshot::error::RecvError>;
301+
302+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
303+
if self.0.is_terminated() {
304+
// don't panic when polling a terminated receiver
305+
Poll::Pending
306+
} else {
307+
Future::poll(Pin::new(&mut self.0), cx)
308+
}
309+
}
310+
}
311+
}
312+
pub use fuse_wrapper::FusedOneshotReceiver;

0 commit comments

Comments
 (0)