Trait BitHelper

Source
pub trait BitHelper {
    // Required methods
    fn read_bit(&self, pos: u32) -> bool;
    fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self;
    fn read_bits_in_range(&self, bit_range: &BitRange) -> Self;
    fn write_bits_in_range(
        &mut self,
        bit_range: &BitRange,
        val: Self,
    ) -> &mut Self;
}
Expand description

Trait containing helper methods for bit operations.

Required Methods§

Source

fn read_bit(&self, pos: u32) -> bool

Reads the value of the bit at position pos

Source

fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self

Changes the value of the bit at position pos to val

Source

fn read_bits_in_range(&self, bit_range: &BitRange) -> Self

Reads the value stored within the specified range of bits

§Example
#[macro_use]
use dbs_arch::cpuid::bit_helper::*;

let val: u32 = 0b000010001000;
let range = BitRange {
    msb_index: 7,
    lsb_index: 3,
};
println!("binary value: {:b}", val.read_bits_in_range(&range));

The code above will print:

binary value: 10001
Source

fn write_bits_in_range(&mut self, bit_range: &BitRange, val: Self) -> &mut Self

Stores a value within the specified range of bits

§Example
#[macro_use]
use dbs_arch::cpuid::bit_helper::*;

let mut val: u32 = 0;
let range = BitRange {
    msb_index: 7,
    lsb_index: 3,
};
val.write_bits_in_range(&range, 0b10001 as u32);
println!("binary value: {:b}", val);

The code above will print:

binary value: 10001000

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.

Implementations on Foreign Types§

Source§

impl BitHelper for u32

Source§

fn read_bit(&self, pos: u32) -> bool

Source§

fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self

Source§

fn read_bits_in_range(&self, range: &BitRange) -> Self

Source§

fn write_bits_in_range(&mut self, range: &BitRange, val: Self) -> &mut Self

Implementors§