From ae26394aa0c0d0cf31b418ec11a6f214370de0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Laitl?= Date: Wed, 24 Feb 2021 18:45:09 +0100 Subject: [PATCH] Temporary: Recipient: remove remaining_capacity(), implement send_if_not_full() differently I expect that at least the second point will be resolved (more correctly) by #20. Not sure whether we want to keep Recipient::remaining_capacity() or whether it will be eventually superseded by #22. This a temporary commit just to make prototyping of #11 easier. --- src/lib.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2d956dd..69321fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -671,22 +671,16 @@ impl Recipient { /// frequently full (slow consumer), but would still like to be notified if a /// different error occurs (e.g. disconnection). pub fn send_if_not_full>(&self, message: N) -> Result<(), SendError> { - if self.remaining_capacity().unwrap_or(usize::max_value()) > 1 { - return self.send(message); - } - - Ok(()) + self.try_send(message).or_else(|err| match err { + SendError::Full => Ok(()), + other => Err(other), + }) } pub fn stop(&self) -> Result<(), SendError> { self.control_tx.send(Control::Stop).map_err(|_| SendError::Disconnected) } - // TODO(ryo): Properly support the concept of priority channels. - pub fn remaining_capacity(&self) -> Option { - self.message_tx.capacity().map(|capacity| capacity - self.message_tx.len()) - } - pub fn control_addr(&self) -> ControlAddr { ControlAddr { control_tx: self.control_tx.clone() } }