pub trait ReadFrom<T>: Sealed + Sized{
type ReadError: Error;
// Required method
fn read_from<C: ?Sized + Config>(
reader: &mut T,
configuration: &C,
) -> Result<Self, Self::ReadError>;
}v0 only.Expand description
Trait for reading a value from bytes with option to fail.
Required Associated Types§
Required Methods§
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<R> ReadFrom<R> for String
impl<R> ReadFrom<R> for String
Source§fn read_from<C: ?Sized + Config>(
reader: &mut R,
configuration: &C,
) -> Result<Self, Self::ReadError>
fn read_from<C: ?Sized + Config>( reader: &mut R, configuration: &C, ) -> Result<Self, Self::ReadError>
Reads a string from the given byte stream according to the given configuration.
§Example
let file = vec![0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F];
let mut file_reader = file.as_slice();
let configuration = OverridableConfig::default();
let string = String::read_from(&mut file_reader, &configuration)?;
assert_eq!(string, "Hello");
type ReadError = StringReadError
Implementors§
Source§impl<R> ReadFrom<R> for VariableLengthEnum
Reading a variable length enum from a byte stream.
impl<R> ReadFrom<R> for VariableLengthEnum
Reading a variable length enum from a byte stream.
This reads from a bytes reader and interprets the bytes as a variable length enum. It ends when it finished reading a variable length enum. It returns an error if it encounters an unexpected end of the byte stream or other I/O errors.
§Examples
Simple reading of a small variable length enum:
let configuration = fef::v0::config::OverridableConfig::default();
let file: Vec<u8> = vec![0x81, 0x80, 0x00, 0x12];
let mut file_reader = &mut file.as_slice();
let variable_length_enum = VariableLengthEnum::read_from(&mut file_reader, &configuration)?;
assert_eq!(variable_length_enum, VariableLengthEnum::from(0b1_0000000_0000000));
let mut next_byte = [0; 1];
file_reader.read_exact(&mut next_byte)?;
assert_eq!(next_byte[0], 0x12);
file_reader.read_exact(&mut next_byte).expect_err("Expected EOF");
Reading a large variable length enum with leading 0x80 bytes:
let configuration = fef::v0::config::OverridableConfig::default();
let file: Vec<u8> = vec![0x80, 0xFF, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x12];
let mut file_reader1 = &mut file.as_slice();
let mut file_reader2 = &mut file.as_slice();
let mut void_bytes: [u8; 1] = [0; 1];
file_reader1.read_exact(&mut void_bytes)?; // Skip the leading 0x80, it should have no effect
let variable_length_enum = VariableLengthEnum::read_from(&mut file_reader1, &configuration)?;
let variable_length_enum2 = VariableLengthEnum::read_from(&mut file_reader2, &configuration)?;
assert_eq!(variable_length_enum, variable_length_enum2);
let mut next_byte = [0; 1];
file_reader1.read_exact(&mut next_byte)?;
assert_eq!(next_byte[0], 0x12);
file_reader1.read_exact(&mut next_byte).expect_err("Expected EOF");
file_reader2.read_exact(&mut next_byte)?;
assert_eq!(next_byte[0], 0x12);
file_reader2.read_exact(&mut next_byte).expect_err("Expected EOF");
Reading from a passed &mut Read<R>:
fn read_two_variable_length_enums<R: std::io::Read + ?Sized, C: Config>(reader: &mut R, configuration: &C) -> Result<(VariableLengthEnum, VariableLengthEnum), Box<dyn std::error::Error>> {
let enum1 = VariableLengthEnum::read_from(reader, configuration)?; // Notice the reborrowing here
let enum2 = VariableLengthEnum::read_from(reader,configuration)?;
Ok((enum1, enum2))
}
let configuration = fef::v0::config::OverridableConfig::default();
let file = vec![0x80, 0x80, 0x00, 0x82, 0x80, 0x04, 0x12];
let mut file_reader = &mut file.as_slice();
let (enum1, enum2) = read_two_variable_length_enums(&mut file_reader, &configuration)?;
assert_eq!(enum1, VariableLengthEnum::from(0));
assert_eq!(enum2, VariableLengthEnum::from(0b10_0000000_0000100));
let mut next_byte = [0; 1];
file_reader.read_exact(&mut next_byte)?;
assert_eq!(next_byte[0], 0x12);
file_reader.read_exact(&mut next_byte).expect_err("Expected EOF");
Source§impl<R: ?Sized + Read> ReadFrom<R> for RawFormulaFile
Reads a raw formula file from a byte stream. Expects, that the file content type identifier has already been read.
impl<R: ?Sized + Read> ReadFrom<R> for RawFormulaFile
Reads a raw formula file from a byte stream. Expects, that the file content type identifier has already been read.
Source§impl<R: ?Sized + Read> ReadFrom<R> for SingleFormulaFile
Reads a single formula file from a byte stream. Expects, that the file content type identifier has already been read.
impl<R: ?Sized + Read> ReadFrom<R> for SingleFormulaFile
Reads a single formula file from a byte stream. Expects, that the file content type identifier has already been read.