pub struct Bits<V: IntoBits> { /* private fields */ }
Expand description
该结构体可以通过 0x10u32.bits(0x01)
来构造
use bits::BitsOps;
use bits::IntoBits;
assert_eq!(0u8.bits(0).set(), 0x01);
assert_eq!(0u8.bits(1).set(), 0x02);
assert_eq!(0u8.bits(4..=7).set(), 0xf0);
assert_eq!(0xffu8.bits(4..=7).clr(), 0x0f);
assert_eq!(0xffu8.bits(3).revert(), 0xf7);
assert_eq!(0xffu8.bits(4..=7).revert(), 0x0f);
assert_eq!(0u8.bits(4..=7).write(0x10), 0x0);
// 只会写入 value 的相应的 bit 位。低 4 bit 并不会被修改。
assert_eq!(0u8.bits(4..=7).write(0x12), 0x20);
assert_eq!(0x12u8.bits(4..=7).read(), 0x1);
assert_eq!(0xf0u8.bits(4..=7).is_set(), true);
assert_eq!(0x70u8.bits(4..=7).is_set(), false);
assert_eq!(0x70u8.bits(4..=7).is_clr(), false);
assert_eq!(0x70u8.bits(0..=3).is_clr(), true);
单独构造该结构体主要是为了将 bit range 和要写入的值分开,这两者的类型可能会一样,在无 IDE 类型提示的情况下导致调用顺序颠倒:
0u8.bits_write(5, 1)
无法区分哪一个是 range,哪一个是要写入的值。
当然也可以通过 0u8.bits_set(5);
来避免,但 bits_write 的存在依旧会暴露风险。
综上选择单独构造 Bits 结构体。
关于溢出
只尽可能的使输出的值符合预期:
0u8.bits(0..=10).set() == 0xff
0xffu8.bits(3..2).clr() == 0xff
当然这两个代码片段在非 release 编译下会导致溢出 panic(rust 自带的溢出检查)。
Trait Implementations
sourceimpl BitsOps<u128> for Bits<u128>
impl BitsOps<u128> for Bits<u128>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> u128
fn clr(&self) -> u128
fn revert(&self) -> u128
fn write(&self, value: u128) -> u128
fn read(&self) -> u128
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
sourceimpl BitsOps<u16> for Bits<u16>
impl BitsOps<u16> for Bits<u16>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> u16
fn clr(&self) -> u16
fn revert(&self) -> u16
fn write(&self, value: u16) -> u16
fn read(&self) -> u16
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
sourceimpl BitsOps<u32> for Bits<u32>
impl BitsOps<u32> for Bits<u32>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> u32
fn clr(&self) -> u32
fn revert(&self) -> u32
fn write(&self, value: u32) -> u32
fn read(&self) -> u32
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
sourceimpl BitsOps<u64> for Bits<u64>
impl BitsOps<u64> for Bits<u64>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> u64
fn clr(&self) -> u64
fn revert(&self) -> u64
fn write(&self, value: u64) -> u64
fn read(&self) -> u64
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
sourceimpl BitsOps<u8> for Bits<u8>
impl BitsOps<u8> for Bits<u8>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> u8
fn clr(&self) -> u8
fn revert(&self) -> u8
fn write(&self, value: u8) -> u8
fn read(&self) -> u8
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
sourceimpl BitsOps<usize> for Bits<usize>
impl BitsOps<usize> for Bits<usize>
sourcefn count_ones(&self) -> u32
fn count_ones(&self) -> u32
运行效率和标准库(编译器内部提供的)不相上下。
fn msb(&self) -> bool
fn lsb(&self) -> bool
fn set(&self) -> usize
fn clr(&self) -> usize
fn revert(&self) -> usize
fn write(&self, value: usize) -> usize
fn read(&self) -> usize
fn is_clr(&self) -> bool
fn is_set(&self) -> bool
Auto Trait Implementations
impl<V> RefUnwindSafe for Bits<V>where
V: RefUnwindSafe,
impl<V> Send for Bits<V>where
V: Send,
impl<V> Sync for Bits<V>where
V: Sync,
impl<V> Unpin for Bits<V>where
V: Unpin,
impl<V> UnwindSafe for Bits<V>where
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more