Struct savefile::Deserializer
source · pub struct Deserializer<'a, R: Read> {
pub reader: &'a mut R,
pub file_version: u32,
pub ephemeral_state: HashMap<TypeId, Box<dyn Any>>,
}
Expand description
Object from which bytes to be deserialized are read.
This is basically just a wrapped std::io::Read
object,
the version number of the file being read, and the
current version number of the data structures in memory.
Fields§
§reader: &'a mut R
The wrapped reader
file_version: u32
The version of the input file
ephemeral_state: HashMap<TypeId, Box<dyn Any>>
This contains ephemeral state that can be used to implement de-duplication of strings or possibly other situations where it is desired to deserialize DAGs.
Implementations§
source§impl<'a, TR: Read> Deserializer<'a, TR>
impl<'a, TR: Read> Deserializer<'a, TR>
sourcepub fn get_state<T: 'static, R: Default + 'static>(&mut self) -> &mut R
pub fn get_state<T: 'static, R: Default + 'static>(&mut self) -> &mut R
This function constructs a temporary state object of type R, and returns a mutable
reference to it. This object can be used to store data that needs to live for the entire
deserialization session. An example is de-duplicating Arc and other reference counted objects.
Out of the box, Arc<str>
has this deduplication done for it.
The type T must be set to the type being deserialized, and is used as a key in a hashmap
separating the state for different types.
source§impl<'a, TR: Read> Deserializer<'a, TR>
impl<'a, TR: Read> Deserializer<'a, TR>
sourcepub fn read_bool(&mut self) -> Result<bool, SavefileError>
pub fn read_bool(&mut self) -> Result<bool, SavefileError>
Reads a u8 and return true if equal to 1
sourcepub fn read_u8(&mut self) -> Result<u8, SavefileError>
pub fn read_u8(&mut self) -> Result<u8, SavefileError>
Reads an u8
sourcepub fn read_u16(&mut self) -> Result<u16, SavefileError>
pub fn read_u16(&mut self) -> Result<u16, SavefileError>
Reads a little endian u16
sourcepub fn read_u32(&mut self) -> Result<u32, SavefileError>
pub fn read_u32(&mut self) -> Result<u32, SavefileError>
Reads a little endian u32
sourcepub fn read_u64(&mut self) -> Result<u64, SavefileError>
pub fn read_u64(&mut self) -> Result<u64, SavefileError>
Reads a little endian u64
sourcepub unsafe fn read_raw_ptr<T: ?Sized>(
&mut self
) -> Result<*const T, SavefileError>
pub unsafe fn read_raw_ptr<T: ?Sized>( &mut self ) -> Result<*const T, SavefileError>
sourcepub unsafe fn read_raw_ptr_mut<T: ?Sized>(
&mut self
) -> Result<*mut T, SavefileError>
pub unsafe fn read_raw_ptr_mut<T: ?Sized>( &mut self ) -> Result<*mut T, SavefileError>
sourcepub fn read_u128(&mut self) -> Result<u128, SavefileError>
pub fn read_u128(&mut self) -> Result<u128, SavefileError>
Reads a little endian u128
sourcepub fn read_i8(&mut self) -> Result<i8, SavefileError>
pub fn read_i8(&mut self) -> Result<i8, SavefileError>
Reads an i8
sourcepub fn read_i16(&mut self) -> Result<i16, SavefileError>
pub fn read_i16(&mut self) -> Result<i16, SavefileError>
Reads a little endian i16
sourcepub fn read_i32(&mut self) -> Result<i32, SavefileError>
pub fn read_i32(&mut self) -> Result<i32, SavefileError>
Reads a little endian i32
sourcepub fn read_i64(&mut self) -> Result<i64, SavefileError>
pub fn read_i64(&mut self) -> Result<i64, SavefileError>
Reads a little endian i64
sourcepub fn read_i128(&mut self) -> Result<i128, SavefileError>
pub fn read_i128(&mut self) -> Result<i128, SavefileError>
Reads a little endian i128
sourcepub fn read_f32(&mut self) -> Result<f32, SavefileError>
pub fn read_f32(&mut self) -> Result<f32, SavefileError>
Reads a little endian f32
sourcepub fn read_f64(&mut self) -> Result<f64, SavefileError>
pub fn read_f64(&mut self) -> Result<f64, SavefileError>
Reads a little endian f64
sourcepub fn read_isize(&mut self) -> Result<isize, SavefileError>
pub fn read_isize(&mut self) -> Result<isize, SavefileError>
Reads an i64 into an isize. For 32 bit architectures, the function fails on overflow.
sourcepub fn read_usize(&mut self) -> Result<usize, SavefileError>
pub fn read_usize(&mut self) -> Result<usize, SavefileError>
Reads an u64 into an usize. For 32 bit architectures, the function fails on overflow.
sourcepub fn read_string(&mut self) -> Result<String, SavefileError>
pub fn read_string(&mut self) -> Result<String, SavefileError>
Reads a 64 bit length followed by an utf8 encoded string. Fails if data is not valid utf8
sourcepub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, SavefileError>
pub fn read_bytes(&mut self, len: usize) -> Result<Vec<u8>, SavefileError>
Reads ‘len’ raw u8 bytes as a Vec<u8>
sourcepub fn read_bytes_to_buf(&mut self, buf: &mut [u8]) -> Result<(), SavefileError>
pub fn read_bytes_to_buf(&mut self, buf: &mut [u8]) -> Result<(), SavefileError>
Reads raw u8 bytes into the given buffer. The buffer size must be equal to the number of bytes desired to be read.
sourcepub fn load<T: WithSchema + Deserialize>(
reader: &mut TR,
version: u32
) -> Result<T, SavefileError>
pub fn load<T: WithSchema + Deserialize>( reader: &mut TR, version: u32 ) -> Result<T, SavefileError>
Deserialize an object of type T from the given reader. Don’t use this method directly, use the crate::load function instead.
sourcepub fn load_noschema<T: Deserialize>(
reader: &mut TR,
version: u32
) -> Result<T, SavefileError>
pub fn load_noschema<T: Deserialize>( reader: &mut TR, version: u32 ) -> Result<T, SavefileError>
Deserialize an object of type T from the given reader. Don’t use this method directly, use the crate::load_noschema function instead.
sourcepub fn bare_deserialize<T: Deserialize>(
reader: &mut TR,
file_version: u32
) -> Result<T, SavefileError>
pub fn bare_deserialize<T: Deserialize>( reader: &mut TR, file_version: u32 ) -> Result<T, SavefileError>
Deserialize data which was serialized using ‘bare_serialize’