Skip to content

Commit

Permalink
Merge pull request #136 from EspressoSystems/rm/add-try-send
Browse files Browse the repository at this point in the history
Add `try_send`
  • Loading branch information
rob-maron authored Jul 10, 2024
2 parents 27473ac + e1b09e8 commit 5b077ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "async-compatibility-layer"
description = "an abstraction layer for using both async-std and tokio"
authors = ["Espresso Systems <[email protected]>"]
version = "1.2.0"
version = "1.2.1"
edition = "2021"
license = "MIT"

Expand Down
4 changes: 3 additions & 1 deletion src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ compile_error!(
"async_channel_impl = 'tokio' requires tokio runtime, e. g. async_executor_impl = 'tokio'"
);

pub use bounded::{bounded, BoundedStream, Receiver, RecvError, SendError, Sender, TryRecvError};
pub use bounded::{
bounded, BoundedStream, Receiver, RecvError, SendError, Sender, TryRecvError, TrySendError,
};
pub use oneshot::{oneshot, OneShotReceiver, OneShotRecvError, OneShotSender, OneShotTryRecvError};
pub use unbounded::{
unbounded, UnboundedReceiver, UnboundedRecvError, UnboundedSendError, UnboundedSender,
Expand Down
15 changes: 12 additions & 3 deletions src/channel/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::Stream;
/// inner module, used to group feature-specific imports
#[cfg(async_channel_impl = "tokio")]
mod inner {
pub use tokio::sync::mpsc::error::{SendError, TryRecvError};
pub use tokio::sync::mpsc::error::{SendError, TryRecvError, TrySendError};

use tokio::sync::mpsc::{Receiver as InnerReceiver, Sender as InnerSender};

Expand Down Expand Up @@ -47,7 +47,7 @@ mod inner {
/// inner module, used to group feature-specific imports
#[cfg(async_channel_impl = "flume")]
mod inner {
pub use flume::{RecvError, SendError, TryRecvError};
pub use flume::{RecvError, SendError, TryRecvError, TrySendError};

use flume::{r#async::RecvStream, Receiver as InnerReceiver, Sender as InnerSender};

Expand Down Expand Up @@ -77,7 +77,7 @@ mod inner {
/// inner module, used to group feature-specific imports
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
mod inner {
pub use async_std::channel::{RecvError, SendError, TryRecvError};
pub use async_std::channel::{RecvError, SendError, TryRecvError, TrySendError};

use async_std::channel::{Receiver as InnerReceiver, Sender as InnerSender};

Expand Down Expand Up @@ -121,6 +121,15 @@ impl<T> Sender<T> {

result
}

/// Try to send a value over the channel. Will return immediately if the channel is full.
///
/// # Errors
/// - If the channel is full
/// - If the channel is dropped
pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
self.0.try_send(msg)
}
}

impl<T> Receiver<T> {
Expand Down

0 comments on commit 5b077ed

Please sign in to comment.