Skip to content

Commit 5b077ed

Browse files
authored
Merge pull request #136 from EspressoSystems/rm/add-try-send
Add `try_send`
2 parents 27473ac + e1b09e8 commit 5b077ed

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "async-compatibility-layer"
33
description = "an abstraction layer for using both async-std and tokio"
44
authors = ["Espresso Systems <[email protected]>"]
5-
version = "1.2.0"
5+
version = "1.2.1"
66
edition = "2021"
77
license = "MIT"
88

src/channel.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ compile_error!(
2626
"async_channel_impl = 'tokio' requires tokio runtime, e. g. async_executor_impl = 'tokio'"
2727
);
2828

29-
pub use bounded::{bounded, BoundedStream, Receiver, RecvError, SendError, Sender, TryRecvError};
29+
pub use bounded::{
30+
bounded, BoundedStream, Receiver, RecvError, SendError, Sender, TryRecvError, TrySendError,
31+
};
3032
pub use oneshot::{oneshot, OneShotReceiver, OneShotRecvError, OneShotSender, OneShotTryRecvError};
3133
pub use unbounded::{
3234
unbounded, UnboundedReceiver, UnboundedRecvError, UnboundedSendError, UnboundedSender,

src/channel/bounded.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use futures::Stream;
55
/// inner module, used to group feature-specific imports
66
#[cfg(async_channel_impl = "tokio")]
77
mod inner {
8-
pub use tokio::sync::mpsc::error::{SendError, TryRecvError};
8+
pub use tokio::sync::mpsc::error::{SendError, TryRecvError, TrySendError};
99

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

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

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

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

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

@@ -121,6 +121,15 @@ impl<T> Sender<T> {
121121

122122
result
123123
}
124+
125+
/// Try to send a value over the channel. Will return immediately if the channel is full.
126+
///
127+
/// # Errors
128+
/// - If the channel is full
129+
/// - If the channel is dropped
130+
pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
131+
self.0.try_send(msg)
132+
}
124133
}
125134

126135
impl<T> Receiver<T> {

0 commit comments

Comments
 (0)