Struct TryIter

pub struct TryIter<'a, T: 'a> { pub(in ::sync::mpsc) rx: &'a Receiver<T> }

An iterator that attempts to yield all pending values for a Receiver, created by try_iter.

None will be returned when there are no pending values remaining or if the corresponding channel has hung up.

This iterator will never block the caller in order to wait for data to become available. Instead, it will return None. (See std::sync for precise guarantees on blocking.)

Examples

use std::sync::mpsc::channel;
use std::thread;
use std::time::Duration;

let (sender, receiver) = channel();

// Nothing is in the buffer yet
assert!(receiver.try_iter().next().is_none());
println!("Nothing in the buffer...");

thread::spawn(move || {
    sender.send(1).unwrap();
    sender.send(2).unwrap();
    sender.send(3).unwrap();
});

println!("Going to sleep...");
thread::sleep(Duration::from_secs(2)); // block for two seconds

for x in receiver.try_iter() {
    println!("Got: {x}");
}

Fields

rx: &'a Receiver<T>

Trait Implementations

impl<'a, T> Iterator for TryIter<'a, T>

type Item = T;
fn next(&mut self) -> Option<T>

impl<'a, T: Debug + 'a> Debug for TryIter<'a, T>

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

Auto Trait Implementations

impl<'a, T> !Send for TryIter<'a, T>

impl<'a, T> !Sync for TryIter<'a, T>

impl<'a, T> Freeze for TryIter<'a, T> where &'a Receiver<T>: Freeze,

impl<'a, T> RefUnwindSafe for TryIter<'a, T> where &'a Receiver<T>: RefUnwindSafe,

impl<'a, T> Unpin for TryIter<'a, T> where &'a Receiver<T>: Unpin,

impl<'a, T> UnsafeUnpin for TryIter<'a, T> where &'a Receiver<T>: UnsafeUnpin,

impl<'a, T> UnwindSafe for TryIter<'a, T> where &'a Receiver<T>: UnwindSafe,

Blanket Implementations

impl<I> IntoIterator for TryIter<'a, T> where I: Iterator,

type Item = <I as Iterator>::Item;
type IntoIter = I;
fn into_iter(self) -> I

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for TryIter<'a, T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for TryIter<'a, T> where T: ?Sized,

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

impl<T> From<T> for TryIter<'a, T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for TryIter<'a, T> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for TryIter<'a, T>

impl<T, U> Into<U> for TryIter<'a, T> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for TryIter<'a, T> where U: Into<T>,

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

impl<T, U> TryInto<U> for TryIter<'a, T> where U: TryFrom<T>,

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