Skip to content

Commit

Permalink
fixup: riscv: use result for CSR enum conversion
Browse files Browse the repository at this point in the history
Uses the crate result type for converting a CSR enum type from a
`usize`.
  • Loading branch information
rmsyn committed Sep 18, 2024
1 parent 158db4f commit 7ad499a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
11 changes: 4 additions & 7 deletions riscv/src/register/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,10 @@ macro_rules! csr_field_enum {
}

/// Attempts to convert a [`usize`] into a valid variant.
pub const fn from_usize(val: usize) -> Option<Self> {
pub const fn from_usize(val: usize) -> $crate::result::Result<Self> {
match val {
$($value => Some(Self::$variant),)+
_ => None,
$($value => Ok(Self::$variant),)+
_ => Err($crate::result::Error::InvalidVariant(val)),
}
}

Expand Down Expand Up @@ -1036,10 +1036,7 @@ macro_rules! read_only_csr_field {
$field_end - $field_start + 1,
);

$field_ty::from_usize(value).ok_or($crate::result::Error::InvalidFieldVariant {
field: stringify!($field),
value,
})
$field_ty::from_usize(value)
} else {
Err($crate::result::Error::IndexOutOfBounds {
index: $field_start,
Expand Down
16 changes: 2 additions & 14 deletions riscv/src/register/tests/read_only_csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ fn test_mtest_read_only() {
assert_eq!(mtest.multi_field(), 0x0);
assert_eq!(mtest.try_multi_field(), Ok(0x0));

assert_eq!(
mtest.try_field_enum(),
Err(Error::InvalidFieldVariant {
field: "field_enum",
value: 0,
})
);
assert_eq!(mtest.try_field_enum(), Err(Error::InvalidVariant(0)),);

[
MtestFieldEnum::Field1,
Expand All @@ -137,11 +131,5 @@ fn test_mtest_read_only() {

// check that setting an invalid variant returns `None`
mtest = Mtest::from_bits(0xbad << 7);
assert_eq!(
mtest.try_field_enum(),
Err(Error::InvalidFieldVariant {
field: "field_enum",
value: 13,
})
);
assert_eq!(mtest.try_field_enum(), Err(Error::InvalidVariant(13)),);
}
16 changes: 2 additions & 14 deletions riscv/src/register/tests/read_write_csr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,7 @@ fn test_mtest_read_write() {
assert_eq!(mtest.multi_field(), 0x0);
assert_eq!(mtest.try_multi_field(), Ok(0x0));

assert_eq!(
mtest.try_field_enum(),
Err(Error::InvalidFieldVariant {
field: "field_enum",
value: 0,
})
);
assert_eq!(mtest.try_field_enum(), Err(Error::InvalidVariant(0)),);

[
MtestFieldEnum::Field1,
Expand All @@ -158,11 +152,5 @@ fn test_mtest_read_write() {

// check that setting an invalid variant returns `None`
mtest = Mtest::from_bits(0xbad << 7);
assert_eq!(
mtest.try_field_enum(),
Err(Error::InvalidFieldVariant {
field: "field_enum",
value: 13,
})
);
assert_eq!(mtest.try_field_enum(), Err(Error::InvalidVariant(13)),);
}
14 changes: 10 additions & 4 deletions riscv/src/register/tests/write_only_csr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::result::Result;
use crate::result::{Error, Result};

write_only_csr! {
/// test CSR register type
Expand Down Expand Up @@ -100,7 +100,10 @@ fn test_mtest_write_only() {

mtest = Mtest::from_bits(0);

assert_eq!(MtestFieldEnum::from_usize(mtest.bits() >> 8), None);
assert_eq!(
MtestFieldEnum::from_usize(mtest.bits() >> 8),
Err(Error::InvalidVariant(0))
);

[
MtestFieldEnum::Field1,
Expand All @@ -116,10 +119,13 @@ fn test_mtest_write_only() {
"field value: {variant:?}"
);
mtest.set_field_enum(variant);
assert_eq!(MtestFieldEnum::from_usize(mtest.bits() >> 8), Some(variant));
assert_eq!(MtestFieldEnum::from_usize(mtest.bits() >> 8), Ok(variant));
});

// check that setting an invalid variant returns `None`
mtest = Mtest::from_bits(0xbad << 8);
assert_eq!(MtestFieldEnum::from_usize(mtest.bits() >> 8), None);
assert_eq!(
MtestFieldEnum::from_usize(mtest.bits() >> 8),
Err(Error::InvalidVariant(13))
);
}

0 comments on commit 7ad499a

Please sign in to comment.