Scalar

Trait Scalar 

Source
pub trait Scalar: Sized + Default {
    // Required methods
    fn ty() -> Type;
    fn write<W: Write>(&self, w: &mut W) -> Result<usize>;
    fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>;

    // 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 scalar values.

Required Methods§

Source

fn ty() -> Type

Type of scalar.

Source

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Write to binary.

Source

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Read from binary.

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 21)
15fn scalar() {
16    let filename = "assets/test-scalar.pool";
17
18    let mut file = File::create(filename).unwrap();
19    let data: Vec<f32> = vec![1.0, 2.0, 3.0];
20    Scalar::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
21    (10 as u8).write_property(SINGLE_PROPERTY, &mut file).unwrap();
22    drop(file);
23
24    let mut file = File::open(filename).unwrap();
25
26    let mut data: Vec<f32> = vec![];
27    let mut val: u8 = 0;
28    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
29        match prop {
30            ARRAY_PROPERTY => Scalar::read_array(state, ty, &mut data, &mut file).unwrap(),
31            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
32            _ => break,
33        }
34    }
35
36    println!("=== Scalar ===");
37    println!("data {:?}", data);
38    println!("val {:?}", val);
39}
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 20)
15fn scalar() {
16    let filename = "assets/test-scalar.pool";
17
18    let mut file = File::create(filename).unwrap();
19    let data: Vec<f32> = vec![1.0, 2.0, 3.0];
20    Scalar::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
21    (10 as u8).write_property(SINGLE_PROPERTY, &mut file).unwrap();
22    drop(file);
23
24    let mut file = File::open(filename).unwrap();
25
26    let mut data: Vec<f32> = vec![];
27    let mut val: u8 = 0;
28    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
29        match prop {
30            ARRAY_PROPERTY => Scalar::read_array(state, ty, &mut data, &mut file).unwrap(),
31            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
32            _ => break,
33        }
34    }
35
36    println!("=== Scalar ===");
37    println!("data {:?}", data);
38    println!("val {:?}", val);
39}
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 31)
15fn scalar() {
16    let filename = "assets/test-scalar.pool";
17
18    let mut file = File::create(filename).unwrap();
19    let data: Vec<f32> = vec![1.0, 2.0, 3.0];
20    Scalar::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
21    (10 as u8).write_property(SINGLE_PROPERTY, &mut file).unwrap();
22    drop(file);
23
24    let mut file = File::open(filename).unwrap();
25
26    let mut data: Vec<f32> = vec![];
27    let mut val: u8 = 0;
28    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
29        match prop {
30            ARRAY_PROPERTY => Scalar::read_array(state, ty, &mut data, &mut file).unwrap(),
31            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
32            _ => break,
33        }
34    }
35
36    println!("=== Scalar ===");
37    println!("data {:?}", data);
38    println!("val {:?}", val);
39}
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 30)
15fn scalar() {
16    let filename = "assets/test-scalar.pool";
17
18    let mut file = File::create(filename).unwrap();
19    let data: Vec<f32> = vec![1.0, 2.0, 3.0];
20    Scalar::write_array(ARRAY_PROPERTY, &data, &mut file).unwrap();
21    (10 as u8).write_property(SINGLE_PROPERTY, &mut file).unwrap();
22    drop(file);
23
24    let mut file = File::open(filename).unwrap();
25
26    let mut data: Vec<f32> = vec![];
27    let mut val: u8 = 0;
28    while let Ok((Some(state), ty, prop)) = State::read(&mut file) {
29        match prop {
30            ARRAY_PROPERTY => Scalar::read_array(state, ty, &mut data, &mut file).unwrap(),
31            SINGLE_PROPERTY => val.read_property(state, ty, &mut file).unwrap(),
32            _ => break,
33        }
34    }
35
36    println!("=== Scalar ===");
37    println!("data {:?}", data);
38    println!("val {:?}", val);
39}

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 Scalar for f32

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for f64

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for i8

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for i16

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for i32

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for i64

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for u8

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for u16

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for u32

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Source§

impl Scalar for u64

Source§

fn ty() -> Type

Source§

fn write<W: Write>(&self, w: &mut W) -> Result<usize>

Source§

fn read<R: Read>(&mut self, r: &mut R) -> Result<usize>

Implementors§