Matrix

Trait Matrix 

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

    // Required methods
    fn dim() -> [usize; 2];
    fn get(&self, row: usize, col: usize) -> &Self::Scalar;
    fn set(&mut self, row: usize, col: 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 matrix types.

Required Associated Types§

Source

type Scalar: Scalar

Scalar type.

Required Methods§

Source

fn dim() -> [usize; 2]

Returns dimensions.

Source

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

Gets value.

Source

fn set(&mut self, row: usize, col: 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 74)
67fn matrix() {
68    let filename = "assets/test-matrix.pool";
69
70    let mut file = File::create(filename).unwrap();
71    let data: Vec<[[f32; 2]; 2]> = vec![[[1.0, 2.0], [3.0, 4.0]]];
72    Matrix::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
73    let val: [[u8; 2]; 2] = [[10; 2]; 2];
74    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
75    drop(file);
76
77    let mut file = File::open(filename).unwrap();
78    let mut data: Vec<[[f32; 2]; 2]> = vec![];
79    let mut val: [[u8; 2]; 2] = [[0; 2]; 2];
80
81    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
82        match prop {
83            ARRAY_PROPERTY => Matrix::read_array(state, ty, &mut data, &mut file).unwrap(),
84            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
85            _ => break,
86        }
87    }
88
89    println!("=== Matrix ===");
90    println!("data {:?}", data);
91    println!("val {:?}", val);
92}
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 72)
67fn matrix() {
68    let filename = "assets/test-matrix.pool";
69
70    let mut file = File::create(filename).unwrap();
71    let data: Vec<[[f32; 2]; 2]> = vec![[[1.0, 2.0], [3.0, 4.0]]];
72    Matrix::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
73    let val: [[u8; 2]; 2] = [[10; 2]; 2];
74    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
75    drop(file);
76
77    let mut file = File::open(filename).unwrap();
78    let mut data: Vec<[[f32; 2]; 2]> = vec![];
79    let mut val: [[u8; 2]; 2] = [[0; 2]; 2];
80
81    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
82        match prop {
83            ARRAY_PROPERTY => Matrix::read_array(state, ty, &mut data, &mut file).unwrap(),
84            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
85            _ => break,
86        }
87    }
88
89    println!("=== Matrix ===");
90    println!("data {:?}", data);
91    println!("val {:?}", val);
92}
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 84)
67fn matrix() {
68    let filename = "assets/test-matrix.pool";
69
70    let mut file = File::create(filename).unwrap();
71    let data: Vec<[[f32; 2]; 2]> = vec![[[1.0, 2.0], [3.0, 4.0]]];
72    Matrix::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
73    let val: [[u8; 2]; 2] = [[10; 2]; 2];
74    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
75    drop(file);
76
77    let mut file = File::open(filename).unwrap();
78    let mut data: Vec<[[f32; 2]; 2]> = vec![];
79    let mut val: [[u8; 2]; 2] = [[0; 2]; 2];
80
81    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
82        match prop {
83            ARRAY_PROPERTY => Matrix::read_array(state, ty, &mut data, &mut file).unwrap(),
84            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
85            _ => break,
86        }
87    }
88
89    println!("=== Matrix ===");
90    println!("data {:?}", data);
91    println!("val {:?}", val);
92}
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 83)
67fn matrix() {
68    let filename = "assets/test-matrix.pool";
69
70    let mut file = File::create(filename).unwrap();
71    let data: Vec<[[f32; 2]; 2]> = vec![[[1.0, 2.0], [3.0, 4.0]]];
72    Matrix::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
73    let val: [[u8; 2]; 2] = [[10; 2]; 2];
74    val.write_property(SINGLE_PROPERTY, &mut file).unwrap();
75    drop(file);
76
77    let mut file = File::open(filename).unwrap();
78    let mut data: Vec<[[f32; 2]; 2]> = vec![];
79    let mut val: [[u8; 2]; 2] = [[0; 2]; 2];
80
81    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
82        match prop {
83            ARRAY_PROPERTY => Matrix::read_array(state, ty, &mut data, &mut file).unwrap(),
84            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
85            _ => break,
86        }
87    }
88
89    println!("=== Matrix ===");
90    println!("data {:?}", data);
91    println!("val {:?}", val);
92}

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> Matrix for [[T; 2]; 2]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 2]; 3]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 2]; 4]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 3]; 2]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

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

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 3]; 4]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 4]; 2]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

impl<T: Scalar> Matrix for [[T; 4]; 3]

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Source§

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

Source§

type Scalar = T

Source§

fn dim() -> [usize; 2]

Source§

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

Source§

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

Implementors§