pub trait AsVector<'a, U: 'a> {
// Required methods
fn vec_size(&self) -> usize;
fn vec_at(&self, i: usize) -> U;
}
Expand description
Defines a trait to handle Vector-like data
§Example
use plotpy::AsVector;
fn sum<'a, T, U>(array: &'a T) -> f64
where
T: AsVector<'a, U>,
U: 'a + Into<f64>,
{
let mut res = 0.0;
let m = array.vec_size();
for i in 0..m {
res += array.vec_at(i).into();
}
res
}
// heap-allocated 1D array (vector)
let x = vec![1.0, 2.0, 3.0];
assert_eq!(sum(&x), 6.0);
// heap-allocated 1D array (slice)
let y: &[f64] = &[10.0, 20.0, 30.0];
assert_eq!(sum(&y), 60.0);
// stack-allocated (fixed-size) 2D array
let z = [100.0, 200.0, 300.0];
assert_eq!(sum(&z), 600.0);
Required Methods§
Implementations on Foreign Types§
source§impl<'a, U> AsVector<'a, U> for &'a [U]where
U: 'a + Copy,
impl<'a, U> AsVector<'a, U> for &'a [U]where
U: 'a + Copy,
Defines a heap-allocated 1D array (slice)
source§impl<'a, U> AsVector<'a, U> for Vec<U>where
U: 'a + Copy,
impl<'a, U> AsVector<'a, U> for Vec<U>where
U: 'a + Copy,
Defines a heap-allocated 1D array (vector)
source§impl<'a, U, const M: usize> AsVector<'a, U> for [U; M]where
U: 'a + Copy,
impl<'a, U, const M: usize> AsVector<'a, U> for [U; M]where
U: 'a + Copy,
Defines a stack-allocated (fixed-size) 1D array