Skip to main content

IterateByValueFrom

Trait IterateByValueFrom 

Source
pub trait IterateByValueFrom: for<'a> IterateByValueFromGat<'a> {
    // Required method
    fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>;
}
Expand description

A trait for obtaining a by-value iterator starting from a given position.

This is a version of IterateByValue that is useful for types in which obtaining a global iterator and skipping is expensive.

We cannot provide a skip-based default implementation because the returned type is not necessarily the same type as that returned by IterateByValue::iter_value, but you are free to implement iter_value_from that way.

§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: IterateByValueFrom + for<'a> IterateByValueFromGat<'a, IterFrom = std::slice::Iter<'a, usize>>,
{
    let _: std::slice::Iter<'_, usize> = s.iter_value_from(0);
}

You can also bind the iterator using traits:

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValueFrom + for<'a> IterateByValueFromGat<'a, IterFrom: ExactSizeIterator>,
{
    let _ = s.iter_value_from(0).len();
}

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

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValueFrom,
   for<'a> IterFrom<'a, S>: ExactSizeIterator,
{
    let _ = s.iter_value_from(0).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: IterateByValueFrom + for<'a> IterateByValueFromGat<'a, Item = usize>,
{
    let _: Option<usize> = s.iter_value_from(0).next();
}

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

use value_traits::iter::*;

fn f<S>(s: S) where
   S: IterateByValueFrom,
   for<'a> IterFrom<'a, S>: Iterator<Item = usize>,
{
    let _: Option<usize> = s.iter_value_from(0).next();
}

Required Methods§

Source

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Returns an iterator on values starting at the given position.

Iterating from the length of the structure is allowed, and returns an exhausted iterator.

§Panics

This method will panic if from is greater than the length of the structure being iterated, mirroring the behavior of slice indexing.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<S: IterateByValueFrom + ?Sized> IterateByValueFrom for Arc<S>

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

impl<S: IterateByValueFrom + ?Sized> IterateByValueFrom for Box<S>

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

impl<S: IterateByValueFrom + ?Sized> IterateByValueFrom for Rc<S>

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

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

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

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

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

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

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

impl<T: Clone> IterateByValueFrom for [T]

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

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

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Source§

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

Source§

fn iter_value_from(&self, from: usize) -> IterFrom<'_, Self>

Implementors§