pub struct AsyncSender<T> { /* private fields */ }Expand description
Implementations§
Source§impl<T> AsyncSender<T>
impl<T> AsyncSender<T>
Sourcepub fn send(&self, data: T) -> SendFuture<'_, T> ⓘ
pub fn send(&self, data: T) -> SendFuture<'_, T> ⓘ
Sends data asynchronously to the channel.
§Examples
s.send(1).await?;
assert_eq!(r.recv().await?,1);Sourcepub fn send_many<'a, 'b>(
&'a self,
elements: &'b mut VecDeque<T>,
) -> SendManyFuture<'a, 'b, T> ⓘ
pub fn send_many<'a, 'b>( &'a self, elements: &'b mut VecDeque<T>, ) -> SendManyFuture<'a, 'b, T> ⓘ
Sends multiple elements from a VecDeque into the channel
asynchronously.
This method consumes the provided VecDeque by repeatedly popping
elements from its front and sending each one over the channel. The
operation completes when the deque is empty or when the channel is
closed.
§Examples
let (s, r) = kanal_plus::bounded_async(3);
let handle = co(async move {
let mut elems = VecDeque::from(vec![10, 20, 30, 40, 50]);
// Send all elements in the deque
s.send_many(&mut elems).await.unwrap();
});
// Receive the values in the same order they were sent
assert_eq!(r.recv().await?, 10);
assert_eq!(r.recv().await?, 20);
assert_eq!(r.recv().await?, 30);
//panic!("here");
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
// Now the sender has sent the remaining elements
//handle.await.unwrap();
assert_eq!(r.recv().await?, 40);
assert_eq!(r.recv().await?, 50);
Sourcepub fn try_send(&self, data: T) -> Result<(), SendTimeoutError<T>>
pub fn try_send(&self, data: T) -> Result<(), SendTimeoutError<T>>
Tries sending to the channel without waiting on the waitlist, if
send fails then the object will be dropped. It returns Ok(true) in
case of a successful operation and Ok(false) for a failed one, or
error in case that channel is closed. Important note: this function
is not lock-free as it acquires a mutex guard of the channel
internal for a short time.
§Examples
let (s, r) = kanal_plus::bounded(0);
let t=spawn( move || {
loop{
if s.try_send(1).is_ok() {
break;
}
}
});
assert_eq!(r.recv()?,1);Sourcepub fn try_send_realtime(&self, data: T) -> Result<(), SendTimeoutError<T>>
pub fn try_send_realtime(&self, data: T) -> Result<(), SendTimeoutError<T>>
Tries sending to the channel without waiting on the waitlist or for
the internal mutex, if send fails then the object will be dropped.
It returns Ok(true) in case of a successful operation and
Ok(false) for a failed one, or error in case that channel is
closed. Do not use this function unless you know exactly what you
are doing.
§Examples
let (s, r) = kanal_plus::bounded(0);
let t=spawn( move || {
loop{
if s.try_send_realtime(1).is_ok() {
break;
}
}
});
assert_eq!(r.recv()?,1);Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Returns whether the receive side of the channel is closed or not.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
drop(r); // drop receiver and disconnect the receive side from the channel
assert_eq!(s.is_disconnected(),true);Sourcepub fn clone_sync(&self) -> Sender<T>
pub fn clone_sync(&self) -> Sender<T>
Clones AsyncSender as Sender with sync api of it.
§Examples
let (s, r) = kanal_plus::unbounded_async();
let sync_sender=s.clone_sync();
// JUST FOR EXAMPLE IT IS WRONG TO USE SYNC INSTANCE IN ASYNC CONTEXT
sync_sender.send(1)?;
assert_eq!(r.recv().await?,1);Sourcepub fn to_sync(self) -> Sender<T>
pub fn to_sync(self) -> Sender<T>
Converts AsyncSender to Sender and returns it.
§Examples
let (s, r) = kanal_plus::bounded_async(0);
// move to sync environment
std::thread::spawn(move || {
let s=s.to_sync();
s.send("World")?;
anyhow::Ok(())
});
let name=r.recv().await?;
println!("Hello {}!",name);Sourcepub fn as_sync(&self) -> &Sender<T>
pub fn as_sync(&self) -> &Sender<T>
Borrows AsyncSender as Sender and returns it.
§Examples
let (s, r) = kanal_plus::bounded_async(0);
// move to sync environment
std::thread::spawn(move || {
s.as_sync().send("World")?;
anyhow::Ok(())
});
let name=r.recv().await?;
println!("Hello {}!",name);Sourcepub fn is_bounded(&self) -> bool
pub fn is_bounded(&self) -> bool
Returns whether the channel is bounded or not.
§Examples
let (s, r) = kanal_plus::bounded::<u64>(0);
assert_eq!(s.is_bounded(),true);
assert_eq!(r.is_bounded(),true);let (s, r) = kanal_plus::unbounded::<u64>();
assert_eq!(s.is_bounded(),false);
assert_eq!(r.is_bounded(),false);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns length of the queue.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
assert_eq!(s.len(),0);
assert_eq!(r.len(),0);
s.send(10);
assert_eq!(s.len(),1);
assert_eq!(r.len(),1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the channel queue is empty or not.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
assert_eq!(s.is_empty(),true);
assert_eq!(r.is_empty(),true);Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns whether the channel queue is full or not full channels will block on send and recv calls it always returns true for zero sized channels.
§Examples
let (s, r) = kanal_plus::bounded(1);
s.send("Hi!").unwrap();
assert_eq!(s.is_full(),true);
assert_eq!(r.is_full(),true);Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns capacity of channel (not the queue) for unbounded channels, it will return usize::MAX.
§Examples
let (s, r) = kanal_plus::bounded::<u64>(0);
assert_eq!(s.capacity(),0);
assert_eq!(r.capacity(),0);let (s, r) = kanal_plus::unbounded::<u64>();
assert_eq!(s.capacity(),usize::MAX);
assert_eq!(r.capacity(),usize::MAX);Sourcepub fn receiver_count(&self) -> usize
pub fn receiver_count(&self) -> usize
Returns count of alive receiver instances of the channel.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
let receiver_clone=r.clone();
assert_eq!(r.receiver_count(),2);Sourcepub fn sender_count(&self) -> usize
pub fn sender_count(&self) -> usize
Returns count of alive sender instances of the channel.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
let sender_clone=s.clone();
assert_eq!(r.sender_count(),2);Sourcepub fn close(&self) -> Result<(), CloseError>
pub fn close(&self) -> Result<(), CloseError>
Closes the channel completely on both sides and terminates waiting signals.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
// closes channel on both sides and has same effect as r.close();
s.close().unwrap();
assert_eq!(r.is_closed(),true);
assert_eq!(s.is_closed(),true);Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns whether the channel is closed on both side of send and receive or not.
§Examples
let (s, r) = kanal_plus::unbounded::<u64>();
// closes channel on both sides and has same effect as r.close();
s.close();
assert_eq!(r.is_closed(),true);
assert_eq!(s.is_closed(),true);Trait Implementations§
Source§impl<T> Clone for AsyncSender<T>
Available on crate feature async only.
impl<T> Clone for AsyncSender<T>
async only.Source§impl<T> Debug for AsyncSender<T>
Available on crate feature async only.
impl<T> Debug for AsyncSender<T>
async only.