macro_rules! set_bits {
($value:expr, $new_value:literal, $start:literal) => { ... };
($value:expr, $new_value:literal, $end:literal:$start:literal) => { ... };
($value:expr, $new_value:literal, $start:literal..=$end:literal) => { ... };
($value:expr, $new_value:expr, $start:literal..=$end:literal) => { ... };
($value:expr, $new_value:literal, WITH RANGE $bitrange:expr) => { ... };
($value:expr, $new_value:expr, WITH RANGE $bitrange:expr) => { ... };
($($tt:tt)*) => { ... };
}Expand description
Set bits of a value using a supplied bitrange.
§Panics
This macro will always evaluate at compile time, if new_value is a literal.
Otherwise it can panic at runtime, if not const evaluated, when:
- given an invalid bitrange.
- the value is out of bounds of the bitrange.
§Syntax
The syntax for the macro is as follows:
|seperates the possible syntax optionsmsb/lsbare integer bit indices:msb:lsbis an inclusive range (withmsb >= lsb)
expris anything that evaluates to aBitRangeInclusive, including a path.
set_bits!(
value, new_value, [lsb | msb:lsb | lsb..=msb | WITH RANGE expr]
)Using msb:lsb instead of lsb..=msb is inspired by the syntax Intel uses in their manuals.
§Example
use bitdecl::{BitRangeInclusive, bitrange, set_bits};
// set bits using a literal bit and value
let mut value = 0b00000010u8;
set_bits!(value, 0b00000001u8, 0);
assert!(0b00000011u8 == value);
// set bits using a inclusive range and value
let mut value = 0b00000010u8;
// intel syntax
set_bits!(value, 0b00000001u8, 0:0);
assert!(0b00000011u8 == value);
// standard syntax
set_bits!(value, 0b00000001u8, 0:0);
assert!(0b00000011u8 == value);
// set bits using a literal value and a range from a constant
const BITRANGE: BitRangeInclusive<u8> = bitrange!(0..=0);
let mut value = 0b00000010u8;
set_bits!(value, 0b00000001u8, WITH RANGE BITRANGE);
assert!(0b00000011u8 == value);