SelfSubvectorView

Trait SelfSubvectorView 

Source
pub trait SelfSubvectorView<T: ?Sized>: Sized + VectorView<T> {
    // Required method
    fn restrict_full(self, range: Range<usize>) -> Self;

    // Provided method
    fn restrict<R: RangeBounds<usize>>(self, range: R) -> Self { ... }
}
Expand description

Trait for VectorViews that support shrinking, i.e. transforming the vector into a subvector of itself.

Note that you can easily get a subvector of a vector by using subvector::SubvectorView, but this will wrap the original type. This makes subvector::SubvectorView unsuitable for some applications, like recursive algorithms.

Note also that SelfSubvectorView::restrict() consumes the current object, thus it is most useful for vectors that implement Clone/Copy, in particular for references to vectors.

This is the VectorView-counterpart to SelfSubvectorFn.

§Example

fn compute_sum_recursive<V: SelfSubvectorView<i32>>(vec: V) -> i32 {
    if vec.len() == 0 {
        0
    } else {
        *vec.at(0) + compute_sum_recursive(vec.restrict(1..))
    }
}
assert_eq!(10, compute_sum_recursive(SubvectorView::new([1, 2, 3, 4])));
assert_eq!(10, compute_sum_recursive(SubvectorView::new(vec![1, 2, 3, 4])));
assert_eq!(10, compute_sum_recursive(SubvectorView::new(&[1, 2, 3, 4, 5][..4])));

Required Methods§

Source

fn restrict_full(self, range: Range<usize>) -> Self

Returns a SelfSubvectorView that represents the elements within the given range of this vector.

Provided Methods§

Source

fn restrict<R: RangeBounds<usize>>(self, range: R) -> Self

Returns a SelfSubvectorView that represents the elements within the given range of this vector.

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<'a, T> SelfSubvectorView<T> for &'a [T]

Source§

fn restrict_full(self, range: Range<usize>) -> Self

Source§

impl<'a, T> SelfSubvectorView<T> for &'a mut [T]

Source§

fn restrict_full(self, range: Range<usize>) -> Self

Implementors§