Vector

Trait Vector 

Source
pub trait Vector: Sized + Default {
    type Scalar: Scalar;

    // Required methods
    fn dim() -> usize;
    fn get(&self, ind: usize) -> &Self::Scalar;
    fn set(&mut self, ind: usize, val: Self::Scalar);

    // Provided methods
    fn write_property<W: Write>(
        &self,
        property_id: u16,
        w: &mut W,
    ) -> Result<()> { ... }
    fn write_array<W: Write, A: Array<Item = Self>>(
        property_id: u16,
        arr: &A,
        w: &mut W,
    ) -> Result<()> { ... }
    fn read_property<R: Read>(
        &mut self,
        state: State<Bytes>,
        ty: u16,
        r: &mut R,
    ) -> Result<()> { ... }
    fn read_array<R: Read, A: Array<Item = Self>>(
        state: State<Bytes>,
        ty: u16,
        arr: &mut A,
        r: &mut R,
    ) -> Result<()> { ... }
}
Expand description

Implemented by vector types.

Required Associated Types§

Source

type Scalar: Scalar

Scalar type.

Required Methods§

Source

fn dim() -> usize

Returns the number of dimensions.

Source

fn get(&self, ind: usize) -> &Self::Scalar

Gets value.

Source

fn set(&mut self, ind: usize, val: Self::Scalar)

Sets value.

Provided Methods§

Source

fn write_property<W: Write>(&self, property_id: u16, w: &mut W) -> Result<()>

Writes property.

Examples found in repository?
examples/test.rs (line 48)
41fn vector() {
42    let filename = "assets/test-vector.pool";
43
44    let mut file = File::create(filename).unwrap();
45    let data: Vec<[f32; 2]> = vec![[1.0, 2.0], [3.0, 4.0]];
46    Vector::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
47    let val: [u8; 2] = [10; 2];
48    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
49    drop(file);
50
51    let mut file = File::open(filename).unwrap();
52    let mut data: Vec<[f32; 2]> = vec![];
53    let mut val: [u8; 2] = [0; 2];
54    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
55        match prop {
56            ARRAY_PROPERTY => Vector::read_array(state, ty, &mut data, &mut file).unwrap(),
57            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
58            _ => break,
59        }
60    }
61
62    println!("=== Vector ===");
63    println!("data {:?}", data);
64    println!("val {:?}", val);
65}
Source

fn write_array<W: Write, A: Array<Item = Self>>( property_id: u16, arr: &A, w: &mut W, ) -> Result<()>

Writes array.

Examples found in repository?
examples/test.rs (line 46)
41fn vector() {
42    let filename = "assets/test-vector.pool";
43
44    let mut file = File::create(filename).unwrap();
45    let data: Vec<[f32; 2]> = vec![[1.0, 2.0], [3.0, 4.0]];
46    Vector::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
47    let val: [u8; 2] = [10; 2];
48    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
49    drop(file);
50
51    let mut file = File::open(filename).unwrap();
52    let mut data: Vec<[f32; 2]> = vec![];
53    let mut val: [u8; 2] = [0; 2];
54    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
55        match prop {
56            ARRAY_PROPERTY => Vector::read_array(state, ty, &mut data, &mut file).unwrap(),
57            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
58            _ => break,
59        }
60    }
61
62    println!("=== Vector ===");
63    println!("data {:?}", data);
64    println!("val {:?}", val);
65}
Source

fn read_property<R: Read>( &mut self, state: State<Bytes>, ty: u16, r: &mut R, ) -> Result<()>

Reads property.

Examples found in repository?
examples/test.rs (line 57)
41fn vector() {
42    let filename = "assets/test-vector.pool";
43
44    let mut file = File::create(filename).unwrap();
45    let data: Vec<[f32; 2]> = vec![[1.0, 2.0], [3.0, 4.0]];
46    Vector::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
47    let val: [u8; 2] = [10; 2];
48    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
49    drop(file);
50
51    let mut file = File::open(filename).unwrap();
52    let mut data: Vec<[f32; 2]> = vec![];
53    let mut val: [u8; 2] = [0; 2];
54    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
55        match prop {
56            ARRAY_PROPERTY => Vector::read_array(state, ty, &mut data, &mut file).unwrap(),
57            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
58            _ => break,
59        }
60    }
61
62    println!("=== Vector ===");
63    println!("data {:?}", data);
64    println!("val {:?}", val);
65}
Source

fn read_array<R: Read, A: Array<Item = Self>>( state: State<Bytes>, ty: u16, arr: &mut A, r: &mut R, ) -> Result<()>

Reads array.

Examples found in repository?
examples/test.rs (line 56)
41fn vector() {
42    let filename = "assets/test-vector.pool";
43
44    let mut file = File::create(filename).unwrap();
45    let data: Vec<[f32; 2]> = vec![[1.0, 2.0], [3.0, 4.0]];
46    Vector::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
47    let val: [u8; 2] = [10; 2];
48    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
49    drop(file);
50
51    let mut file = File::open(filename).unwrap();
52    let mut data: Vec<[f32; 2]> = vec![];
53    let mut val: [u8; 2] = [0; 2];
54    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
55        match prop {
56            ARRAY_PROPERTY => Vector::read_array(state, ty, &mut data, &mut file).unwrap(),
57            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
58            _ => break,
59        }
60    }
61
62    println!("=== Vector ===");
63    println!("data {:?}", data);
64    println!("val {:?}", val);
65}

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<T: Scalar> Vector for [T; 2]

Source§

type Scalar = T

Source§

fn dim() -> usize

Source§

fn get(&self, ind: usize) -> &T

Source§

fn set(&mut self, ind: usize, val: T)

Source§

impl<T: Scalar> Vector for [T; 3]

Source§

type Scalar = T

Source§

fn dim() -> usize

Source§

fn get(&self, ind: usize) -> &T

Source§

fn set(&mut self, ind: usize, val: T)

Source§

impl<T: Scalar> Vector for [T; 4]

Source§

type Scalar = T

Source§

fn dim() -> usize

Source§

fn get(&self, ind: usize) -> &T

Source§

fn set(&mut self, ind: usize, val: T)

Implementors§