Struct binary_layout::Field[][src]

pub struct Field<T: ?Sized, E: Endianness, const OFFSET_: usize> { /* fields omitted */ }

A field represents one of the fields in the data layout and offers accessors for it. It remembers the offset of the field in its const generic parameter and the accessors use that to access the field.

A field does not hold any data storage, so if you use this API directly, you have to pass in the storage pointer for each call. If you want an API object that remembers the storage, take a look at the FieldView based API instead.

Example:

use binary_layout::prelude::*;

define_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]);
}

Implementations

impl<E: Endianness, const OFFSET_: usize> Field<i8, E, OFFSET_>[src]

Field type i8: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> i8[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: i8 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<i8, E, OFFSET_>[src]

Field type i8: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: i8)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<i16, E, OFFSET_>[src]

Field type i16: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> i16[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: i16 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<i16, E, OFFSET_>[src]

Field type i16: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: i16)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<i32, E, OFFSET_>[src]

Field type i32: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> i32[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: i32 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<i32, E, OFFSET_>[src]

Field type i32: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: i32)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<i64, E, OFFSET_>[src]

Field type i64: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> i64[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: i64 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<i64, E, OFFSET_>[src]

Field type i64: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: i64)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

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

Field type u8: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> u8[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: u8 = my_layout::some_integer_field::read(storage_data);
}

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

Field type u8: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: u8)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<u16, E, OFFSET_>[src]

Field type u16: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> u16[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: u16 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<u16, E, OFFSET_>[src]

Field type u16: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: u16)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<u32, E, OFFSET_>[src]

Field type u32: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> u32[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: u32 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<u32, E, OFFSET_>[src]

Field type u32: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: u32)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

impl<E: Endianness, const OFFSET_: usize> Field<u64, E, OFFSET_>[src]

Field type u64: This field represents a primitive integer. In this impl, we define read accessors for such integer fields. See supported primitive integer types.

pub fn read(storage: &[u8]) -> u64[src]

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

Example:

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

fn func(storage_data: &[u8]) {
    let read: u64 = my_layout::some_integer_field::read(storage_data);
}

impl<E: Endianness, const OFFSET_: usize> Field<u64, E, OFFSET_>[src]

Field type u64: This field represents a little endian integer. In this impl, we define write accessors for such integer fields. See supported primitive integer types.

pub fn write(storage: &mut [u8], value: u64)[src]

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

Example:

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

fn func(storage_data: &mut [u8]) {
    my_layout::some_integer_field::write(storage_data, 10);
}

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

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

pub fn data(storage: &[u8]) -> &[u8][src]

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

Example:

use binary_layout::prelude::*;
            
define_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);
}

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

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

pub fn data_mut(storage: &mut [u8]) -> &mut [u8][src]

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

Example:

use binary_layout::prelude::*;
            
define_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);
}

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

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

pub fn data(storage: &[u8]) -> &[u8; N][src]

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

Example:

use binary_layout::prelude::*;
            
define_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);
}

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

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

pub fn data_mut(storage: &mut [u8]) -> &mut [u8; N][src]

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

Example:

use binary_layout::prelude::*;
            
define_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);
}

Trait Implementations

impl<T: ?Sized, E: Endianness, const OFFSET_: usize> FieldMetadata for Field<T, E, OFFSET_>[src]

type Type = T

The data type of the field, e.g. u8, i32, …

impl<T: FieldSize, E: Endianness, const OFFSET_: usize> SizedFieldMetadata for Field<T, E, OFFSET_>[src]

Auto Trait Implementations

impl<T: ?Sized, E, const OFFSET_: usize> RefUnwindSafe for Field<T, E, OFFSET_> where
    E: RefUnwindSafe,
    T: RefUnwindSafe

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

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

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

impl<T: ?Sized, E, const OFFSET_: usize> UnwindSafe for Field<T, E, OFFSET_> where
    E: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.