Skip to content

Commit 50f353e

Browse files
committed
tock-register-interface: improve read_as_enum documentation
The tock-register-interface's README contained an incorrect usage example of the `Readable::read_as_enum` method. Because of its non-trivial type signature, using the custom TryFromValue trait, this adds extensive documentation and doctests outlining the relationship with the generated structures and enums through `register_bitfields!`. Suggested-by: nihalpasham <nihal.pasham@gmail.com> Signed-off-by: Leon Schuermann <leon@is.currently.online>
1 parent d8de432 commit 50f353e

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

libraries/tock-register-interface/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ let any_ints = registers.s.matches_any(Status::TXINTERRUPT + Status::RXINTERRUPT
356356
let mode = registers.cr.read_as_enum(Status::MODE);
357357

358358
match mode {
359-
Some(Status::MODE::FullDuplex) => { /* ... */ }
360-
Some(Status::MODE::HalfDuplex) => { /* ... */ }
359+
Some(Status::MODE::Value::FullDuplex) => { /* ... */ }
360+
Some(Status::MODE::Value::HalfDuplex) => { /* ... */ }
361361

362362
None => unreachable!("invalid value")
363363
}

libraries/tock-register-interface/src/fields.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,42 @@ impl<T: UIntLike, R: RegisterLongName> Field<T, R> {
101101

102102
#[inline]
103103
/// Read value of the field as an enum member
104+
///
105+
/// This method expects to be passed the unasked and unshifted register
106+
/// value, extracts the field value by calling [`Field::read`] and
107+
/// subsequently passes that value to the [`TryFromValue`] implementation on
108+
/// the passed enum type.
109+
///
110+
/// The [`register_bitfields!`](crate::register_bitfields) macro will
111+
/// generate an enum containing the various named field variants and
112+
/// implementing the required [`TryFromValue`] trait. It is accessible as
113+
/// `$REGISTER_NAME::$FIELD_NAME::Value`.
114+
///
115+
/// This method can be useful to symbolically represent read register field
116+
/// states throughout the codebase and to enforce exhaustive matches over
117+
/// all defined valid register field values.
118+
///
119+
/// ## Usage Example
120+
///
121+
/// ```rust
122+
/// # use tock_registers::interfaces::Readable;
123+
/// # use tock_registers::registers::InMemoryRegister;
124+
/// # use tock_registers::register_bitfields;
125+
/// register_bitfields![u8,
126+
/// EXAMPLEREG [
127+
/// TESTFIELD OFFSET(3) NUMBITS(3) [
128+
/// Foo = 2,
129+
/// Bar = 3,
130+
/// Baz = 6,
131+
/// ],
132+
/// ],
133+
/// ];
134+
///
135+
/// match EXAMPLEREG::TESTFIELD.read_as_enum(0x9C) {
136+
/// Some(EXAMPLEREG::TESTFIELD::Value::Bar) => "The value is 3!",
137+
/// _ => panic!("boo!"),
138+
/// };
139+
/// ```
104140
pub fn read_as_enum<E: TryFromValue<T, EnumType = E>>(self, val: T) -> Option<E> {
105141
E::try_from_value(self.read(val))
106142
}

libraries/tock-register-interface/src/interfaces.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,44 @@ pub trait Readable {
173173
field.read(self.get())
174174
}
175175

176-
#[inline]
177176
/// Set the raw register value
177+
///
178+
/// The [`register_bitfields!`](crate::register_bitfields) macro will
179+
/// generate an enum containing the various named field variants and
180+
/// implementing the required [`TryFromValue`] trait. It is accessible as
181+
/// `$REGISTER_NAME::$FIELD_NAME::Value`.
182+
///
183+
/// This method can be useful to symbolically represent read register field
184+
/// states throughout the codebase and to enforce exhaustive matches over
185+
/// all defined valid register field values.
186+
///
187+
/// ## Usage Example
188+
///
189+
/// ```rust
190+
/// # use tock_registers::interfaces::Readable;
191+
/// # use tock_registers::registers::InMemoryRegister;
192+
/// # use tock_registers::register_bitfields;
193+
/// register_bitfields![u8,
194+
/// EXAMPLEREG [
195+
/// TESTFIELD OFFSET(0) NUMBITS(2) [
196+
/// Foo = 0,
197+
/// Bar = 1,
198+
/// Baz = 2,
199+
/// ],
200+
/// ],
201+
/// ];
202+
///
203+
/// let reg: InMemoryRegister<u8, EXAMPLEREG::Register> =
204+
/// InMemoryRegister::new(2);
205+
///
206+
/// match reg.read_as_enum(EXAMPLEREG::TESTFIELD) {
207+
/// Some(EXAMPLEREG::TESTFIELD::Value::Foo) => "Tock",
208+
/// Some(EXAMPLEREG::TESTFIELD::Value::Bar) => "is",
209+
/// Some(EXAMPLEREG::TESTFIELD::Value::Baz) => "awesome!",
210+
/// None => panic!("boo!"),
211+
/// };
212+
/// ```
213+
#[inline]
178214
fn read_as_enum<E: TryFromValue<Self::T, EnumType = E>>(
179215
&self,
180216
field: Field<Self::T, Self::R>,

0 commit comments

Comments
 (0)