pub struct Sender<T> { /* private fields */ }Expand description
Sending side of the channel with sync API. It’s possible to convert it to
async AsyncSender with as_async, to_async or clone_async based on
software requirement.
§Examples
let (sender, _r) = kanal_plus::bounded::<u64>(0);
let sync_sender=sender.clone_async();Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn send_many(&self, elements: &mut VecDeque<T>) -> Result<(), SendError<T>>
pub fn send_many(&self, elements: &mut VecDeque<T>) -> Result<(), SendError<T>>
Sends multiple elements from a VecDeque into the channel.
This method attempts to push as many items from elements as possible,
respecting the channel’s capacity and the current state of the receiver
side. It behaves similarly to repeatedly calling send for each
element, but is more efficient because it holds the internal lock
only while it can make progress.
- If the channel is closed (no receivers), the first element that cannot
be sent is returned inside
SendError. - If the channel’s queue becomes full, mutex guard will be released and
remaining elements stay in the supplied
VecDequeto be send in a signal. - Elements are taken from the front of the deque (FIFO order). When the internal queue has spare capacity, elements are moved from the back of the deque into the internal queue to fill it as quickly as possible.
§Examples
use std::collections::VecDeque;
// Create a bounded channel with capacity 3
let (s, r) = kanal_plus::bounded::<i32>(3);
// Move the sender and the buffer into a new thread that will
// push as many items as the channel can accept.
let handle = std::thread::spawn(move || {
/// // Prepare a deque with several values
let mut buf = VecDeque::from(vec![1, 2, 3, 4, 5]);
// `send_many` consumes items from the front of the deque.
// It returns `Ok(())` when all possible items have been sent
// or `Err` if the channel is closed. Here we unwrap the result
// because the channel stays alive for the whole test.
s.send_many(&mut buf).unwrap();
// Return the (now‑partially‑filled) buffer so the main thread can
// inspect the remaining elements.
buf
});
// In the current thread we receive the three items that fit into the
// channel's capacity.
assert_eq!(r.recv().unwrap(), 1);
assert_eq!(r.recv().unwrap(), 2);
assert_eq!(r.recv().unwrap(), 3);
std::thread::sleep(std::time::Duration::from_millis(100));
// Sender now written two more items into the channel queue and exited.
let remaining = handle.join().expect("sender thread panicked");
assert_eq!(r.len(), 2);
assert_eq!(r.recv().unwrap(), 4);
assert_eq!(r.recv().unwrap(), 5);The function returns Ok(()) when all elements have been successfully
transferred, or Err(SendError<T>) containing the first element that
could not be sent (typically because the receiver side has been
closed).
Sourcepub fn send_timeout(
&self,
data: T,
duration: Duration,
) -> Result<(), SendTimeoutError<T>>
pub fn send_timeout( &self, data: T, duration: Duration, ) -> Result<(), SendTimeoutError<T>>
Sends data to the channel with a deadline, if send fails then the object will be dropped. you can use send_option_timeout if you like to keep the object in case of timeout.
§Examples
s.send_timeout("Hello",Duration::from_millis(500)).unwrap();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_async(&self) -> AsyncSender<T>
pub fn clone_async(&self) -> AsyncSender<T>
Clones Sender as the async version of it and returns it
Sourcepub fn to_async(self) -> AsyncSender<T>
pub fn to_async(self) -> AsyncSender<T>
Converts Sender to AsyncSender and returns it
§Examples
let (s, r) = kanal_plus::bounded(0);
co(async move {
let s=s.to_async();
s.send("World").await;
});
let name=r.recv()?;
println!("Hello {}!",name);Sourcepub fn as_async(&self) -> &AsyncSender<T>
pub fn as_async(&self) -> &AsyncSender<T>
Borrows Sender as AsyncSender and returns it
§Examples
let (s, r) = kanal_plus::bounded(0);
co(async move {
s.as_async().send("World").await;
});
let name=r.recv()?;
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);