Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return SendError from Sender::send on disconnect. #901

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions src/sync/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ use crate::sync::WakerSet;
/// let (s, r) = channel(1);
///
/// // This call returns immediately because there is enough space in the channel.
/// s.send(1usize).await;
/// s.send(1usize).await.unwrap();
///
/// task::spawn(async move {
/// // This call will have to wait because the channel is full.
/// // It will be able to complete only after the first message is received.
/// s.send(2).await;
/// s.send(2).await.unwrap();
/// });
///
/// task::sleep(Duration::from_secs(1)).await;
Expand Down Expand Up @@ -124,8 +124,8 @@ impl<T> Sender<T> {
/// let (s, r) = channel(1);
///
/// task::spawn(async move {
/// s.send(1).await;
/// s.send(2).await;
/// s.send(1).await.unwrap();
/// s.send(2).await.unwrap();
/// });
///
/// assert_eq!(r.recv().await?, 1);
Expand All @@ -135,7 +135,7 @@ impl<T> Sender<T> {
/// # Ok(())
/// # }) }
/// ```
pub async fn send(&self, msg: T) {
pub async fn send(&self, msg: T) -> Result<(), SendError<T>> {
struct SendFuture<'a, T> {
channel: &'a Channel<T>,
msg: Option<T>,
Expand All @@ -145,7 +145,7 @@ impl<T> Sender<T> {
impl<T> Unpin for SendFuture<'_, T> {}

impl<T> Future for SendFuture<'_, T> {
type Output = ();
type Output = Result<(), SendError<T>>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
Expand All @@ -158,10 +158,9 @@ impl<T> Sender<T> {

// Try sending the message.
match self.channel.try_send(msg) {
Ok(()) => return Poll::Ready(()),
Ok(()) => return Poll::Ready(Ok(())),
Err(TrySendError::Disconnected(msg)) => {
self.msg = Some(msg);
return Poll::Pending;
return Poll::Ready(Err(SendError(msg)));
}
Err(TrySendError::Full(msg)) => {
self.msg = Some(msg);
Expand Down Expand Up @@ -244,7 +243,7 @@ impl<T> Sender<T> {
/// let (s, r) = channel(1);
///
/// assert!(s.is_empty());
/// s.send(0).await;
/// s.send(0).await.unwrap();
/// assert!(!s.is_empty());
/// #
/// # })
Expand All @@ -265,7 +264,7 @@ impl<T> Sender<T> {
/// let (s, r) = channel(1);
///
/// assert!(!s.is_full());
/// s.send(0).await;
/// s.send(0).await.unwrap();
/// assert!(s.is_full());
/// #
/// # })
Expand All @@ -286,8 +285,8 @@ impl<T> Sender<T> {
/// let (s, r) = channel(2);
/// assert_eq!(s.len(), 0);
///
/// s.send(1).await;
/// s.send(2).await;
/// s.send(1).await.unwrap();
/// s.send(2).await.unwrap();
/// assert_eq!(s.len(), 2);
/// #
/// # })
Expand Down Expand Up @@ -350,9 +349,9 @@ impl<T> fmt::Debug for Sender<T> {
/// let (s, r) = channel(100);
///
/// task::spawn(async move {
/// s.send(1usize).await;
/// s.send(1usize).await.unwrap();
/// task::sleep(Duration::from_secs(1)).await;
/// s.send(2).await;
/// s.send(2).await.unwrap();
/// });
///
/// assert_eq!(r.recv().await?, 1); // Received immediately.
Expand Down Expand Up @@ -390,8 +389,8 @@ impl<T> Receiver<T> {
/// let (s, r) = channel(1);
///
/// task::spawn(async move {
/// s.send(1usize).await;
/// s.send(2).await;
/// s.send(1usize).await.unwrap();
/// s.send(2).await.unwrap();
/// // Then we drop the sender
/// });
///
Expand Down Expand Up @@ -450,7 +449,7 @@ impl<T> Receiver<T> {
///
/// let (s, r) = channel(1);
///
/// s.send(1u8).await;
/// s.send(1u8).await.unwrap();
///
/// assert!(r.try_recv().is_ok());
/// assert!(r.try_recv().is_err());
Expand Down Expand Up @@ -487,7 +486,7 @@ impl<T> Receiver<T> {
/// let (s, r) = channel(1);
///
/// assert!(r.is_empty());
/// s.send(0).await;
/// s.send(0).await.unwrap();
/// assert!(!r.is_empty());
/// #
/// # })
Expand All @@ -508,7 +507,7 @@ impl<T> Receiver<T> {
/// let (s, r) = channel(1);
///
/// assert!(!r.is_full());
/// s.send(0).await;
/// s.send(0).await.unwrap();
/// assert!(r.is_full());
/// #
/// # })
Expand All @@ -529,8 +528,8 @@ impl<T> Receiver<T> {
/// let (s, r) = channel(2);
/// assert_eq!(r.len(), 0);
///
/// s.send(1).await;
/// s.send(2).await;
/// s.send(1).await.unwrap();
/// s.send(2).await.unwrap();
/// assert_eq!(r.len(), 2);
/// #
/// # })
Expand Down Expand Up @@ -1001,6 +1000,13 @@ pub enum TrySendError<T> {
Disconnected(T),
}

/// An error returned from the `send` method when it is disconnected.
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
#[derive(PartialEq, Eq, Debug)]
pub struct SendError<T>(pub T);


impl<T> Error for TrySendError<T> {}

impl<T> Debug for TrySendError<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mod rwlock;

cfg_unstable! {
pub use barrier::{Barrier, BarrierWaitResult};
pub use channel::{channel, Sender, Receiver, RecvError, TryRecvError, TrySendError};
pub use channel::{channel, Sender, Receiver, RecvError, TryRecvError, SendError, TrySendError};
pub use condvar::Condvar;

mod barrier;
Expand Down
46 changes: 23 additions & 23 deletions tests/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ fn smoke() {
task::block_on(async {
let (s, r) = channel(1);

s.send(7).await;
s.send(7).await.unwrap();
assert_eq!(r.recv().await.unwrap(), 7);

s.send(8).await;
s.send(8).await.unwrap();
assert_eq!(r.recv().await.unwrap(), 8);

drop(s);
Expand All @@ -39,7 +39,7 @@ fn smoke() {
task::block_on(async {
let (s, r) = channel(10);
drop(r);
s.send(1).await;
s.send(1).await.unwrap();
});
}

Expand Down Expand Up @@ -67,7 +67,7 @@ fn len_empty_full() {
assert_eq!(r.is_empty(), true);
assert_eq!(r.is_full(), false);

s.send(()).await;
s.send(()).await.unwrap();

assert_eq!(s.len(), 1);
assert_eq!(s.is_empty(), false);
Expand All @@ -76,7 +76,7 @@ fn len_empty_full() {
assert_eq!(r.is_empty(), false);
assert_eq!(r.is_full(), false);

s.send(()).await;
s.send(()).await.unwrap();

assert_eq!(s.len(), 2);
assert_eq!(s.is_empty(), false);
Expand Down Expand Up @@ -112,9 +112,9 @@ fn recv() {
});

task::sleep(ms(1500)).await;
s.send(7).await;
s.send(8).await;
s.send(9).await;
s.send(7).await.unwrap();
s.send(8).await.unwrap();
s.send(9).await.unwrap();
})
}

Expand All @@ -125,13 +125,13 @@ fn send() {
let (s, r) = channel(1);

spawn(async move {
s.send(7).await;
s.send(7).await.unwrap();
task::sleep(ms(1000)).await;
s.send(8).await;
s.send(8).await.unwrap();
task::sleep(ms(1000)).await;
s.send(9).await;
s.send(9).await.unwrap();
task::sleep(ms(1000)).await;
s.send(10).await;
s.send(10).await.unwrap();
});

task::sleep(ms(1500)).await;
Expand All @@ -147,9 +147,9 @@ fn recv_after_disconnect() {
task::block_on(async {
let (s, r) = channel(100);

s.send(1).await;
s.send(2).await;
s.send(3).await;
s.send(1).await.unwrap();
s.send(2).await.unwrap();
s.send(3).await.unwrap();

drop(s);

Expand All @@ -174,7 +174,7 @@ fn len() {

for _ in 0..CAP / 10 {
for i in 0..50 {
s.send(i).await;
s.send(i).await.unwrap();
assert_eq!(s.len(), i + 1);
}

Expand All @@ -188,7 +188,7 @@ fn len() {
assert_eq!(r.len(), 0);

for i in 0..CAP {
s.send(i).await;
s.send(i).await.unwrap();
assert_eq!(s.len(), i + 1);
}

Expand All @@ -211,7 +211,7 @@ fn len() {
});

for i in 0..COUNT {
s.send(i).await;
s.send(i).await.unwrap();
let len = s.len();
assert!(len <= CAP);
}
Expand Down Expand Up @@ -256,7 +256,7 @@ fn spsc() {
});

for i in 0..COUNT {
s.send(i).await;
s.send(i).await.unwrap();
}
drop(s);

Expand Down Expand Up @@ -292,7 +292,7 @@ fn mpmc() {
let s = s.clone();
tasks.push(spawn(async move {
for i in 0..COUNT {
s.send(i).await;
s.send(i).await.unwrap();
}
}));
}
Expand Down Expand Up @@ -320,7 +320,7 @@ fn oneshot() {
let c2 = spawn(async move { s.send(0).await });

c1.await;
c2.await;
c2.await.unwrap();
}
})
}
Expand Down Expand Up @@ -360,13 +360,13 @@ fn drops() {
});

for _ in 0..steps {
s.send(DropCounter).await;
s.send(DropCounter).await.unwrap();
}

child.await;

for _ in 0..additional {
s.send(DropCounter).await;
s.send(DropCounter).await.unwrap();
}

assert_eq!(DROPS.load(Ordering::SeqCst), steps);
Expand Down
4 changes: 2 additions & 2 deletions tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn merging_delayed_streams_work() {

task::block_on(async move {
task::sleep(std::time::Duration::from_millis(500)).await;
sender.send(92).await;
sender.send(92).await.unwrap();
drop(sender);
let xs = t.await;
assert_eq!(xs, vec![92])
Expand All @@ -55,7 +55,7 @@ fn merging_delayed_streams_work() {

task::block_on(async move {
task::sleep(std::time::Duration::from_millis(500)).await;
sender.send(92).await;
sender.send(92).await.unwrap();
drop(sender);
let xs = t.await;
assert_eq!(xs, vec![92])
Expand Down