splitbits

Macro splitbits_named

source
splitbits_named!() { /* proc-macro */ }
Expand description

Same as splitbits!, except that full-length variable names can be used. Returns a tuple instead of a generated struct. If there is only a single field specified in the template, returns a single variable instead (not a 1-tuple). Fields are returned in the order that they first appear in the template, and the single character template names are discarded.

use splitbits::splitbits_named;

let (apple_count, banana_count) = splitbits_named!(0b11110000, "aaabbbbb");
assert_eq!(apple_count, 0b111);
assert_eq!(banana_count, 0b10000);

Existing variables can be set, rather than declaring new ones:

use splitbits::splitbits_named;

let mut apple_count = 5;
let banana_count;

/* Various operations on apple_count omitted here. */

// Overwrite the existing values of apple_count and banana_count.
(apple_count, banana_count) = splitbits_named!(0b11110000, "aaabbbbb");
assert_eq!(apple_count, 0b111);
assert_eq!(banana_count, 0b10000);

Just as with splitbits!, the template can have spaces for readability, period placeholders for ignoring certain bits, and fields broken up into multiple segments:

use splitbits::splitbits_named;

let input = 0b1111_0000;
let (apple_count, banana_count) = splitbits_named!(min=u32, input, "ab.b .aaa");
assert_eq!(apple_count, 0b1000u32);
assert_eq!(banana_count, 0b11u32);