IterateByValue

Trait IterateByValue 

Source
pub trait IterateByValue: for<'a> IterateByValueGat<'a> {
    // Required method
    fn iter_value(&self) -> Iter<'_, Self>;
}
Expand description

A trait for obtaining a by-value iterator.

This trait necessary as all standard Rust containers already have IntoIterator-based methods for obtaining reference-based iterators.

Note that iter_value returns a standard iterator. However, the intended semantics is that the iterator will return values.

If you need to iterate from a given position, and you can implement such an iterator more efficiently, please consider IterateByValueFrom.

§Binding the Iterator Type

To bind the iterator type or the type of its items you need to use higher-rank trait bounds, as in:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValue + for<'a> IterateByValueGat<'a, Iter = std::slice::Iter<'a, usize>>,
{
    let _: std::slice::Iter<'_, usize> = s.iter_value();
}

You can also bind the iterator using traits:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValue + for<'a> IterateByValueGat<'a, Iter: ExactSizeIterator>,
{
    let _ = s.iter_value().len();
}

In this case, you can equivalently use the Iter type alias, which might be more concise:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValue,
   for<'a> Iter<'a, S>: ExactSizeIterator,
{
    let _ = s.iter_value().len();
}

As it happens for IntoIterator, it is possible to bind the type of the items returned by the iterator without referring to the iterator type itself:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValue + for<'a> IterateByValueGat<'a, Item = usize>,
{
    let _: Option<usize> = s.iter_value().next();
}

Once again, the Iter type alias can be used to make the bound more concise:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValue,
   for<'a> Iter<'a, S>: Iterator<Item = usize>,
{
    let _: Option<usize> = s.iter_value().next();
}

Required Methods§

Source

fn iter_value(&self) -> Iter<'_, Self>

Returns an iterator on values.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: Clone> IterateByValue for [T]

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: Clone> IterateByValue for Box<[T]>

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: Clone> IterateByValue for VecDeque<T>

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: Clone> IterateByValue for Vec<T>

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: Clone, const N: usize> IterateByValue for [T; N]

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: IterateByValue + ?Sized> IterateByValue for &T

Source§

fn iter_value(&self) -> Iter<'_, Self>

Source§

impl<T: IterateByValue + ?Sized> IterateByValue for &mut T

Source§

fn iter_value(&self) -> Iter<'_, Self>

Implementors§