Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Option-like API for `generic::Variant`

- Generated crates now contain the git commit hash and date of svd2rust
compilation.

Expand Down
90 changes: 81 additions & 9 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,6 @@ impl<REG: RegisterSpec> W<REG> {
}
}

/// Used if enumerated values cover not the whole range.
#[derive(Clone, Copy, PartialEq)]
pub enum Variant<U, T> {
/// Expected variant.
Val(T),
/// Raw bits.
Res(U),
}

/// Field reader.
///
/// Result of the `read` methods of fields.
Expand Down Expand Up @@ -287,3 +278,84 @@ impl<FI> FieldReader<bool, FI> {
self.bit()
}
}

/// Used if enumerated values cover not the whole range.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Variant<U, T> {
/// Expected variant.
Val(T),
/// Raw bits.
Res(U),
}

use Variant::*;
impl<U, T> Variant<U, T> {
/// Check if the variant is expected
pub fn is_expected(&self) -> bool {
match self {
Val(_) => true,
Res(_) => false,
}
}

/// Check if the variant is not expected
pub fn is_reserved(&self) -> bool {
match self {
Val(_) => false,
Res(_) => true,
}
}

/// Moves the value `v` out of the `Variant` if it is `Val(v)`.
///
/// Panics if the self value equals `Res`
#[inline]
pub fn unwrap(self) -> T {
match self {
Val(v) => v,
Res(_) => panic!("Unexpected variant"),
}
}

/// Returns the contained value or a default
#[inline]
pub fn unwrap_or(self, def: T) -> T {
match self {
Val(v) => v,
Res(_) => def,
}
}

/// Returns the contained value or computes it from a closure
#[inline]
pub fn unwrap_or_else<F: FnOnce(U) -> T>(self, f: F) -> T {
match self {
Val(v) => v,
Res(u) => f(u),
}
}

/// Unwraps a result, yielding the content of an `Val`.
///
/// Panics if the value is an `Res`, with a panic message including the
/// passed message, and the content of the `Res`.
pub fn expect(self, msg: &'static str) -> T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need 'static bound here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn’t compile without 'static.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird. The original expect doesn't have it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because original expect use formatted panic!("{}", msg).
With 2 arguments.

https://doc.rust-lang.org/src/core/macros.rs.html#12

match self {
Val(v) => v,
Res(_) => panic!(msg),
}
}
}

impl<U, T> Variant<U, T>
where
T: Into<U>
{
/// Get raw bits
pub fn to_bits(self) -> U {
match self {
Val(v) => v.into(),
Res(u) => u,
}
}
}