Trait BitFieldLayout

Source
pub trait BitFieldLayout: Layout {
    type Value: Copy + IntoBits + FromBits;

    // Required methods
    fn get(&self) -> Self::Value;
    fn set(&mut self, new: Self::Value);

    // Provided methods
    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>  { ... }
    fn flags(
        &self,
    ) -> Flags<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>  { ... }
    fn diff(
        &self,
        other: Self,
    ) -> Diff<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>> 
       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.

Required Associated Types§

Required Methods§

Source

fn get(&self) -> Self::Value

Returns a copy of the contained value.

Source

fn set(&mut self, new: Self::Value)

Sets the contained value.

Provided Methods§

Source

fn replace(&mut self, new: Self::Value) -> Self::Value

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());
Source

fn swap(&mut self, other: &mut Self)

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);
Source

fn update<F>(&mut self, f: F) -> Self::Value
where F: FnOnce(Self::Value) -> Self::Value,

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());
Source

fn insert_flag(&mut self, position: usize, b: bool) -> bool

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());
Source

fn toggle_flag(&mut self, position: usize)

The specified bit (flag) will be inverted

let mut simple = Simple(0b10011001);

simple.toggle_flag(0);

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

fn bits(&self) -> Bits<<Self::Value as IntoBits>::Bytes>

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));
Source

fn flags(&self) -> Flags<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>

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());
Source

fn diff( &self, other: Self, ) -> Diff<Self::Layout, Bits<<Self::Value as IntoBits>::Bytes>>
where Self: Sized,

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<_>>());
Source

fn find_state<P>(&self, predicate: P) -> Option<bool>
where P: Fn(<<Self as Layout>::Layout as Iterator>::Item) -> bool,

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"));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§