Field

Struct Field 

Source
pub struct Field { /* private fields */ }
Expand description

Defines a region of bits.

Each field has a unit, size, and a flag indicating if the field represents a signed integer. The unit is the minimum number of bits extracted by the field. The size is the total number of units extracted by the field.

The flag comes into play when a field extracts more bits than are available. If at least one bit is still available and the flag is true, the bit string is sign extended to the length of the field.

§Constants

Common field types.

ConstantUnitSizeSigned
BIT11
BYTES8UNBOUND
SIGNED1UNBOUNDX
SIGNED_212X
SIGNED_414X
SIGNED_818X
SIGNED_16116X
SIGNED_32132X
SIGNED_64164X
UNSIGNED1UNBOUND
UNSIGNED_212
UNSIGNED_414
UNSIGNED_818
UNSIGNED_16116
UNSIGNED_32132
UNSIGNED_64164

Implementations§

Source§

impl Field

Source

pub const BIT: Field

Source

pub const BYTES: Field

Source

pub const SIGNED: Field

Source

pub const SIGNED_2: Field

Source

pub const SIGNED_4: Field

Source

pub const SIGNED_8: Field

Source

pub const SIGNED_16: Field

Source

pub const SIGNED_32: Field

Source

pub const SIGNED_64: Field

Source

pub const UNSIGNED: Field

Source

pub const UNSIGNED_2: Field

Source

pub const UNSIGNED_4: Field

Source

pub const UNSIGNED_8: Field

Source

pub const UNSIGNED_16: Field

Source

pub const UNSIGNED_32: Field

Source

pub const UNSIGNED_64: Field

Source

pub fn aligned(unit: usize, size: isize, signed: bool) -> Self

Bit field aligned to a given unit

When m and n are negative Bits::aligned(m, signed) == Bits::aligned(n, signed), even if m != n.

Example

let field = Field::aligned(7, 48, false);

assert_eq!(field.unit(), 7);
assert_eq!(field.size(), 48);
assert_eq!(field.len(), 48 * 7);

let mfield = Field::aligned(7, -1, true);
let nfield = Field::aligned(7, isize::MIN, true);

assert_eq!(mfield, nfield);
assert_eq!(mfield.size(), -1);
assert_eq!(nfield.size(), -1);
assert!(mfield.is_signed());
Source

pub fn bytes(size: isize, signed: bool) -> Self

Byte-aligned bit field

When m and n are negative Bits::bytes(m, signed) == Bits::bytes(n, signed), even if m != n.

Example

let field = Field::bytes(48, false);

assert_eq!(field.unit(), 8);
assert_eq!(field.size(), 48);
assert_eq!(field.len(), 48 * 8);

let mfield = Field::bytes(-1, true);
let nfield = Field::bytes(isize::MIN, true);

assert_eq!(mfield, nfield);
assert_eq!(mfield.size(), -1);
assert_eq!(nfield.size(), -1);
assert!(mfield.is_signed());
Source

pub fn signed(size: isize) -> Self

Bit field

When m and n are negative Bits::bits(m, signed) == Bits::bits(n, signed), even if m != n.

Example

let field = Field::signed(96);

assert_eq!(field.unit(), 1);
assert_eq!(field.size(), 96);
assert_eq!(field.len(), 96);
assert!(field.is_signed());

let mfield = Field::signed(-1);
let nfield = Field::signed(isize::MIN);

assert_eq!(mfield, nfield);
assert_eq!(mfield.size(), -1);
assert_eq!(nfield.size(), -1);
assert!(mfield.is_signed());
Source

pub fn unsigned(size: isize) -> Self

Bit field

When m and n are negative Bits::bits(m, signed) == Bits::bits(n, signed), even if m != n.

Example

let field = Field::unsigned(96);

assert_eq!(field.unit(), 1);
assert_eq!(field.size(), 96);
assert_eq!(field.len(), 96);
assert!(!field.is_signed());

let mfield = Field::unsigned(-1);
let nfield = Field::unsigned(isize::MIN);

