BitCodec

Trait BitCodec 

Source
pub trait BitCodec {
    // Required methods
    unsafe fn read_aligned<T: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &[u8],
    ) -> T;
    fn read_unaligned<T: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &[u8],
    ) -> T;
    unsafe fn write_aligned<T: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &mut [u8],
        value: T,
    );
    fn write_unaligned<T: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &mut [u8],
        value: T,
    );

    // Provided methods
    fn read<T: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &[u8],
    ) -> T { ... }
    fn write<T: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>(
        buffer: &mut [u8],
        value: T,
    ) { ... }
}

Required Methods§

Source

unsafe fn read_aligned<T: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( buffer: &[u8], ) -> T

Reads a value of type T from the buffer argument with a custom bit offset and size 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: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( buffer: &[u8], ) -> T

Reads a value of type T from the buffer argument with a custom bit offset and size 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: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( buffer: &mut [u8], value: T, )

Writes a value of type T in the buffer argument with a custom bit offset and size 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: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( buffer: &mut [u8], value: T, )

Provided Methods§

Source

fn read<T: ToUsize + ReadBytes + Shr<Output = T> + BitAnd<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( buffer: &[u8], ) -> T

Source

fn write<T: ToUsize + ReadBytes + WriteBytes + Shl<Output = T> + Shr<Output = T> + BitAnd<Output = T> + BitOr<Output = T>, const BIT_OFFSET: usize, const BIT_SIZE: usize>( 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§