2 个版本
| 0.1.1 | 2020 年 3 月 21 日 |
|---|---|
| 0.1.0 | 2020 年 1 月 20 日 |
#9 in #bitpacking
3,014 每月下载次数
用于 11 个 crate(直接使用 3 个)
24KB
428 代码行
Bitmatch
bitmatch crate 提供了用于将整数打包和拆包为位序列的工具。支持 #![no_std]。
示例
使用 #[bitmatch] 与 let 拆包,将位拆分为每个字母单独的单字符变量。
使用 bitpack!() 将这些单字符变量重新打包成一个整数。
use bitmatch::bitmatch;
#[bitmatch]
fn interleave(n: u8) -> u8 {
#[bitmatch]
let "xxxx_yyyy" = n;
bitpack!("xyxy_xyxy")
}
fn main() {
assert_eq!(interleave(0xF0), 0xAA);
}
您还可以在 match 上使用 #[bitmatch],它将确保字面部分匹配,并绑定变量部分。
use bitmatch::bitmatch;
#[bitmatch]
fn decode(inst: u8) -> String {
#[bitmatch]
match inst {
"00oo_aabb" => format!("Op {}, {}, {}", o, a, b),
"0100_aaii" => format!("Val {}, {}", a, i),
"01??_????" => format!("Invalid"),
"10ii_aabb" => format!("Ld {}, {}, {}", a, b, i),
"11ii_aabb" => format!("St {}, {}, {}", a, b, i),
}
}
依赖关系
~2MB
~47K SLoC