整型的相关 bit æ“作,如:
rust
use erg_bits::BitsOps;
use erg_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);
在 BitsOps 接å£çš„åŸºç¡€ä¸Šæ·»åŠ äº†ç»“æž„ä½“ bit å—段相关的接å£ï¼Œä»¥åŠè¾…助实现的å®ï¼š
```rust use ergbits::field::BufferWriter; use ergbits::field::BufferReader; use ergbits::IntoBits; use ergbits::BitsOps;
pub struct FoolData { data: u32, data1: u32, } impl BufferWriter for FoolData{} impl BufferReader for FoolData{}
// bit å—段 1,其余类推 pub struct Flag1; pub struct Flag2; pub struct Flag3; pub struct Flag4; pub struct Flag5; pub struct Flag6;
bits::fields! {
FoolData [data] {
Flag1 [0..=3, rw, u32],
Flag2 [4..=5, rw, u32],
Flag3 [6, ro, bool],
Flag4 [7, rw, bool],
Flag5 [8..=9, rw, bool] {
inputconverter: |x| match x {
true => 0x1,
_ => 0x0
};
outputconverter: |x| match x {
0x1 => true,
_ => false
}
}
}
FoolData [data1] {
Flag6 [0..=3, rw, u32]
}
}
let mut fool = FoolData {data:0x0, data1: 0x0};
fool.write::
fool.write::
// error: the trait FieldWriter<FoolData>
is not implemented for Flag3
// fool.write::
fool.write::
let flag3 = fool.read::
let flag4 = fool.read::
fool.data = fool.data.bits(8..=9).write(0x2); // set fool.data bits 8..=9 to 0x2
assert_eq!(false, fool.read::
fool.write::
// bit ä½è¿ä»£å™¨ for bit in 0x55u8.bits() { print!("{}", bit.is_set()); } ```