Struct BitField

Source
pub struct BitField<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool>(/* private fields */);
Expand description

A bitfield descriptor type. Define an alias to this structure in order to use it properly.

§Parameters

  • S: Storage container for bitfield, must be larger or equal to SIZE.
  • T: Type to store in a bitfield, must implement ToBitfield and FromBitfield traits, this allows us to also encode booleans and enums easily in bitfield.
  • POSITION: position of bitfield in storage container, must not be out of bounds.
  • SIZE: Size of the bitfield, note that if T is u32 and we set size to 16 then only 16 bits of the 32-bit value will be stored.
  • SIGN_EXTEND: Whether or not to do sign-extension on decode operation.

§Example usage

use easy_bitfield::*;
// simple boolean bitfield at position 0
pub type MyBitfield = BitField<usize, bool, 0, 1, false>;
// simple 17-bit bitfield that is encoded/decoded from u32, located at second bit
pub type OtherBitfield = BitField<usize, u32, { MyBitfield::NEXT_BIT }, 17, false>;

Trait Implementations§

Source§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> BitFieldTrait<S> for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>

Source§

const NEXT_BIT: usize

Next bit after current bitfield, use this to not waste time calculating what the next bitfield position should be
Source§

type Type = T

Type we encode/decode in bitfield
Source§

fn mask() -> S

Mask of this bitfield
Source§

fn mask_in_place() -> S

In place mask of this bitfield
Source§

fn shift() -> usize

Shift of this bitfield
Source§

fn bitsize() -> usize

Bitsize of this bitifield
Source§

fn encode(value: T) -> S

Encodes value as bitfield Read more
Source§

fn decode(value: S) -> T

Decodes bitfield into Type
Source§

fn update(value: T, original: S) -> S

Updates the value in storage original, returns updated storage
Source§

fn is_valid(value: T) -> bool

Checks if value is valid to encode.
Source§

fn encode_unchecked(value: T) -> S

Unchecked encode of value, all bits that can’t be encoded will be stripped down.

Auto Trait Implementations§

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> Freeze for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> RefUnwindSafe for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> Send for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>
where S: Send, T: Send,

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> Sync for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>
where S: Sync, T: Sync,

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> Unpin for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>
where S: Unpin, T: Unpin,

§

impl<S, T, const POSITION: usize, const SIZE: usize, const SIGN_EXTEND: bool> UnwindSafe for BitField<S, T, POSITION, SIZE, SIGN_EXTEND>
where S: UnwindSafe, 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 = 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.