Skip to main content

Sender

Struct Sender 

Source
pub struct Sender<T> { /* private fields */ }

Implementations§

Source§

impl<T> Sender<T>

Source

pub fn send(&self, t: T) -> Result<(), SendError<T>>

Non-blocking send.

§Delivery guarantee

Returning Ok(()) indicates that the message was successfully enqueued, but it does not guarantee that the receiver will observe it: if the Receiver is dropped concurrently with this call, the message may be silently discarded. The orphaned value is destructed properly when the underlying queue is dropped; it is not leaked.

Examples found in repository?
examples/sequential-recv.rs (line 21)
17fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
18  let mut counter = 0;
19  let start_time = std::time::SystemTime::now();
20  while counter < MESSAGE_COUNT {
21    sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
22    counter += 1;
23  }
24  let duration = start_time.elapsed().unwrap();
25  let duration_ns
26    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
27  println!("sendfun duration ns: {duration_ns}");
28  println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
29}
More examples
Hide additional examples
examples/sequential-try_recv.rs (line 21)
17fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
18  let mut counter = 0;
19  let start_time = std::time::SystemTime::now();
20  while counter < MESSAGE_COUNT {
21    sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
22    counter += 1;
23  }
24  let duration = start_time.elapsed().unwrap();
25  let duration_ns
26    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
27  println!("sendfun duration ns: {duration_ns}");
28  println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
29}
examples/parallel-recv.rs (line 31)
22fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
23  let mut counter = 0;
24  SENDER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
25  // spin until receiver is started
26  while !RECEIVER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
27    std::hint::spin_loop()
28  }
29  let start_time = std::time::SystemTime::now();
30  while counter < MESSAGE_COUNT {
31    sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
32    counter += 1;
33  }
34  let duration = start_time.elapsed().unwrap();
35  let duration_ns
36    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
37  println!("sendfun duration ns: {duration_ns}");
38  println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
39}
examples/parallel-try_recv.rs (line 31)
22fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
23  let mut counter = 0;
24  SENDER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
25  // spin until receiver is started
26  while !RECEIVER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
27    std::hint::spin_loop()
28  }
29  let start_time = std::time::SystemTime::now();
30  while counter < MESSAGE_COUNT {
31    sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
32    counter += 1;
33  }
34  let duration = start_time.elapsed().unwrap();
35  let duration_ns
36    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
37  println!("sendfun duration ns: {duration_ns}");
38  println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
39}

Trait Implementations§

Source§

impl<T> Debug for Sender<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Sender<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Sender<T>

§

impl<T> !RefUnwindSafe for Sender<T>

§

impl<T> Send for Sender<T>
where T: Send,

§

impl<T> !Sync for Sender<T>

§

impl<T> Unpin for Sender<T>

§

impl<T> UnsafeUnpin for Sender<T>

§

impl<T> !UnwindSafe for Sender<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.