Skip to main content

ReadWrite

Trait ReadWrite 

Source
pub trait ReadWrite: Sized {
    // Required methods
    fn read(reader: impl Read) -> Result<Self>;
    fn write(&self, writer: impl Write) -> Result<()>;

    // Provided method
    fn serialize(&self) -> Vec<u8> ⓘ { ... }
}
Expand description

An item which can be read and written.

Required Methods§

Source

fn read(reader: impl Read) -> Result<Self>

Read an instance of Self.

If the reader yields a reachable serialization of a value, it MUST be readable. In other words, the following round-trip MUST succeed:

let serialization = item.serialize();
let mut serialization = serialization.as_slice();
let item2 = I::read(&mut serialization).unwrap();
// Functional equivalence in all regards, not just `<I as PartialEq>::eq` returning `true`
assert_eq!(item, item2);
assert!(serialization.is_empty());

Additionally, the item MUST be self-delimited and MUST NOT read to the end.

Source

fn write(&self, writer: impl Write) -> Result<()>

Write the value of self.

This MUST be infallible except if the writer fails.

Provided Methods§

Source

fn serialize(&self) -> Vec<u8> ⓘ

Serialize self to a Vec<u8>.

This is infallible as ReadWrite::write is infallible except if the writer fails, and <Vec<u8> as Write> is infallible.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§