Trait binary_layout::FieldCopyAccess

source ·
pub trait FieldCopyAccess: Field {
    type ReadError;
    type WriteError;
    type HighLevelType;

    // Required methods
    fn try_read(storage: &[u8]) -> Result<Self::HighLevelType, Self::ReadError>;
    fn try_write(
        storage: &mut [u8],
        v: Self::HighLevelType
    ) -> Result<(), Self::WriteError>;
}
Expand description

This trait is implemented for fields with “try copy access”, i.e. fields that read/write data by copying it from/to the binary blob, but where reading or writing can fail. Examples of this are primitive types like NonZeroU8, NonZeroI32, …

Required Associated Types§

source

type ReadError

Error type that can be thrown from FieldCopyAccess::try_read. You can set this to Infallible if the function does not throw an error.

source

type WriteError

Error type that can be thrown from FieldCopyAccess::try_write. You can set this to Infallible if the function does not throw an error.

source

type HighLevelType

The data type that is returned from read calls and has to be passed in to write calls. This can be different from the primitive type used in the binary blob, since that primitive type can be wrapped (see WrappedField ) into a high level type before being returned from read calls (or vice versa unwrapped when writing).

Required Methods§

source

fn try_read(storage: &[u8]) -> Result<Self::HighLevelType, Self::ReadError>

Read the field from a given data region, assuming the defined layout, using the Field API.

§Example:
use binary_layout::prelude::*;
use core::num::NonZeroU16;

binary_layout!(my_layout, LittleEndian, {
  //... other fields ...
  some_integer_field: core::num::NonZeroU16,
  //... other fields ...
});

fn func(storage_data: &[u8]) -> Result<NonZeroU16, NonZeroIsZeroError> {
  let read: NonZeroU16 = my_layout::some_integer_field::try_read(storage_data)?;
  Ok(read)
}
source

fn try_write( storage: &mut [u8], v: Self::HighLevelType ) -> Result<(), Self::WriteError>

Write the field to a given data region, assuming the defined layout, using the Field API.

§Example:
use binary_layout::prelude::*;
use core::num::NonZeroU16;
use core::convert::Infallible;

binary_layout!(my_layout, LittleEndian, {
  //... other fields ...
  some_integer_field: core::num::NonZeroU16,
  //... other fields ...
});

fn func(storage_data: &mut [u8]) -> Result<(), Infallible> {
  let value = NonZeroU16::new(10).unwrap();
  my_layout::some_integer_field::try_write(storage_data, value)?;
  Ok(())
}

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<f32, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<f64, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<i8, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<i16, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<i32, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<i64, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<i128, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<u8, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<u16, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<u32, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<u64, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<u128, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<(), E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroI8, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroI16, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroI32, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroI64, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroI128, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroU8, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroU16, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroU32, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroU64, E, OFFSET_>

source§

impl<E: Endianness, const OFFSET_: usize> FieldCopyAccess for PrimitiveField<NonZeroU128, E, OFFSET_>

source§

impl<U, T: LayoutAs<U>, F: FieldCopyAccess<HighLevelType = U>> FieldCopyAccess for WrappedField<U, T, F>