Crate bit_seq

Source
Expand description

bit_seq provides procedural macros for generating bit sequences.

§Overview

This crate provides the macro bseq!, which allows for the creation of bit sequences using a simple and intuitive syntax. Bit sequences can be created from raw binary values, hexadecimal values, or even variable expressions. This makes the bit_seq crate a useful tool for systems programming, hardware interfacing, or any application where bit manipulation is common.

bit_seq also provides bseq_8!, bseq_16!, bseq_32!, bseq_64! and bseq_128! to simply type mixing.

§Examples

The following examples illustrate some of the ways bseq! can be used.

§Raw Bit Sequences

use bit_seq::bseq;

let t = bseq!(0110 01 0 1);
assert_eq!(t, 0b0110_01_0_1);

§Hex Values

Hexadecimal values are interpreted as 4-bit sequences.

use bit_seq::bseq;

let t = bseq!(01 0x1f);
assert_eq!(t, 0b01_0001_1111);

§Length Expressions

Length expressions take the form <val>:<len>, where <len> is the number of bits from <val> to be used.

use bit_seq::bseq;

let t = bseq!(3:1 0 0xf:2);
assert_eq!(t, 0b1_0_11);

§Variable Interpolation

Variable interpolation is supported for length expressions.

use bit_seq::bseq;
let var = 0xf;
let t = bseq!(10 var:2);
assert_eq!(t, 0b10_11);

§Unary Operations

The bseq syntax supports unary operations for length expressions. This simplifies bit sequences like 0b111111.

use bit_seq::bseq;
// bit negation
assert_eq!(bseq!(!0:6), 0b111111);

// numerical negation with variable interpolation
let var = 1;
assert_eq!(bseq!(-var:8), 0xff);

§Performance

The bseq! macro compiles down to standard bit manipulation operations, meaning there is no runtime overhead to using it.

Macros§

bseq
bseq is a procedural macro for creating bit sequences.
bseq_8
The bseq_8 procedural macro is specifically tailored for creating 8-bit sequences.
bseq_16
The bseq_16 procedural macro is specifically tailored for creating 16-bit sequences.
bseq_32
The bseq_32 procedural macro is specifically tailored for creating 32-bit sequences.
bseq_64
The bseq_64 procedural macro is designed for creating 64-bit sequences.
bseq_128
The bseq_128 procedural macro is designed for creating 128-bit sequences.