Trait bitfield_layout::BitFieldLayout[][src]

pub trait BitFieldLayout: Layout {
    type Value: Copy + IntoBits + FromBits;
    fn get(&self) -> Self::Value;
fn set(&mut self, new: Self::Value); fn replace(&mut self, new: Self::Value) -> Self::Value { ... }
fn swap(&mut self, other: &mut Self) { ... }
fn update<F>(&mut self, f: F) -> Self::Value
    where
        F: FnOnce(Self::Value) -> Self::Value
, { ... }
fn insert_flag(&mut self, position: usize, b: bool) -> bool { ... }
fn toggle_flag(&mut self, position: usize) { ... }
fn bits(&self) -> Bits<<Self::Value as IntoBits>::Bytes>
Notable traits for Bits<I>
impl<I: Iterator<Item = u8>> Iterator for Bits<I> type Item = bool;
{ ... }
fn flags(
        &self
    ) -> Flags<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>
Notable traits for Flags<L, B>
impl<L, B> Iterator for Flags<L, B> where
    L: Iterator,
    B: Iterator<Item = bool>, 
type Item = Flag<L::Item>;
{ ... }
fn diff(
        &self,
        other: Self
    ) -> Diff<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>
Notable traits for Diff<L, B>
impl<L, T, B> Iterator for Diff<L, B> where
    L: Iterator<Item = T>,
    B: Iterator<Item = bool>, 
type Item = Either<(usize, T), (usize, T)>;

    where
        Self: Sized
, { ... }
fn find_state<P>(&self, predicate: P) -> Option<bool>
    where
        P: Fn(<<Self as Layout>::Layout as Iterator>::Item) -> bool
, { ... } }
Expand description

Main trait for creating bitfield

In general you need implement this trait and its dependencies: Layout. This trait already implemented for BitField.

Associated Types

Required methods

Returns a copy of the contained value.

Sets the contained value.

Provided methods

Replaces the contained value with val, and returns the old contained value.

let mut simple = Simple(42);

assert_eq!(42, simple.replace(13));
assert_eq!(13, simple.get());

Swaps the values of two bitfields.

let mut one = Simple(1);
let mut two = Simple(2);

one.swap(&mut two);

assert!(one.get() == 2 && two.get() == 1);

Updates the contained value using a function and returns the new value.

let mut simple = Simple(1111111111);

assert_eq!(2222222222, simple.update(|x| x * 2));
assert_eq!(2222222222, simple.get());

Set the specified bit (flag) in-place. Returns current state

let mut simple = Simple(0b10011001);

assert_eq!(false, simple.insert_flag(2, true));
assert_eq!(0b10011101, simple.get());

The specified bit (flag) will be inverted

let mut simple = Simple(0b10011001);

simple.toggle_flag(0);

assert_eq!(0b10011000, simple.get());

Return iterator through bitfield value bits. Every bit represents as bool value.

let mut simple = Simple(0b01010101);

assert!(simple.bits().step_by(2).all(|v| v == true));

Return iterator through bitfield value flags. Every flag contains bit state (set or unset) and item (record) value - string in simple case.

struct Simple(u8);
impl Layout for Simple {
    type Layout = slice::Iter<'static, &'static str>;
    fn layout() -> Self::Layout {
        [
            "First",
            "Second",
            "Third",
            "Fourth",
            "Fifth",
            "Sixth",
            "Seventh",
            "Eighth",
        ].iter()
    }
}
let mut simple = Simple(0b01010101);

assert_eq!(Flag { position: 0, is_set: true, value: &"First"}, simple.flags().next().unwrap());

Helps to find difference between two bitfield values.

struct Simple(u8);
impl Layout for Simple {
    type Layout = slice::Iter<'static, &'static str>;
    fn layout() -> Self::Layout {
        [
            "First",
            "Second",
            "Third",
            "Fourth",
            "Fifth",
            "Sixth",
            "Seventh",
            "Eighth",
        ].iter()
    }
}
let mut left =  Simple(0b01010101);
let mut right = Simple(0b01010001);

assert_eq!(vec![Either::Left((2, &"Third"))], left.diff(right).collect::<Vec<_>>());

Find specific flag state. None if not find

struct Simple(u8);
impl Layout for Simple {
    type Layout = slice::Iter<'static, &'static str>;
    fn layout() -> Self::Layout {
        [
            "First",
            "Second",
            "Third",
            "Fourth",
            "Fifth",
            "Sixth",
            "Seventh",
            "Eighth",
        ].iter()
    }
}
let mut simple =  Simple(0b01010101);

assert_eq!(Some(true), simple.find_state(|v| v == &"Third"));
assert_eq!(None, simple.find_state(|v| v == &"Thirddd"));

Implementors