Skip to main content

PrimitiveField

Struct PrimitiveField 

Source
pub struct PrimitiveField<T: ?Sized, E: Endianness, const OFFSET_: usize> { /* private fields */ }
Expand description

A PrimitiveField is a Field that directly represents a primitive type like u8, i16, … See Field for more info on this API.

§Example:

use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
  field_one: u16,
  another_field: [u8; 16],
  something_else: u32,
  tail_data: [u8],
});

fn func(storage_data: &mut [u8]) {
  // read some data
  let format_version_header: u16 = my_layout::field_one::read(storage_data);
  // equivalent: let format_version_header = u16::from_le_bytes((&storage_data[0..2]).try_into().unwrap());

  // write some data
  my_layout::something_else::write(storage_data, 10);
  // equivalent: data_slice[18..22].copy_from_slice(&10u32.to_le_bytes());

  // access a data region
  let tail_data: &[u8] = my_layout::tail_data::data(storage_data);
  // equivalent: let tail_data: &[u8] = &data_slice[22..];

  // and modify it
  my_layout::tail_data::data_mut(storage_data)[..5].copy_from_slice(&[1, 2, 3, 4, 5]);
  // equivalent: data_slice[18..22].copy_from_slice(&[1, 2, 3, 4, 5]);
}

Trait Implementations§

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<N: NestedViewInfo, E: Endianness, const OFFSET_: usize> Field for PrimitiveField<N, E, OFFSET_>

Source§

const OFFSET: usize = OFFSET_

Source§

const SIZE: Option<usize> = N::SIZE

Source§

type Endian = E

Source§

impl<E: Endianness, const OFFSET_: usize> Field for PrimitiveField<[u8], E, OFFSET_>

Source§

impl<E: Endianness, const N: usize, const OFFSET_: usize> Field for PrimitiveField<[u8; N], E, OFFSET_>

Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = f32

Source§

fn try_read(storage: &[u8]) -> Result<f32, Infallible>

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

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

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_float_field: f32
    //... other fields ...
});

fn func(storage_data: &[u8]) -> f32 {
    let read: f32 = my_layout::some_float_field::try_read(storage_data).unwrap();
    read
}
§WARNING

At it’s core, this method uses f32::from_bits, which has some weird behavior around signaling and non-signaling NaN values. Read the documentation for f32::from_bits which explains the situation.

Source§

fn try_write(storage: &mut [u8], value: f32) -> Result<(), Infallible>

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

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_float_field: f32
    //... other fields ...
});

fn func(storage_data: &mut [u8]) {
    my_layout::some_float_field::try_write(storage_data, 10.0).unwrap();
}
§WARNING

At it’s core, this method uses f32::to_bits, which has some weird behavior around signaling and non-signaling NaN values. Read the documentation for f32::to_bits which explains the situation.

Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = f64

Source§

fn try_read(storage: &[u8]) -> Result<f64, Infallible>

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

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

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_float_field: f64
    //... other fields ...
});

fn func(storage_data: &[u8]) -> f64 {
    let read: f64 = my_layout::some_float_field::try_read(storage_data).unwrap();
    read
}
§WARNING

At it’s core, this method uses f64::from_bits, which has some weird behavior around signaling and non-signaling NaN values. Read the documentation for f64::from_bits which explains the situation.

Source§

fn try_write(storage: &mut [u8], value: f64) -> Result<(), Infallible>

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

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_float_field: f64
    //... other fields ...
});

fn func(storage_data: &mut [u8]) {
    my_layout::some_float_field::try_write(storage_data, 10.0).unwrap();
}
§WARNING

At it’s core, this method uses f64::to_bits, which has some weird behavior around signaling and non-signaling NaN values. Read the documentation for f64::to_bits which explains the situation.

Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = i8

Source§

fn try_read(storage: &[u8]) -> Result<i8, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> i8 {
    let read: i8 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: i8) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = i16

Source§

fn try_read(storage: &[u8]) -> Result<i16, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> i16 {
    let read: i16 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: i16) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = i32

Source§

fn try_read(storage: &[u8]) -> Result<i32, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> i32 {
    let read: i32 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: i32) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = i64

Source§

fn try_read(storage: &[u8]) -> Result<i64, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> i64 {
    let read: i64 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: i64) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = i128

Source§

