@@ -221,15 +221,6 @@ impl<REG: RegisterSpec> W<REG> {
221221 }
222222}
223223
224- /// Used if enumerated values cover not the whole range.
225- #[ derive( Clone , Copy , PartialEq ) ]
226- pub enum Variant < U , T > {
227- /// Expected variant.
228- Val ( T ) ,
229- /// Raw bits.
230- Res ( U ) ,
231- }
232-
233224/// Field reader.
234225///
235226/// Result of the `read` methods of fields.
@@ -287,3 +278,84 @@ impl<FI> FieldReader<bool, FI> {
287278 self . bit ( )
288279 }
289280}
281+
282+ /// Used if enumerated values cover not the whole range.
283+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
284+ pub enum Variant < U , T > {
285+ /// Expected variant.
286+ Val ( T ) ,
287+ /// Raw bits.
288+ Res ( U ) ,
289+ }
290+
291+ use Variant :: * ;
292+ impl < U , T > Variant < U , T > {
293+ /// Check if the variant is expected
294+ pub fn is_expected ( & self ) -> bool {
295+ match self {
296+ Val ( _) => true ,
297+ Res ( _) => false ,
298+ }
299+ }
300+
301+ /// Check if the variant is not expected
302+ pub fn is_reserved ( & self ) -> bool {
303+ match self {
304+ Val ( _) => false ,
305+ Res ( _) => true ,
306+ }
307+ }
308+
309+ /// Moves the value `v` out of the `Variant` if it is `Val(v)`.
310+ ///
311+ /// Panics if the self value equals `Res`
312+ #[ inline]
313+ pub fn unwrap ( self ) -> T {
314+ match self {
315+ Val ( v) => v,
316+ Res ( _) => panic ! ( "Unexpected variant" ) ,
317+ }
318+ }
319+
320+ /// Returns the contained value or a default
321+ #[ inline]
322+ pub fn unwrap_or ( self , def : T ) -> T {
323+ match self {
324+ Val ( v) => v,
325+ Res ( _) => def,
326+ }
327+ }
328+
329+ /// Returns the contained value or computes it from a closure
330+ #[ inline]
331+ pub fn unwrap_or_else < F : FnOnce ( U ) -> T > ( self , f : F ) -> T {
332+ match self {
333+ Val ( v) => v,
334+ Res ( u) => f ( u) ,
335+ }
336+ }
337+
338+ /// Unwraps a result, yielding the content of an `Val`.
339+ ///
340+ /// Panics if the value is an `Res`, with a panic message including the
341+ /// passed message, and the content of the `Res`.
342+ pub fn expect ( self , msg : & ' static str ) -> T {
343+ match self {
344+ Val ( v) => v,
345+ Res ( _) => panic ! ( msg) ,
346+ }
347+ }
348+ }
349+
350+ impl < U , T > Variant < U , T >
351+ where
352+ T : Into < U >
353+ {
354+ /// Get raw bits
355+ pub fn to_bits ( self ) -> U {
356+ match self {
357+ Val ( v) => v. into ( ) ,
358+ Res ( u) => u,
359+ }
360+ }
361+ }
0 commit comments