Trait ReadFrom

Source
pub trait ReadFrom<T>: Sealed + Sized
where T: Read + ?Sized,
{ type ReadError: Error; // Required method fn read_from<C: ?Sized + Config>( reader: &mut T, configuration: &C, ) -> Result<Self, Self::ReadError>; }
Available on crate feature v0 only.
Expand description

Trait for reading a value from bytes with option to fail.

Required Associated Types§

Source

type ReadError: Error

The error type that can be returned when reading fails.

Required Methods§

Source

fn read_from<C: ?Sized + Config>( reader: &mut T, configuration: &C, ) -> Result<Self, Self::ReadError>

Reads a value from the given reader.

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
where R: Read + ?Sized,

Source§

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");
Source§

type ReadError = StringReadError

Implementors§

Source§

impl<R> ReadFrom<R> for VariableLengthEnum
where R: Read + ?Sized,

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: Read + ?Sized> ReadFrom<R> for MetadataRecord

Source§

impl<R: ?Sized + Read> ReadFrom<R> for File

Source§

impl<R: ?Sized + Read> ReadFrom<R> for ExprToken

Source§

impl<R: ?Sized + Read> ReadFrom<R> for FileContentTypeToken

Source§

impl<R: ?Sized + Read> ReadFrom<R> for OverridableConfig

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.

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.

Source§

impl<R: ?Sized + Read> ReadFrom<R> for MetadataHeader

Source§

impl<R: ?Sized + Read> ReadFrom<R> for NameMetadataRecordObj

Source§

impl<R: ?Sized + Read> ReadFrom<R> for VariableNameMetadataRecordObj