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.
§Related traits
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§
Provided Methods§
Sourceunsafe fn at_unchecked<'a>(&self, i: usize) -> &T
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.
Sourcefn specialize_sparse<'a, Op: SparseVectorViewOperation<T>>(
&'a self,
_op: Op,
) -> Result<Op::Output<'a>, ()>
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.
Sourcefn as_iter<'a>(&'a self) -> VectorFnIter<VectorViewFn<'a, Self, T>, &'a T> ⓘ
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.
Sourcefn as_fn<'a>(&'a self) -> VectorViewFn<'a, Self, T>
fn as_fn<'a>(&'a self) -> VectorViewFn<'a, Self, T>
Converts this vector into a VectorFn
that yields references &T
.
Sourcefn as_slice<'a>(&'a self) -> Option<&'a [T]>where
T: Sized,
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.
Sourcefn into_clone_ring_els<R: RingStore>(
self,
ring: R,
) -> CloneElFn<Self, T, CloneRingEl<R>>
fn into_clone_ring_els<R: RingStore>( self, ring: R, ) -> CloneElFn<Self, T, CloneRingEl<R>>
Moves this vector into a VectorFn
that clones ring elements on access using
the given ring.
Sourcefn clone_ring_els<'a, R: RingStore>(
&'a self,
ring: R,
) -> CloneElFn<&'a Self, T, CloneRingEl<R>>
fn clone_ring_els<'a, R: RingStore>( &'a self, ring: R, ) -> CloneElFn<&'a Self, T, CloneRingEl<R>>
Converts this vector into a VectorFn
that clones ring elements on access using
the given ring.
Sourcefn into_clone_els_by<F>(self, clone_entry: F) -> CloneElFn<Self, T, F>
fn into_clone_els_by<F>(self, clone_entry: F) -> CloneElFn<Self, T, F>
Moves this vector into a VectorFn
that clones elements on access using
the given function.
Sourcefn clone_els_by<'a, F>(&'a self, clone_entry: F) -> CloneElFn<&'a Self, T, F>
fn clone_els_by<'a, F>(&'a self, clone_entry: F) -> CloneElFn<&'a Self, T, F>
Converts this vector into a VectorFn
that clones elements on access using
the given function.
Sourcefn into_clone_els(self) -> CloneElFn<Self, T, CloneValue>
fn into_clone_els(self) -> CloneElFn<Self, T, CloneValue>
Moves this vector into a VectorFn
that clones elements on access.
Sourcefn clone_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
fn clone_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
Converts this vector into a VectorFn
that clones elements on access.
Sourcefn into_copy_els(self) -> CloneElFn<Self, T, CloneValue>
fn into_copy_els(self) -> CloneElFn<Self, T, CloneValue>
Moves this vector into a VectorFn
that copies elements on access.
Sourcefn copy_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
fn copy_els<'a>(&'a self) -> CloneElFn<&'a Self, T, CloneValue>
Converts this vector into a VectorFn
that copies elements on access.
Sourcefn map_view<F: for<'a> Fn(&'a T) -> &'a U, U>(
self,
func: F,
) -> VectorViewMap<Self, T, U, F>where
Self: Sized,
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 VectorView
s
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()));
Sourcefn step_by_view(self, step_by: usize) -> StepBy<Self, T>where
Self: Sized,
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.