Skip to main content

Field

Struct Field 

Source
pub struct Field<T, const LO: u32, const HI: u32>(/* private fields */);
Expand description

One value packed into bits LO..=HI of a panel, inclusive, MSB-first from byte 0.

Never instantiated — it names a position plus a conversion, used as MyField::get(&raw) / MyField::set(&mut raw, v).

use nord_format::bits::Field;
let _ = Field::<u8, 0, 15>::read(&[0; 2]);

A one-bit type cannot decode a wider field:

use nord_format::bits::Field;
let _ = Field::<bool, 0, 1>::read(&[0]);

A Packed implementation cannot advertise more value bits than it can decode:

use nord_format::bits::{Field, Packed};
use std::convert::Infallible;
struct Invalid;
impl Packed for Invalid {
    const MAX_BITS: u32 = 2;
    const DECODE_BITS: u32 = 1;
    type Error = Infallible;
    fn from_bits(_: u64) -> Result<Self, Infallible> { Ok(Self) }
    fn to_bits(&self) -> u64 { 0 }
}
let _ = Field::<Invalid, 0, 1>::get::<1>(&[0]);

Implementations§

Source§

impl<T: Packed, const LO: u32, const HI: u32> Field<T, LO, HI>

Source

pub const WIDTH: u32

Width of the field in bits.

Source

pub fn get<const N: usize>(raw: &[u8; N]) -> Result<T, T::Error>

Decode the field out of raw.

Source

pub fn set<const N: usize>(raw: &mut [u8; N], value: T)

Write value, leaving every other bit of raw as it was.

Only compiles when no value of T can overrun the field: a u8 in a 7-bit slot is a compile error.

Source§

impl<T: Packed<Error = Infallible>, const LO: u32, const HI: u32> Field<T, LO, HI>

Source

pub fn read<const N: usize>(raw: &[u8; N]) -> T

Decode, when every bit pattern is a valid value.

Auto Trait Implementations§

§

impl<T, const LO: u32, const HI: u32> Freeze for Field<T, LO, HI>
where PhantomData<fn() -> T>: Freeze,

§

impl<T, const LO: u32, const HI: u32> RefUnwindSafe for Field<T, LO, HI>
where PhantomData<fn() -> T>: RefUnwindSafe,

§

impl<T, const LO: u32, const HI: u32> Send for Field<T, LO, HI>
where PhantomData<fn() -> T>: Send,

§

impl<T, const LO: u32, const HI: u32> Sync for Field<T, LO, HI>
where PhantomData<fn() -> T>: Sync,

§

impl<T, const LO: u32, const HI: u32> Unpin for Field<T, LO, HI>
where PhantomData<fn() -> T>: Unpin,

§

impl<T, const LO: u32, const HI: u32> UnsafeUnpin for Field<T, LO, HI>
where PhantomData<fn() -> T>: UnsafeUnpin,

§

impl<T, const LO: u32, const HI: u32> UnwindSafe for Field<T, LO, HI>
where PhantomData<fn() -> T>: 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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.