ByteCodec

Trait ByteCodec 

Source
pub trait ByteCodec {
    // Required methods
    unsafe fn read_aligned<T: ReadBytes>(buffer: &[u8]) -> T;
    fn read_unaligned<T: ReadBytes>(buffer: &[u8]) -> T;
    unsafe fn write_aligned<T: WriteBytes>(buffer: &mut [u8], value: T);
    fn write_unaligned<T: WriteBytes>(buffer: &mut [u8], value: T);

    // Provided methods
    fn read<T: ReadBytes>(buffer: &[u8]) -> T { ... }
    fn write<T: WriteBytes>(buffer: &mut [u8], value: T) { ... }
}

Required Methods§

Source

unsafe fn read_aligned<T: ReadBytes>(buffer: &[u8]) -> T

Reads a value of type T from the buffer argument assuming the buffer size is greater or equal to the size of T.

§Arguments
  • buffer: the buffer to read from.

returns: T

§Safety

This function assumes that the length of the buffer passed in as argument is at least as large as the size of T. Currently, this relies on bytesutil which does not apply any optimization and as such passing a too small buffer will only panic, however a future optimization might remove the panic check from release builds, essentially causing UB in such build.

Source

fn read_unaligned<T: ReadBytes>(buffer: &[u8]) -> T

Reads a value of type T from the buffer argument assuming the buffer size is always less than the size of T. This is not unsafe as will always cause a copy into an 8 bytes buffer (the maximum size for T is 8).

§Arguments
  • buffer: the buffer to read from.

returns: T

Source

unsafe fn write_aligned<T: WriteBytes>(buffer: &mut [u8], value: T)

Writes a value of type T in the buffer argument assuming the buffer size is greater or equal to the size of T.

§Arguments
  • buffer: the buffer to write to.
  • value: the value to write.
§Safety

This function assumes that the length of the buffer passed in as argument is at least as large as the size of T. Currently, this relies on bytesutil which does not apply any optimization and as such passing a too small buffer will only panic, however a future optimization might remove the panic check from release builds, essentially causing UB in such build.

Source

fn write_unaligned<T: WriteBytes>(buffer: &mut [u8], value: T)

Provided Methods§

Source

fn read<T: ReadBytes>(buffer: &[u8]) -> T

Source

fn write<T: WriteBytes>(buffer: &mut [u8], value: T)

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.

Implementors§