fn try_read(storage: &[u8]) -> Result<i128, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> i128 {
    let read: i128 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: i128) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = u8

Source§

fn try_read(storage: &[u8]) -> Result<u8, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> u8 {
    let read: u8 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: u8) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = u16

Source§

fn try_read(storage: &[u8]) -> Result<u16, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> u16 {
    let read: u16 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: u16) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = u32

Source§

fn try_read(storage: &[u8]) -> Result<u32, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> u32 {
    let read: u32 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: u32) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = u64

Source§

fn try_read(storage: &[u8]) -> Result<u64, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> u64 {
    let read: u64 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: u64) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = u128

Source§

fn try_read(storage: &[u8]) -> Result<u128, Infallible>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> u128 {
    let read: u128 = my_layout::some_integer_field::try_read(storage_data).unwrap();
    read
}
Source§

fn try_write(storage: &mut [u8], value: u128) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::try_write(storage_data, 10).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<i8>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroI8, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroI8, NonZeroIsZeroError>{
    let read: core::num::NonZeroI8 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroI8) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroI8::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<i16>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroI16, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroI16, NonZeroIsZeroError>{
    let read: core::num::NonZeroI16 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroI16) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroI16::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<i32>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroI32, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroI32, NonZeroIsZeroError>{
    let read: core::num::NonZeroI32 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroI32) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroI32::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<i64>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroI64, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroI64, NonZeroIsZeroError>{
    let read: core::num::NonZeroI64 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroI64) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroI64::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<i128>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroI128, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroI128, NonZeroIsZeroError>{
    let read: core::num::NonZeroI128 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroI128) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroI128::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<u8>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroU8, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroU8, NonZeroIsZeroError>{
    let read: core::num::NonZeroU8 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroU8) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroU8::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<u16>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroU16, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

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

fn try_write(storage: &mut [u8], value: NonZeroU16) -> Result<(), Infallible>

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

§Example:
use binary_layout::prelude::*;
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]) {
    let value = core::num::NonZeroU16::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<u32>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroU32, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroU32, NonZeroIsZeroError>{
    let read: core::num::NonZeroU32 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroU32) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroU32::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<u64>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroU64, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroU64, NonZeroIsZeroError>{
    let read: core::num::NonZeroU64 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroU64) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroU64::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = NonZeroIsZeroError

Source§

type WriteError = !

Source§

type HighLevelType = NonZero<u128>

Source§

fn try_read(storage: &[u8]) -> Result<NonZeroU128, NonZeroIsZeroError>

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

§Example:
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) -> Result<core::num::NonZeroU128, NonZeroIsZeroError>{
    let read: core::num::NonZeroU128 = my_layout::some_integer_field::try_read(storage_data)?;
    Ok(read)
}
Source§

fn try_write(storage: &mut [u8], value: NonZeroU128) -> Result<(), Infallible>

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

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

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

fn func(storage_data: &mut [u8]) {
    let value = core::num::NonZeroU128::new(10).unwrap();
    my_layout::some_integer_field::try_write(storage_data, value).unwrap();
}
Source§

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

Source§

type ReadError = !

Source§

type WriteError = !

Source§

type HighLevelType = ()

Source§

fn try_read(_storage: &[u8]) -> Result<(), Infallible>

‘Read’ the ()-typed field from a given data region, assuming the defined layout, using the Field API.

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_zst_field: ()
    //... other fields ...
});

fn func(storage_data: &[u8]) {
    let read: () = my_layout::some_zst_field::try_read(storage_data).unwrap();
    read
}

In reality, this method doesn’t do any work; () is a zero-sized type, so there’s no work to do. This implementation exists solely to make writing derive macros simpler.

Source§

fn try_write(_storage: &mut [u8], _value: ()) -> Result<(), Infallible>

‘Write’ the ()-typed field to a given data region, assuming the defined layout, using the Field API.

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_zst_field: ()
    //... other fields ...
});

fn func(storage_data: &mut [u8]) {
    my_layout::some_zst_field::try_write(storage_data, ()).unwrap();
}
§WARNING

In reality, this method doesn’t do any work; () is a zero-sized type, so there’s no work to do. This implementation exists solely to make writing derive macros simpler.

Source§

impl<'a, E: Endianness, const OFFSET_: usize> FieldSliceAccess<'a> for PrimitiveField<[u8], E, OFFSET_>

