Rename Const to ConstField
This commit is contained in:
@@ -67,7 +67,7 @@ impl Field {
|
|||||||
/// # Invariant
|
/// # Invariant
|
||||||
///
|
///
|
||||||
/// - The value must fit in the bit field: `num_bits(value) <= field.len`.
|
/// - The value must fit in the bit field: `num_bits(value) <= field.len`.
|
||||||
pub struct Const {
|
pub struct ConstField {
|
||||||
/// The bit field.
|
/// The bit field.
|
||||||
pub field: Field,
|
pub field: Field,
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ pub struct Const {
|
|||||||
pub value: usize,
|
pub value: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Const {
|
impl ConstField {
|
||||||
/// Checks that the bit field has its value.
|
/// Checks that the bit field has its value.
|
||||||
pub fn check(&self, word: u32) -> bool {
|
pub fn check(&self, word: u32) -> bool {
|
||||||
self.field.get(word) == self.value
|
self.field.get(word) == self.value
|
||||||
@@ -137,8 +137,10 @@ impl Checksum {
|
|||||||
///
|
///
|
||||||
/// # Preconditions
|
/// # Preconditions
|
||||||
///
|
///
|
||||||
/// - The checksum bits should be set to one.
|
/// - The bits of the checksum bit field should be set to one: `self.field.get(*word) ==
|
||||||
/// - The number of zeros in the word plus the value should fit in the checksum.
|
/// self.field.mask()`.
|
||||||
|
/// - The checksum value should fit in the checksum bit field: `num_bits(word.count_zeros() +
|
||||||
|
/// value) < self.field.len`.
|
||||||
pub fn set(&self, word: &mut u32, value: usize) {
|
pub fn set(&self, word: &mut u32, value: usize) {
|
||||||
debug_assert_eq!(self.field.get(*word), self.field.mask() as usize);
|
debug_assert_eq!(self.field.get(*word), self.field.mask() as usize);
|
||||||
self.field.set(word, word.count_zeros() as usize + value);
|
self.field.set(word, word.count_zeros() as usize + value);
|
||||||
@@ -164,8 +166,8 @@ pub struct Length {
|
|||||||
/// - `$name: Field <= $max,` to define a bit field of minimum length to store `$max`
|
/// - `$name: Field <= $max,` to define a bit field of minimum length to store `$max`
|
||||||
/// - `$name: Checksum <= $max,` to define a checksum of minimum length to store `$max`
|
/// - `$name: Checksum <= $max,` to define a checksum of minimum length to store `$max`
|
||||||
/// - `$name: Length,` to define a length tracker
|
/// - `$name: Length,` to define a length tracker
|
||||||
/// - `$name: Const = [$bits],` to define a constant bit field with value `$bits` (a sequence of
|
/// - `$name: ConstField = [$bits],` to define a constant bit field with value `$bits` (a sequence
|
||||||
/// space-separated bits)
|
/// of space-separated bits)
|
||||||
#[cfg_attr(doc, macro_export)] // For `cargo doc` to produce documentation.
|
#[cfg_attr(doc, macro_export)] // For `cargo doc` to produce documentation.
|
||||||
macro_rules! bitfield {
|
macro_rules! bitfield {
|
||||||
($($input: tt)*) => {
|
($($input: tt)*) => {
|
||||||
@@ -210,7 +212,8 @@ macro_rules! bitfield_impl {
|
|||||||
[$($input)*]
|
[$($input)*]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
([$($output: tt)*]{ pos: $pos: expr }[$name: ident: Const = $bits: tt, $($input: tt)*]) => {
|
([$($output: tt)*]{ pos: $pos: expr }
|
||||||
|
[$name: ident: ConstField = $bits: tt, $($input: tt)*]) => {
|
||||||
bitfield_impl! {
|
bitfield_impl! {
|
||||||
Reverse $name []$bits
|
Reverse $name []$bits
|
||||||
[$($output)*]{ pos: $pos }[$($input)*]
|
[$($output)*]{ pos: $pos }[$($input)*]
|
||||||
@@ -231,7 +234,7 @@ macro_rules! bitfield_impl {
|
|||||||
(Reverse $name: ident $bits: tt []
|
(Reverse $name: ident $bits: tt []
|
||||||
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
||||||
bitfield_impl! {
|
bitfield_impl! {
|
||||||
Const $name { len: 0, val: 0 }$bits
|
ConstField $name { len: 0, val: 0 }$bits
|
||||||
[$($output)*]{ pos: $pos }[$($input)*]
|
[$($output)*]{ pos: $pos }[$($input)*]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -239,10 +242,10 @@ macro_rules! bitfield_impl {
|
|||||||
// Auxiliary rules for constant bit fields:
|
// Auxiliary rules for constant bit fields:
|
||||||
// - Input is a sequence of bits in reversed order
|
// - Input is a sequence of bits in reversed order
|
||||||
// - Output is the constant bit field definition with the sequence of bits as value
|
// - Output is the constant bit field definition with the sequence of bits as value
|
||||||
(Const $name: ident { len: $len: expr, val: $val: expr }[]
|
(ConstField $name: ident { len: $len: expr, val: $val: expr }[]
|
||||||
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
||||||
bitfield_impl! {
|
bitfield_impl! {
|
||||||
[$($output)* const $name: Const = Const {
|
[$($output)* const $name: ConstField = ConstField {
|
||||||
field: Field { pos: $pos, len: $len },
|
field: Field { pos: $pos, len: $len },
|
||||||
value: $val,
|
value: $val,
|
||||||
};]
|
};]
|
||||||
@@ -250,10 +253,10 @@ macro_rules! bitfield_impl {
|
|||||||
[$($input)*]
|
[$($input)*]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(Const $name: ident { len: $len: expr, val: $val: expr }[$bit: tt $($bits: tt)*]
|
(ConstField $name: ident { len: $len: expr, val: $val: expr }[$bit: tt $($bits: tt)*]
|
||||||
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
[$($output: tt)*]{ pos: $pos: expr }[$($input: tt)*]) => {
|
||||||
bitfield_impl! {
|
bitfield_impl! {
|
||||||
Const $name { len: $len + 1, val: $val * 2 + $bit }[$($bits)*]
|
ConstField $name { len: $len + 1, val: $val * 2 + $bit }[$($bits)*]
|
||||||
[$($output)*]{ pos: $pos }[$($input)*]
|
[$($output)*]{ pos: $pos }[$($input)*]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -287,8 +290,8 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn const_ok() {
|
fn const_field_ok() {
|
||||||
let field = Const {
|
let field = ConstField {
|
||||||
field: Field { pos: 3, len: 5 },
|
field: Field { pos: 3, len: 5 },
|
||||||
value: 9,
|
value: 9,
|
||||||
};
|
};
|
||||||
@@ -333,16 +336,16 @@ mod tests {
|
|||||||
fn bitfield_ok() {
|
fn bitfield_ok() {
|
||||||
bitfield! {
|
bitfield! {
|
||||||
FIELD: Field <= 127,
|
FIELD: Field <= 127,
|
||||||
CONST: Const = [0 1 0 1],
|
CONST_FIELD: ConstField = [0 1 0 1],
|
||||||
BIT: Bit,
|
BIT: Bit,
|
||||||
CHECKSUM: Checksum <= 58,
|
CHECKSUM: Checksum <= 58,
|
||||||
LENGTH: Length,
|
LENGTH: Length,
|
||||||
}
|
}
|
||||||
assert_eq!(FIELD.pos, 0);
|
assert_eq!(FIELD.pos, 0);
|
||||||
assert_eq!(FIELD.len, 7);
|
assert_eq!(FIELD.len, 7);
|
||||||
assert_eq!(CONST.field.pos, 7);
|
assert_eq!(CONST_FIELD.field.pos, 7);
|
||||||
assert_eq!(CONST.field.len, 4);
|
assert_eq!(CONST_FIELD.field.len, 4);
|
||||||
assert_eq!(CONST.value, 10);
|
assert_eq!(CONST_FIELD.value, 10);
|
||||||
assert_eq!(BIT.pos, 11);
|
assert_eq!(BIT.pos, 11);
|
||||||
assert_eq!(CHECKSUM.field.pos, 12);
|
assert_eq!(CHECKSUM.field.pos, 12);
|
||||||
assert_eq!(CHECKSUM.field.len, 6);
|
assert_eq!(CHECKSUM.field.len, 6);
|
||||||
|
|||||||
Reference in New Issue
Block a user