State

Struct State 

Source
pub struct State<T = TypeFormat>(/* private fields */);
Expand description

Stores the state for writing and reading.

Implementations§

Source§

impl State

Source

pub fn new() -> State

Creates a new state.

Source

pub fn read<R: Read>(r: &mut R) -> Result<(Option<State<Bytes>>, u16, u16)>

Reads type format and property.

Returns None in first argument if there is no more data.

Examples found in repository?
examples/test.rs (line 28)
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}
40
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}
66
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

pub fn write_type_format<W: Write>( self, type_format: u16, w: &mut W, ) -> Result<State<PropertyId>>

Writes type format.

Source

pub fn read_type_format<R: Read>( self, type_format: &mut u16, r: &mut R, ) -> Result<State<PropertyId>>

Reads type format.

Source

pub fn end_type_formats<W: Write>(self, w: &mut W) -> Result<()>

Ends writing state.

Source§

impl State<PropertyId>

Source

pub fn write_property_id<W: Write>( self, property_id: u16, w: &mut W, ) -> Result<State<Bytes>>

Writes property id.

Source

pub fn read_property_id<R: Read>( self, property_id: &mut u16, r: &mut R, ) -> Result<State<Bytes>>

Reads property id.

Source§

impl State<Bytes>

Source

pub fn write_bytes<W: Write>( self, bytes: u64, w: &mut W, ) -> Result<State<OffsetInstanceId>>

Writes number of bytes in data.

Source

pub fn read_bytes<R: Read>( self, bytes: &mut u64, r: &mut R, ) -> Result<State<OffsetInstanceId>>

Reads bytes.

Source

pub fn end_bytes<W: Write>(self, w: &mut W) -> Result<State<TypeFormat>>

Ends byte block.

Source

pub fn has_end_bytes<R: Read>(self, r: &mut R) -> Result<State<TypeFormat>>

Checks if this is the end of bytes.

Source§

impl State<OffsetInstanceId>

Source

pub fn write_offset_instance_id<W: Write>( self, offset_instance_id: u64, w: &mut W, ) -> Result<State<Data>>

Writes offset instance id.

Source

pub fn read_offset_instance_id<R: Read>( self, offset_instance_id: &mut u64, r: &mut R, ) -> Result<State<Data>>

Reads offset instance id.

Source§

impl State<Data>

Source

pub fn write_data<W: Write>(self, data: &[u8], w: &mut W) -> Result<State<Data>>

Writes data.

Source

pub fn end_data(self) -> State<Bytes>

End of data.

Auto Trait Implementations§

§

impl<T> Freeze for State<T>

§

impl<T> RefUnwindSafe for State<T>
where T: RefUnwindSafe,

§

impl<T> Send for State<T>
where T: Send,

§

impl<T> Sync for State<T>
where T: Sync,

§

impl<T> Unpin for State<T>
where T: Unpin,

§

impl<T> UnwindSafe for State<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.