Field type [u8]: This field represents an open ended byte array. In this impl, we define accessors for such fields.

Source§

fn data(storage: &'a [u8]) -> &'a [u8]

Borrow the data in the byte array with read access using the Field API.

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    tail_data: [u8],
});

fn func(storage_data: &[u8]) {
    let tail_data: &[u8] = my_layout::tail_data::data(storage_data);
}
Source§

fn data_mut(storage: &'a mut [u8]) -> &'a mut [u8]

Borrow the data in the byte array with write access using the Field API.

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    tail_data: [u8],
});

fn func(storage_data: &mut [u8]) {
    let tail_data: &mut [u8] = my_layout::tail_data::data_mut(storage_data);
}
Source§

type SliceType = &'a [u8]

The type of slice returned from calls requesting read access
Source§

type MutSliceType = &'a mut [u8]

The type of slice returned from calls requesting write access
Source§

impl<'a, E: Endianness, const N: usize, const OFFSET_: usize> FieldSliceAccess<'a> for PrimitiveField<[u8; N], E, OFFSET_>

Field type [u8; N]: This field represents a fixed size byte array. In this impl, we define accessors for such fields.

Source§

fn data(storage: &'a [u8]) -> &'a [u8; N]

Borrow the data in the byte array with read access using the Field API. See also FieldSliceAccess::data.

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_field: [u8; 5],
    //... other fields
});

fn func(storage_data: &[u8]) {
    let some_field: &[u8; 5] = my_layout::some_field::data(storage_data);
}
Source§

fn data_mut(storage: &'a mut [u8]) -> &'a mut [u8; N]

Borrow the data in the byte array with write access using the Field API. See also FieldSliceAccess::data_mut

§Example:
use binary_layout::prelude::*;

binary_layout!(my_layout, LittleEndian, {
    //... other fields ...
    some_field: [u8; 5],
    //... other fields
});

fn func(storage_data: &mut [u8]) {
    let some_field: &mut [u8; 5] = my_layout::some_field::data_mut(storage_data);
}
Source§

type SliceType = &'a [u8; N]

The type of slice returned from calls requesting read access
Source§

type MutSliceType = &'a mut [u8; N]

The type of slice returned from calls requesting write access

Auto Trait Implementations§

§

impl<T, E, const OFFSET_: usize> Freeze for PrimitiveField<T, E, OFFSET_>

§

impl<T, E, const OFFSET_: usize> RefUnwindSafe for PrimitiveField<T, E, OFFSET_>

§

impl<T, E, const OFFSET_: usize> Send for PrimitiveField<T, E, OFFSET_>
where PhantomData<T>: Send, PhantomData<E>: Send, T: ?Sized,

§

impl<T, E, const OFFSET_: usize> Sync for PrimitiveField<T, E, OFFSET_>
where PhantomData<T>: Sync, PhantomData<E>: Sync, T: ?Sized,

§

impl<T, E, const OFFSET_: usize> Unpin for PrimitiveField<T, E, OFFSET_>
where PhantomData<T>: Unpin, PhantomData<E>: Unpin, T: ?Sized,

§

impl<T, E, const OFFSET_: usize> UnsafeUnpin for PrimitiveField<T, E, OFFSET_>

§

impl<T, E, const OFFSET_: usize> UnwindSafe for PrimitiveField<T, E, OFFSET_>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<F> FieldReadExt for F
where F: FieldCopyAccess, <F as FieldCopyAccess>::ReadError: IsInfallible,

Source§

fn read(storage: &[u8]) -> <F as FieldReadExt>::HighLevelType

This implements a convenience method for reading any data type whose FieldCopyAccess::try_read does not throw errors. See FieldCopyAccess::try_read.

Source§

type HighLevelType = <F as FieldCopyAccess>::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).
Source§

impl<F> FieldWriteExt for F
where F: FieldCopyAccess, <F as FieldCopyAccess>::WriteError: IsInfallible,

Source§

fn write(storage: &mut [u8], value: <F as FieldWriteExt>::HighLevelType)

This implements a convenience method for writing any data type whose FieldCopyAccess::try_write does not throw errors. See FieldCopyAccess::try_write.

Source§

type HighLevelType = <F as FieldCopyAccess>::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).
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.