Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/porglezomp/bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
https://github.com/porglezomp/bitmatch
binary bitpacking decoding encoding no-std rust rust-library
Last synced: 12 days ago
JSON representation
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
- Host: GitHub
- URL: https://github.com/porglezomp/bitmatch
- Owner: porglezomp
- Created: 2020-01-19T23:16:13.000Z (almost 5 years ago)
- Default Branch: develop
- Last Pushed: 2020-04-20T21:06:51.000Z (over 4 years ago)
- Last Synced: 2024-10-11T03:17:44.057Z (28 days ago)
- Topics: binary, bitpacking, decoding, encoding, no-std, rust, rust-library
- Language: Rust
- Homepage: https://docs.rs/bitmatch
- Size: 41 KB
- Stars: 108
- Watchers: 8
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bitmatch
[![Crates.io Library Version Number](https://img.shields.io/crates/v/bitmatch)](https://crates.io/crates/bitmatch)
[![Crates.io Library License](https://img.shields.io/crates/l/bitmatch)](https://www.mozilla.org/en-US/MPL/2.0/)
[![Docs.rs Documentation Version Number](https://docs.rs/bitmatch/badge.svg)](https://docs.rs/bitmatch/)The bitmatch crate provides tools for packing and unpacking integers as
sequences of bits. Supports `#![no_std]`.## Examples
Using `#[bitmatch]` with a `let` unpacks the bits into separate
single-character variables for each letter you use.Using `bitpack!()` re-packs the bits from those single-character variables
into a single integer.```rust
use bitmatch::bitmatch;#[bitmatch]
fn interleave(n: u8) -> u8 {
#[bitmatch]
let "xxxx_yyyy" = n;
bitpack!("xyxy_xyxy")
}fn main() {
assert_eq!(interleave(0xF0), 0xAA);
}
```You can use `#[bitmatch]` on a `match` as well, and it will ensure that the
literal portions match, and bind the variable portions.```rust
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),
}
}
```