VectorView

Trait VectorView 

Source
pub trait VectorView<T: ?Sized> {
Show 17 methods // Required methods fn len(&self) -> usize; fn at(&self, i: usize) -> &T; // Provided methods unsafe fn at_unchecked<'a>(&self, i: usize) -> &T { ... } fn specialize_sparse<'a, Op: SparseVectorViewOperation<T>>( &'a self, _op: Op, ) -> Result<Op::Output<'a>, ()> { ... } fn as_iter<'a>(&'a self) -> VectorFnIter<VectorViewFn<'a, Self, T>, &'a T> { ... } fn as_fn<'a>(&'a self) -> VectorViewFn<'a, Self, T> { ... } fn as_slice<'a>(&'a self) -> Option<&'a [T]> where T: Sized { ... } fn into_clone_ring_els<R: RingStore>( self, ring: R, ) -> CloneElFn<Self, T, CloneRingEl<R>> where Self: Sized, T: Sized, R::Type: RingBase<Element = T> { ... } fn clone_ring_els<'a, R: RingStore>( &'a self, ring: R, ) -> CloneElFn<&'a Self, T, CloneRingEl<R>> where T: Sized, R::Type: RingBase<Element = T> { ... } fn into_clone_els_by<F>(self, clone_entry: F) -> CloneElFn<Self, T, F> where Self: Sized, T: Sized, F: Fn(&T) -> T { ... } fn clone_els_by<'a, F>( &'a self, clone_entry: F, ) -> CloneElFn<&'a Self, T, F> where T: Sized, F: Fn(&T) -> T { ... } fn into_clone_els(self) -> CloneElFn<Self, T, CloneValue> where Self: Sized, T: Sized + Clone { ... } fn clone_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue> where T: Sized + Clone { ... } fn into_copy_els(self) -> CloneElFn<Self, T, CloneValue> where Self: Sized, T: Sized + Copy { ... } fn copy_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue> where T: Sized + Copy { ... } fn map_view<F: for<'a> Fn(&'a T) -> &'a U, U>( self, func: F, ) -> VectorViewMap<Self, T, U, F> where Self: Sized { ... } fn step_by_view(self, step_by: usize) -> StepBy<Self, T> where Self: Sized { ... }
}
Expand description

A trait for objects that provides random-position read access to a 1-dimensional sequence (or vector) of elements.

Other traits that represent sequences are

  • ExactSizeIterator: Returns elements by value; Since elements are moved, each element is returned only once, and they must be queried in order.
  • VectorFn: Also returns elements by value, but assumes that the underlying structure produces a new element whenever a position is queried. This allows accessing positions multiple times and in a random order, but depending on the represented items, it might require cloning an element on each access.

Apart from that, there are also the subtraits VectorViewMut and SwappableVectorViewMut that allow mutating the underlying sequence (but still don’t allow moving elements out). Finally, there is SelfSubvectorView, which directly supports taking subvectors.

§Example

fn compute_sum<V: VectorView<i32>>(vec: V) -> i32 {
    let mut result = 0;
    for i in 0..vec.len() {
        result += vec.at(i);
    }
    return result;
}
assert_eq!(10, compute_sum([1, 2, 3, 4]));
assert_eq!(10, compute_sum(vec![1, 2, 3, 4]));
assert_eq!(10, compute_sum(&[1, 2, 3, 4, 5][..4]));

Required Methods§

Source

fn len(&self) -> usize

Source

fn at(&self, i: usize) -> &T

Provided Methods§

Source

unsafe fn at_unchecked<'a>(&self, i: usize) -> &T

Returns a refernce to the i-th entry of the vector view, causing UB if i >= self.len().

§Safety

Same as for slice::get_unchecked(). More concretely, calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

Source

fn specialize_sparse<'a, Op: SparseVectorViewOperation<T>>( &'a self, _op: Op, ) -> Result<Op::Output<'a>, ()>

Calls op with self if this vector view supports sparse access. Otherwise, () is returned.

This is basically a workaround that enables users to specialize on V: VectorViewSparse, even though specialization currently does not support this.

Source

fn as_iter<'a>(&'a self) -> VectorFnIter<VectorViewFn<'a, Self, T>, &'a T>

Returns an iterator over all elements in this vector.

NB: Not called iter() to prevent name conflicts, since many containers (e.g. Vec<T>) have a function iter() and implement VectorView. As a result, whenever VectorView is in scope, calling any one iter() would require fully-qualified call syntax.

Source

fn as_fn<'a>(&'a self) -> VectorViewFn<'a, Self, T>

Converts this vector into a VectorFn that yields references &T.

Source

fn as_slice<'a>(&'a self) -> Option<&'a [T]>
where T: Sized,

If the underlying data of this VectorView can be represented as a slice, returns it. Otherwise, None is returned.

Source

fn into_clone_ring_els<R: RingStore>( self, ring: R, ) -> CloneElFn<Self, T, CloneRingEl<R>>
where Self: Sized, T: Sized, R::Type: RingBase<Element = T>,

Moves this vector into a VectorFn that clones ring elements on access using the given ring.

Source

fn clone_ring_els<'a, R: RingStore>( &'a self, ring: R, ) -> CloneElFn<&'a Self, T, CloneRingEl<R>>
where T: Sized, R::Type: RingBase<Element = T>,

