Skip to content

Commit b62f4ff

Browse files
committed
add try_send
1 parent 27473ac commit b62f4ff

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/channel/bounded.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,20 @@ 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 async fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
131+
#[cfg(async_channel_impl = "flume")]
132+
let result = self.0.try_send(msg);
133+
#[cfg(not(all(async_channel_impl = "flume")))]
134+
let result = self.0.try_send(msg).await;
135+
136+
result
137+
}
124138
}
125139

126140
impl<T> Receiver<T> {

0 commit comments

Comments
 (0)