Skip to main content

Receiver

Struct Receiver 

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

Implementations§

Source§

impl<T> Receiver<T>

Source

pub fn try_recv(&self) -> Result<T, TryRecvError>

Non-blocking receive, returns Err(TryRecvError::Empty) if buffer was empty; will continue to receive pending messages from a disconnected channel until it is empty, at which point further calls to this function will return Err(TryRecvError::Disconnected).

Examples found in repository?
examples/sequential-try_recv.rs (line 34)
31fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
32  let start_time = std::time::SystemTime::now();
33  loop {
34    match receiver.try_recv() {
35      Ok  (_m) => (),
36      Err (unbounded_spsc::TryRecvError::Empty) => (),
37      Err (unbounded_spsc::TryRecvError::Disconnected) => break
38    }
39  }
40  let duration = start_time.elapsed().unwrap();
41  let duration_ns
42    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
43  println!("recvfun duration ns: {duration_ns}");
44  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
45  println!("buffer ending capacity: {}", receiver.capacity());
46}
More examples
Hide additional examples
examples/parallel-try_recv.rs (line 49)
41fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
42  RECEIVER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
43  // spin until sender is started
44  while !SENDER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
45    std::hint::spin_loop()
46  }
47  let start_time = std::time::SystemTime::now();
48  loop {
49    match receiver.try_recv() {
50      Ok  (_m) => (),
51      Err (unbounded_spsc::TryRecvError::Empty) => (),
52      Err (unbounded_spsc::TryRecvError::Disconnected) => break
53    }
54  }
55  let duration = start_time.elapsed().unwrap();
56  let duration_ns
57    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
58  println!("recvfun duration ns: {duration_ns}");
59  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
60  println!("buffer ending capacity: {}", receiver.capacity());
61}
Source

pub fn recv(&self) -> Result<T, RecvError>

Block waiting if no messages are pending in the buffer.

Examples found in repository?
examples/sequential-recv.rs (line 33)
31fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
32  let start_time = std::time::SystemTime::now();
33  while let Ok (_m) = receiver.recv() { }
34  let duration = start_time.elapsed().unwrap();
35  let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
36    as u64;
37  println!("recvfun duration ns: {duration_ns}");
38  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
39  println!("buffer ending capacity: {}", receiver.capacity());
40}
More examples
Hide additional examples
examples/parallel-recv.rs (line 48)
41fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
42  RECEIVER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
43  // spin until sender is started
44  while !SENDER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
45    std::hint::spin_loop()
46  }
47  let start_time = std::time::SystemTime::now();
48  while let Ok (_m) = receiver.recv() { }
49  let duration = start_time.elapsed().unwrap();
50  let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
51    as u64;
52  println!("recvfun duration ns: {duration_ns}");
53  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
54  println!("buffer ending capacity: {}", receiver.capacity());
55}
Source

pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>

Source

pub const fn iter(&self) -> Iter<'_, T>

Source

pub const fn try_iter(&self) -> TryIter<'_, T>

Source

pub fn capacity(&self) -> usize

Examples found in repository?
examples/sequential-recv.rs (line 39)
31fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
32  let start_time = std::time::SystemTime::now();
33  while let Ok (_m) = receiver.recv() { }
34  let duration = start_time.elapsed().unwrap();
35  let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
36    as u64;
37  println!("recvfun duration ns: {duration_ns}");
38  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
39  println!("buffer ending capacity: {}", receiver.capacity());
40}
More examples
Hide additional examples
examples/sequential-try_recv.rs (line 45)
31fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
32  let start_time = std::time::SystemTime::now();
33  loop {
34    match receiver.try_recv() {
35      Ok  (_m) => (),
36      Err (unbounded_spsc::TryRecvError::Empty) => (),
37      Err (unbounded_spsc::TryRecvError::Disconnected) => break
38    }
39  }
40  let duration = start_time.elapsed().unwrap();
41  let duration_ns
42    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
43  println!("recvfun duration ns: {duration_ns}");
44  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
45  println!("buffer ending capacity: {}", receiver.capacity());
46}
examples/parallel-recv.rs (line 54)
41fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
42  RECEIVER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
43  // spin until sender is started
44  while !SENDER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
45    std::hint::spin_loop()
46  }
47  let start_time = std::time::SystemTime::now();
48  while let Ok (_m) = receiver.recv() { }
49  let duration = start_time.elapsed().unwrap();
50  let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
51    as u64;
52  println!("recvfun duration ns: {duration_ns}");
53  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
54  println!("buffer ending capacity: {}", receiver.capacity());
55}
examples/parallel-try_recv.rs (line 60)
41fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
42  RECEIVER_STARTED.store (true, std::sync::atomic::Ordering::SeqCst);
43  // spin until sender is started
44  while !SENDER_STARTED.load (std::sync::atomic::Ordering::SeqCst) {
45    std::hint::spin_loop()
46  }
47  let start_time = std::time::SystemTime::now();
48  loop {
49    match receiver.try_recv() {
50      Ok  (_m) => (),
51      Err (unbounded_spsc::TryRecvError::Empty) => (),
52      Err (unbounded_spsc::TryRecvError::Disconnected) => break
53    }
54  }
55  let duration = start_time.elapsed().unwrap();
56  let duration_ns
57    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
58  println!("recvfun duration ns: {duration_ns}");
59  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
60  println!("buffer ending capacity: {}", receiver.capacity());
61}

Trait Implementations§

Source§

impl<T> Debug for Receiver<T>

Source§

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

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

impl<T> Drop for Receiver<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
Source§

impl<'a, T> IntoIterator for &'a Receiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for Receiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Receiver<T>

§

impl<T> !RefUnwindSafe for Receiver<T>

§

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

§

impl<T> !Sync for Receiver<T>

§

impl<T> Unpin for Receiver<T>

§

impl<T> UnsafeUnpin for Receiver<T>

§

impl<T> !UnwindSafe for Receiver<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.