Struct binary_layout::FieldView

source ·
pub struct FieldView<S, F: Field> { /* private fields */ }
Expand description

A field view represents the field metadata stored in a Field plus it stores the underlying storage data it operates on, either as a reference to a slice &[u8], &mut [u8], or as an owning Vec<u8>.

Since this API remembers the underlying storage data in a view object, you don’t have to pass it in each time you’re accessing a field. If you rather prefer an API that does not do that, take a look at the Field API.

§Example:

use binary_layout::prelude::*;

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

fn func(storage_data: &mut [u8]) {
  let mut view = my_layout::View::new(storage_data);

  // read some data
  let format_version_header: u16 = view.field_one().read();
  // equivalent: let format_version_header = u16::from_le_bytes((&storage_data[0..2]).try_into().unwrap());

  // write some data
  view.something_else_mut().write(10);
  // equivalent: data_slice[18..22].copy_from_slice(&10u32.to_le_bytes());

  // access a data region
  let tail: &[u8] = view.tail();
  // equivalent: let tail: &[u8] = &data_slice[22..];

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

Implementations§

source§

impl<S, F: Field> FieldView<S, F>

source

pub fn new(storage: S) -> Self

Create a new view for a field over a given storage. You probably shouldn’t call this directly but should instead call your_layout::View::new(), which is generated by the binary_layout! macro for you.

source§

impl<S: AsRef<[u8]>, F: FieldReadExt> FieldView<S, F>

source

pub fn read(&self) -> F::HighLevelType

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

§Example
use binary_layout::prelude::*;

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

fn func(storage_data: &[u8]) {
  let view = my_layout::View::new(storage_data);
  let read: i8 = view.some_integer_field().read();
}
source§

impl<S: AsMut<[u8]>, F: FieldWriteExt> FieldView<S, F>

source

pub fn write(&mut self, v: F::HighLevelType)

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

§Example
use binary_layout::prelude::*;

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

fn func(storage_data: &mut [u8]) {
  let mut view = my_layout::View::new(storage_data);
  view.some_integer_field_mut().write(10);
}
source§

impl<S: AsRef<[u8]>, F: FieldCopyAccess> FieldView<S, F>

source

pub fn try_read(&self) -> Result<F::HighLevelType, F::ReadError>

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

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

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

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

impl<S: AsMut<[u8]>, F: FieldCopyAccess> FieldView<S, F>

source

pub fn try_write(&mut self, v: F::HighLevelType) -> Result<(), F::WriteError>

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

§Example
use binary_layout::prelude::*;
use core::num::NonZeroI8;
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]) -> Result<(), Infallible> {
  let mut view = my_layout::View::new(storage_data);
  let value = NonZeroI8::new(10).unwrap();
  view.some_integer_field_mut().try_write(value)?;
  Ok(())
}

Auto Trait Implementations§

§

impl<S, F> Freeze for FieldView<S, F>
where S: Freeze,

§

impl<S, F> RefUnwindSafe for FieldView<S, F>

§

impl<S, F> Send for FieldView<S, F>
where S: Send, F: Send,

§

impl<S, F> Sync for FieldView<S, F>
where S: Sync, F: Sync,

§

impl<S, F> Unpin for FieldView<S, F>
where S: Unpin, F: Unpin,

§

impl<S, F> UnwindSafe for FieldView<S, F>
where S: UnwindSafe, F: UnwindSafe,

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<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>,

§

type Error = Infallible

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>,

§

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.