Converts this vector into a VectorFn that clones ring elements on access using the given ring.

Source

fn into_clone_els_by<F>(self, clone_entry: F) -> CloneElFn<Self, T, F>
where Self: Sized, T: Sized, F: Fn(&T) -> T,

Moves this vector into a VectorFn that clones elements on access using the given function.

Source

fn clone_els_by<'a, F>(&'a self, clone_entry: F) -> CloneElFn<&'a Self, T, F>
where T: Sized, F: Fn(&T) -> T,

Converts this vector into a VectorFn that clones elements on access using the given function.

Source

fn into_clone_els(self) -> CloneElFn<Self, T, CloneValue>
where Self: Sized, T: Sized + Clone,

Moves this vector into a VectorFn that clones elements on access.

Source

fn clone_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
where T: Sized + Clone,

Converts this vector into a VectorFn that clones elements on access.

Source

fn into_copy_els(self) -> CloneElFn<Self, T, CloneValue>
where Self: Sized, T: Sized + Copy,

Moves this vector into a VectorFn that copies elements on access.

Source

fn copy_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
where T: Sized + Copy,

Converts this vector into a VectorFn that copies elements on access.

Source

fn map_view<F: for<'a> Fn(&'a T) -> &'a U, U>( self, func: F, ) -> VectorViewMap<Self, T, U, F>
where Self: Sized,

Creates a new VectorView whose elements are the results of the given function applied to the elements of this vector.

The most common use case is a projection on contained elements. Since VectorViews provide elements by reference, this is much less powerful than Iterator::map() or VectorFn::map_fn(), since the function cannot return created elements.

Called map_view() to prevent name conflicts with Iterator::map().

§Example
use feanor_math::seq::*;
fn foo<V: VectorView<i64>>(data: V) {
    // some logic
}
let data = vec![Some(1), Some(2), Some(3)];
// the `as_ref()` is necessary, since we have to return a reference
foo(data.map_view(|x| x.as_ref().unwrap()));
Source

fn step_by_view(self, step_by: usize) -> StepBy<Self, T>
where Self: Sized,

Called step_by_view() to prevent name conflicts with Iterator::step_by().

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: ?Sized, V: ?Sized + VectorView<T>> VectorView<T> for &'a V

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn specialize_sparse<'b, Op: SparseVectorViewOperation<T>>( &'b self, op: Op, ) -> Result<Op::Output<'b>, ()>

Source§

fn as_slice<'b>(&'b self) -> Option<&'b [T]>
where T: Sized,

Source§

impl<'a, T: ?Sized, V: ?Sized + VectorView<T>> VectorView<T> for &'a mut V

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn specialize_sparse<'b, Op: SparseVectorViewOperation<T>>( &'b self, op: Op, ) -> Result<Op::Output<'b>, ()>

Source§

fn as_slice<'b>(&'b self) -> Option<&'b [T]>
where T: Sized,

Source§

impl<T> VectorView<T> for [T]

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn as_slice<'a>(&'a self) -> Option<&'a [T]>
where T: Sized,

Source§

impl<T, A: Allocator> VectorView<T> for Vec<T, A>

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn as_slice<'a>(&'a self) -> Option<&'a [T]>
where T: Sized,

Source§

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

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn as_slice<'a>(&'a self) -> Option<&'a [T]>
where T: Sized,

Source§

impl<T: ?Sized, V: ?Sized + VectorView<T>> VectorView<T> for Box<V>

Source§

fn len(&self) -> usize

Source§

fn at(&self, i: usize) -> &T

Source§

unsafe fn at_unchecked(&self, i: usize) -> &T

Source§

fn specialize_sparse<'a, Op: SparseVectorViewOperation<T>>( &'a self, op: Op, ) -> Result<Op::Output<'a>, ()>

Source§

fn as_slice<'a>(&'a self) -> Option<&'a [T]>
where T: Sized,

Implementors§

Source§

impl<'a, V, T> VectorView<T> for Column<'a, V, T>
where V: AsPointerToSlice<T>,

Source§

impl<'a, V, T> VectorView<T> for ColumnMut<'a, V, T>
where V: AsPointerToSlice<T>,

Source§

impl<C: RingStore, J: RingStore, A: Allocator + Clone> VectorView<C> for ZnBase<C, J, A>

Source§

impl<C: RingStore, J: RingStore, A: Allocator + Clone> VectorView<C> for Zn<C, J, A>

Source§

impl<R: RingStore> VectorView<<<R as RingStore>::Type as RingBase>::Element> for SparseMapVector<R>

Source§

impl<V: VectorView<T>, T: ?Sized> VectorView<T> for StepBy<V, T>

Source§

impl<V: VectorView<T>, T: ?Sized> VectorView<T> for SubvectorView<V, T>

Source§

impl<V: VectorView<T>, T: ?Sized, U: ?Sized, F: for<'a> Fn(&'a T) -> &'a U> VectorView<U> for VectorViewMap<V, T, U, F>

Source§

impl<V: VectorViewMut<T>, T: ?Sized, U: ?Sized, F_const: for<'a> Fn(&'a T) -> &'a U, F_mut: for<'a> FnMut(&'a mut T) -> &'a mut U> VectorView<U> for VectorViewMapMut<V, T, U, F_const, F_mut>