Struct bitfield_layout::BitField [−][src]
pub struct BitField<M, T> {
pub value: T,
// some fields omitted
}Expand description
Accessory struct for convinient type construction
This structure holds value of created bitfield type and may be used for types that doesn’t has own value field: enums and unit-like structs.
Enum wrapper
Using enumeration as bitfield type has the following advantage - you can bind bit (flag) to one of enum variants.
// Declare new bitfield type enum EightFlags { One, Two, Three, Four, OemReserved(u8), // Reserved field referenced to multiple bits FutureReserved(u8), // Reserved field referenced to multiple bits } // Implement Dispaly trait for basic and alternative views impl fmt::Display for EightFlags { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match (self, f.alternate()) { // Basic view (Self::One, false) => write!(f, "one"), (Self::Two, false) => write!(f, "two"), (Self::Three, false) => write!(f, "three"), (Self::Four, false) => write!(f, "four"), // Alternative view (Self::One, true) => write!(f, "ONE"), (Self::Two, true) => write!(f, "TWO"), (Self::Three, true) => write!(f, "THREE"), (Self::Four, true) => write!(f, "FOUR"), // Reserved fields (Self::OemReserved(v), _) => write!(f, "OEM reserved (#{})", v), (Self::FutureReserved(v), _) => write!(f, "Reserved for future usage (#{})", v), } } } // Implement constant bit layout for this type impl EightFlags { const LAYOUT: [Self; 8] = [ Self::One, Self::Two, Self::Three, Self::Four, Self::OemReserved(4), Self::OemReserved(5), Self::FutureReserved(6), Self::FutureReserved(7), ]; } // Implement Layout for enum created early impl Layout for EightFlags { type Layout = std::slice::Iter<'static, EightFlags>; fn layout() -> Self::Layout { EightFlags::LAYOUT.iter() } } // Now we can use wrapped bitfield enum let bf: BitField<EightFlags, u8> = BitField::new(0b01100101); // Get only setted flags let result = bf.flags() .filter_map(|f| { if f.is_set { match f.value { EightFlags::OemReserved(v) => Some(format!("Reserved flag #{}", v)), EightFlags::FutureReserved(_) => Some(format!("{}", f.value)), v @ _ => Some(format!("Name: {}, Description: {:#}", v, v)), } } else { None } }) .collect::<Vec<_>>(); let sample = vec![ "Name: one, Description: ONE", "Name: three, Description: THREE", "Reserved flag #5", "Reserved for future usage (#6)", ]; assert_eq!(sample, result, "Wrapped enum");
Unit-like struct wrapper
We can use bitfield type defined as unit-like struct in the same way as for enum
// Unit-like struct without value struct Status; // Bind flags layout to this struct impl Status { const LAYOUT: [&'static str; 8] = [ "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", ]; } // Implement layout trait impl Layout for Status { type Layout = core::slice::Iter<'static, &'static str>; fn layout() -> Self::Layout { Status::LAYOUT.iter() } } let bf: BitField<Status, u8> = BitField::new(42); // Get formatted strings from flags iteartor let result = bf.flags() .map(|f| format!("{:#}", f.value)) .collect::<Vec<_>>(); let sample = vec!["s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7"]; assert_eq!(sample, result, "Simple unit-like struct");
Unit-like struct with associated constants
Also we can use unit-like struct with associated constant flags. This will gave us feauture to has marked bits. This realisation somewhere in beetween enum and simple unit-like struct.
// Unit-like struct without value struct Status; // Implement layout. You can leave comments on every item. For example, you can use bit id as // Status::ONE. impl Status { const ONE: &'static str = "One"; const TWO: &'static str = "Two"; const THREE: &'static str = "Three"; const FOUR: &'static str = "Four"; const RESERVED: &'static str = "Reserved"; const UNKNOWN: &'static str = "Unknown"; const LAYOUT: [&'static str; 8] = [ Self::ONE, Self::TWO, Self::THREE, Self::FOUR, Self::RESERVED, Self::RESERVED, Self::RESERVED, Self::UNKNOWN, ]; } // Implement layout trait impl Layout for Status { type Layout = core::slice::Iter<'static, &'static str>; fn layout() -> Self::Layout { Status::LAYOUT.iter() } } let bf: BitField<Status, u8> = BitField::new(0b00001000); let result = bf.flags() .find(|f| f.is_set) .map(|f| f.value) .unwrap(); assert_eq!(Status::FOUR, *result, "Enumeration");
Fields
value: TImplementations
Trait Implementations
impl<M, T> BitAnd<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitAnd<Output = T>, [src]
impl<M, T> BitAnd<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitAnd<Output = T>, [src]impl<M, T> BitAndAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitAnd<Output = T>, [src]
impl<M, T> BitAndAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitAnd<Output = T>, [src]fn bitand_assign(&mut self, rhs: Self)[src]
fn bitand_assign(&mut self, rhs: Self)[src]Performs the &= operation. Read more
impl<M: Layout, T: Copy + IntoBits + FromBits> BitFieldLayout for BitField<M, T>[src]
impl<M: Layout, T: Copy + IntoBits + FromBits> BitFieldLayout for BitField<M, T>[src]type Value = T
fn replace(&mut self, new: Self::Value) -> Self::Value[src]
fn replace(&mut self, new: Self::Value) -> Self::Value[src]Replaces the contained value with val, and returns the old contained value. Read more
fn update<F>(&mut self, f: F) -> Self::Value where
F: FnOnce(Self::Value) -> Self::Value, [src]
fn update<F>(&mut self, f: F) -> Self::Value where
F: FnOnce(Self::Value) -> Self::Value, [src]Updates the contained value using a function and returns the new value. Read more
fn insert_flag(&mut self, position: usize, b: bool) -> bool[src]
fn insert_flag(&mut self, position: usize, b: bool) -> bool[src]Set the specified bit (flag) in-place. Returns current state Read more
fn toggle_flag(&mut self, position: usize)[src]
fn toggle_flag(&mut self, position: usize)[src]The specified bit (flag) will be inverted Read more
fn bits(&self) -> Bits<<Self::Value as IntoBits>::Bytes>ⓘ[src]
fn bits(&self) -> Bits<<Self::Value as IntoBits>::Bytes>ⓘ[src]Return iterator through bitfield value bits. Every bit represents as bool value. Read more
fn flags(&self) -> Flags<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>ⓘ[src]
fn flags(&self) -> Flags<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>ⓘ[src]Return iterator through bitfield value flags. Every flag contains bit state (set or unset) and item (record) value - string in simple case. Read more
impl<M, T> BitOr<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitOr<Output = T>, [src]
impl<M, T> BitOr<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitOr<Output = T>, [src]impl<M, T> BitOrAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitOr<Output = T>, [src]
impl<M, T> BitOrAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitOr<Output = T>, [src]fn bitor_assign(&mut self, rhs: Self)[src]
fn bitor_assign(&mut self, rhs: Self)[src]Performs the |= operation. Read more
impl<M, T> BitXor<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitXor<Output = T>, [src]
impl<M, T> BitXor<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitXor<Output = T>, [src]impl<M, T> BitXorAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitXor<Output = T>, [src]
impl<M, T> BitXorAssign<BitField<M, T>> for BitField<M, T> where
M: Layout,
T: Copy + IntoBits + FromBits + BitXor<Output = T>, [src]fn bitxor_assign(&mut self, rhs: Self)[src]
fn bitxor_assign(&mut self, rhs: Self)[src]Performs the ^= operation. Read more
impl<M: Ord, T: Ord> Ord for BitField<M, T>[src]
impl<M: Ord, T: Ord> Ord for BitField<M, T>[src]impl<M: PartialOrd, T: PartialOrd> PartialOrd<BitField<M, T>> for BitField<M, T>[src]
impl<M: PartialOrd, T: PartialOrd> PartialOrd<BitField<M, T>> for BitField<M, T>[src]fn partial_cmp(&self, other: &BitField<M, T>) -> Option<Ordering>[src]
fn partial_cmp(&self, other: &BitField<M, T>) -> Option<Ordering>[src]This method returns an ordering between self and other values if one exists. Read more
#[must_use]fn lt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]fn lt(&self, other: &Rhs) -> bool1.0.0[src]This method tests less than (for self and other) and is used by the < operator. Read more
#[must_use]fn le(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]fn le(&self, other: &Rhs) -> bool1.0.0[src]This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
impl<M: Eq, T: Eq> Eq for BitField<M, T>[src]
impl<M, T> StructuralEq for BitField<M, T>[src]
impl<M, T> StructuralPartialEq for BitField<M, T>[src]
Auto Trait Implementations
impl<M, T> RefUnwindSafe for BitField<M, T> where
M: RefUnwindSafe,
T: RefUnwindSafe,
M: RefUnwindSafe,
T: RefUnwindSafe,
impl<M, T> Send for BitField<M, T> where
M: Send,
T: Send,
M: Send,
T: Send,
impl<M, T> Sync for BitField<M, T> where
M: Sync,
T: Sync,
M: Sync,
T: Sync,
impl<M, T> Unpin for BitField<M, T> where
M: Unpin,
T: Unpin,
M: Unpin,
T: Unpin,
impl<M, T> UnwindSafe for BitField<M, T> where
M: UnwindSafe,
T: UnwindSafe,
M: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]pub fn borrow_mut(&mut self) -> &mut T[src]
pub fn borrow_mut(&mut self) -> &mut T[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, [src]type Owned = T
type Owned = TThe resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn to_owned(&self) -> T[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)[src]
pub fn clone_into(&self, target: &mut T)[src]🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more