pub struct State<T = TypeFormat>(/* private fields */);Expand description
Stores the state for writing and reading.
Implementations§
Source§impl State
impl State
Sourcepub fn read<R: Read>(r: &mut R) -> Result<(Option<State<Bytes>>, u16, u16)>
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}Sourcepub fn write_type_format<W: Write>(
self,
type_format: u16,
w: &mut W,
) -> Result<State<PropertyId>>
pub fn write_type_format<W: Write>( self, type_format: u16, w: &mut W, ) -> Result<State<PropertyId>>
Writes type format.
Sourcepub fn read_type_format<R: Read>(
self,
type_format: &mut u16,
r: &mut R,
) -> Result<State<PropertyId>>
pub fn read_type_format<R: Read>( self, type_format: &mut u16, r: &mut R, ) -> Result<State<PropertyId>>
Reads type format.
Source§impl State<PropertyId>
impl State<PropertyId>
Source§impl State<Bytes>
impl State<Bytes>
Sourcepub fn write_bytes<W: Write>(
self,
bytes: u64,
w: &mut W,
) -> Result<State<OffsetInstanceId>>
pub fn write_bytes<W: Write>( self, bytes: u64, w: &mut W, ) -> Result<State<OffsetInstanceId>>
Writes number of bytes in data.
Sourcepub fn read_bytes<R: Read>(
self,
bytes: &mut u64,
r: &mut R,
) -> Result<State<OffsetInstanceId>>
pub fn read_bytes<R: Read>( self, bytes: &mut u64, r: &mut R, ) -> Result<State<OffsetInstanceId>>
Reads bytes.
Sourcepub fn has_end_bytes<R: Read>(self, r: &mut R) -> Result<State<TypeFormat>>
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>
impl State<OffsetInstanceId>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more