Trait BitUtils

Source
pub trait BitUtils: Sized + Copy {
    // Required methods
    fn bit(self, index: u8) -> bool;
    fn try_bit(self, index: u8) -> Option<bool>;
    fn with_bit(self, index: u8, value: bool) -> Self;
    fn try_with_bit(self, index: u8, value: bool) -> Option<Self>;
    fn bits(self, start: u8, end: u8) -> Self;
    fn try_bits(self, start: u8, end: u8) -> Option<Self>;
    fn with_bits(self, start: u8, end: u8, value: Self) -> Self;
    fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>;
}
Expand description

Provides methods for querying and modifying the value of specific bits. Convention is LSB0.

Required Methods§

Source

fn bit(self, index: u8) -> bool

Returns whether the bit at index is set or not. Indices out of range always return false.

Source

fn try_bit(self, index: u8) -> Option<bool>

Returns whether the bit at index is set or not. If out of range, returns None.

Source

fn with_bit(self, index: u8, value: bool) -> Self

Sets the state of the bit at index. Indices out of range return the value unmodified.

Source

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Sets the state of the bit at index. If out of range, returns None.

Source

fn bits(self, start: u8, end: u8) -> Self

Extracts the value between bits start (inclusive) and end (exclusive). Bits out of range are always zero and invalid ranges return zero.

Source

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Extracts the value between bits start (inclusive) and end (exclusive). If the range is invalid, returns None.

Source

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Sets the value between given bits. The value is masked, so a value with more bits than available will drop it’s most significant bits. Bits out of range are left unmodified and invalid ranges return the value unmodified.

Source

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Sets the value between given bits. The value is masked, so a value with more bits than available will drop it’s most significant bits. If the range is invalid, returns None.

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 BitUtils for i8

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for i16

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for i32

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for i64

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for i128

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for u8

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for u16

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for u32

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Source§

impl BitUtils for u64

Source§

fn bit(self, index: u8) -> bool

Source§

fn try_bit(self, index: u8) -> Option<bool>

Source§

fn with_bit(self, index: u8, value: bool) -> Self

Source§

fn try_with_bit(self, index: u8, value: bool) -> Option<Self>

Source§

fn bits(self, start: u8, end: u8) -> Self

Source§

fn try_bits(self, start: u8, end: u8) -> Option<Self>

Source§

fn with_bits(self, start: u8, end: u8, value: Self) -> Self

Source§

fn try_with_bits(self, start: u8, end: u8, value: Self) -> Option<Self>

Implementors§