assert_eq!(mfield, nfield);
assert_eq!(mfield.size(), -1);
assert_eq!(nfield.size(), -1);
assert!(!mfield.is_signed());
Source

pub fn is_signed(&self) -> bool

Whether this field represents a signed integer.

Example

assert!(Field::aligned(7, 48, true).is_signed());
assert!(!Field::aligned(7, 48, false).is_signed());
assert!(Field::bytes(-1, true).is_signed());
assert!(!Field::bytes(-1, false).is_signed());
assert!(Field::signed(96).is_signed());
assert!(!Field::unsigned(96).is_signed());
Source

pub fn len(&self) -> isize

Number of bits extracted by the field

-1 means that the field extracts the maximum number of bits that can be divided by the unit of the field. Extraction never exceeds the remaining the number of bits.

Example

assert_eq!(Field::aligned(0, -1, true).len(), 0);
assert_eq!(Field::aligned(7, 0, false).len(), 0);
assert_eq!(Field::aligned(7, 48, true).len(), 48 * 7);
assert_eq!(Field::aligned(7, -1, false).len(), -1);
assert_eq!(Field::aligned(7, isize::MIN, true).len(), -1);
assert_eq!(Field::bytes(48, true).len(), 48 * 8);
assert_eq!(Field::bytes(-1, false).len(), -1);
assert_eq!(Field::bytes(isize::MIN, true).len(), -1);
assert_eq!(Field::signed(48).len(), 48);
assert_eq!(Field::signed(-1).len(), -1);
assert_eq!(Field::signed(isize::MIN).len(), -1);
assert_eq!(Field::unsigned(48).len(), 48);
assert_eq!(Field::unsigned(-1).len(), -1);
assert_eq!(Field::unsigned(isize::MIN).len(), -1);
Source

pub fn size(&self) -> isize

Number of units in the field

Example

assert_eq!(Field::aligned(7, 48, true).size(), 48);
assert_eq!(Field::aligned(7, -1, false).size(), -1);
assert_eq!(Field::aligned(7, isize::MIN, true).size(), -1);
assert_eq!(Field::bytes(48, true).size(), 48);
assert_eq!(Field::bytes(-1, false).size(), -1);
assert_eq!(Field::bytes(isize::MIN, true).size(), -1);
assert_eq!(Field::signed(48).size(), 48);
assert_eq!(Field::signed(-1).size(), -1);
assert_eq!(Field::signed(isize::MIN).size(), -1);
assert_eq!(Field::unsigned(48).size(), 48);
assert_eq!(Field::unsigned(-1).size(), -1);
assert_eq!(Field::unsigned(isize::MIN).size(), -1);
Source

pub fn unit(&self) -> usize

Minimum number of bits extracted by the field.

Example

assert_eq!(Field::aligned(7, 48, true).unit(), 7);
assert_eq!(Field::aligned(7, -1, false).unit(), 7);
assert_eq!(Field::aligned(7, isize::MIN, true).unit(), 7);
assert_eq!(Field::bytes(48, true).unit(), 8);
assert_eq!(Field::bytes(-1, false).unit(), 8);
assert_eq!(Field::bytes(isize::MIN, true).unit(), 8);
assert_eq!(Field::signed(48).unit(), 1);
assert_eq!(Field::signed(-1).unit(), 1);
assert_eq!(Field::signed(isize::MIN).unit(), 1);
assert_eq!(Field::unsigned(48).unit(), 1);
assert_eq!(Field::unsigned(-1).unit(), 1);
assert_eq!(Field::unsigned(isize::MIN).unit(), 1);

Trait Implementations§

Source§

impl Clone for Field

Source§

fn clone(&self) -> Field

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Field

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Field

Source§

fn eq(&self, other: &Field) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Field

Source§

impl Eq for Field

Auto Trait Implementations§

§

impl Freeze for Field

§

impl RefUnwindSafe for Field

§

impl Send for Field

§

impl Sync for Field

§

impl Unpin for Field

§

impl UnwindSafe for Field

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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

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.