Skip to content

Commit 928f9f8

Browse files
committed
fixup: apply review suggestions
1 parent 72085ae commit 928f9f8

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

riscv/src/register/mvien.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! mvien register
22
33
use crate::bits::{bf_extract, bf_insert};
4+
use riscv_pac::result::{Error, Result};
45
use riscv_pac::InterruptNumber;
56

67
#[cfg(target_arch = "riscv32")]
@@ -49,29 +50,45 @@ impl Mvien {
4950
}
5051

5152
/// Check if a specific core interrupt source is enabled.
53+
///
54+
/// Returns `Error` if the interrupt number is invalid.
5255
#[inline]
53-
pub fn is_enabled<I: InterruptNumber>(&self, interrupt: I) -> bool {
56+
pub fn is_enabled<I: InterruptNumber>(&self, interrupt: I) -> Result<bool> {
5457
let n = interrupt.number();
55-
Self::is_valid_interrupt(n) && bf_extract(self.bits, n, 1) != 0
58+
if Self::is_valid_interrupt(n) {
59+
Ok(bf_extract(self.bits, n, 1) != 0)
60+
} else {
61+
Err(Error::InvalidVariant(n))
62+
}
5663
}
5764

5865
/// Enable a specific core interrupt source.
66+
///
67+
/// Returns `Error` if the interrupt number is invalid.
5968
#[inline]
60-
pub fn enable<I: InterruptNumber>(&mut self, interrupt: I) {
69+
pub fn enable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> {
6170
let n = interrupt.number();
6271

6372
if Self::is_valid_interrupt(n) {
6473
self.bits = bf_insert(self.bits, n, 1, 1);
74+
Ok(())
75+
} else {
76+
Err(Error::InvalidVariant(n))
6577
}
6678
}
6779

6880
/// Disable a specific core interrupt source.
81+
///
82+
/// Returns `Error` if the interrupt number is invalid.
6983
#[inline]
70-
pub fn disable<I: InterruptNumber>(&mut self, interrupt: I) {
84+
pub fn disable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> {
7185
let n = interrupt.number();
7286

7387
if Self::is_valid_interrupt(n) {
7488
self.bits = bf_insert(self.bits, n, 1, 0);
89+
Ok(())
90+
} else {
91+
Err(Error::InvalidVariant(n))
7592
}
7693
}
7794
}
@@ -94,15 +111,14 @@ read_composite_csr!(super::mvienh::read().bits(), read().bits());
94111
#[cfg(test)]
95112
mod tests {
96113
use super::*;
97-
use riscv_pac::result::{Error, Result};
98114

99115
/// Represents a custom set of virtual interrupts.
100116
///
101117
/// NOTE: a real implementation may want to enumerate the valid virtual interrupt variants.
102118
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
103119
pub struct VirtualInterrupt(usize);
104120

105-
/// SAFETY: `Interrupt` represents the standard RISC-V interrupts
121+
/// SAFETY: `VirtualInterrupt` represents the virtual RISC-V interrupts
106122
unsafe impl InterruptNumber for VirtualInterrupt {
107123
const MAX_INTERRUPT_NUMBER: usize = Mvien::MAX_INTERRUPT;
108124

@@ -132,13 +148,13 @@ mod tests {
132148
(0..=VirtualInterrupt::MAX_INTERRUPT_NUMBER)
133149
.filter_map(|n| VirtualInterrupt::from_number(n).ok())
134150
.for_each(|int| {
135-
assert!(!m.is_enabled(int));
151+
assert_eq!(m.is_enabled(int), Ok(false));
136152

137-
m.enable(int);
138-
assert!(m.is_enabled(int));
153+
assert!(m.enable(int).is_ok());
154+
assert_eq!(m.is_enabled(int), Ok(true));
139155

140-
m.disable(int);
141-
assert!(!m.is_enabled(int));
156+
assert!(m.disable(int).is_ok());
157+
assert_eq!(m.is_enabled(int), Ok(false));
142158
});
143159
}
144160
}

riscv/src/register/mvienh.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! mvienh register
22
33
use crate::bits::{bf_extract, bf_insert};
4+
use riscv_pac::result::{Error, Result};
45
use riscv_pac::InterruptNumber;
56

67
read_write_csr! {
@@ -33,45 +34,57 @@ impl Mvienh {
3334
}
3435

3536
/// Check if a specific core interrupt source is enabled.
37+
///
38+
/// Returns `Error` if the interrupt number is invalid.
3639
#[inline]
37-
pub fn is_enabled<I: InterruptNumber>(&self, interrupt: I) -> bool {
40+
pub fn is_enabled<I: InterruptNumber>(&self, interrupt: I) -> Result<bool> {
3841
let n = interrupt.number();
39-
Self::is_valid_interrupt(n) && bf_extract(self.bits, Self::shift_interrupt(n), 1) != 0
42+
43+
if Self::is_valid_interrupt(n) {
44+
Ok(bf_extract(self.bits, Self::shift_interrupt(n), 1) != 0)
45+
} else {
46+
Err(Error::InvalidVariant(n))
47+
}
4048
}
4149

4250
/// Enable a specific core interrupt source.
4351
#[inline]
44-
pub fn enable<I: InterruptNumber>(&mut self, interrupt: I) {
52+
pub fn enable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> {
4553
let n = interrupt.number();
4654

4755
if Self::is_valid_interrupt(n) {
4856
self.bits = bf_insert(self.bits, Self::shift_interrupt(n), 1, 1);
57+
Ok(())
58+
} else {
59+
Err(Error::InvalidVariant(n))
4960
}
5061
}
5162

5263
/// Disable a specific core interrupt source.
5364
#[inline]
54-
pub fn disable<I: InterruptNumber>(&mut self, interrupt: I) {
65+
pub fn disable<I: InterruptNumber>(&mut self, interrupt: I) -> Result<()> {
5566
let n = interrupt.number();
5667

5768
if Self::is_valid_interrupt(n) {
5869
self.bits = bf_insert(self.bits, Self::shift_interrupt(n), 1, 0);
70+
Ok(())
71+
} else {
72+
Err(Error::InvalidVariant(n))
5973
}
6074
}
6175
}
6276

6377
#[cfg(test)]
6478
mod tests {
6579
use super::*;
66-
use riscv_pac::result::{Error, Result};
6780

6881
/// Represents a custom set of virtual interrupts.
6982
///
7083
/// NOTE: a real implementation may want to enumerate the valid virtual interrupt variants.
7184
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7285
pub struct VirtualInterrupt(usize);
7386

74-
/// SAFETY: `Interrupt` represents the standard RISC-V interrupts
87+
/// SAFETY: `VirtualInterrupt` represents the virtual RISC-V interrupts
7588
unsafe impl InterruptNumber for VirtualInterrupt {
7689
const MAX_INTERRUPT_NUMBER: usize = Mvienh::MAX_INTERRUPT;
7790

@@ -97,13 +110,13 @@ mod tests {
97110
(Mvienh::MIN_INTERRUPT..=Mvienh::MAX_INTERRUPT)
98111
.filter_map(|n| VirtualInterrupt::from_number(n).ok())
99112
.for_each(|int| {
100-
assert!(!m.is_enabled(int));
113+
assert_eq!(m.is_enabled(int), Ok(false));
101114

102-
m.enable(int);
103-
assert!(m.is_enabled(int));
115+
assert!(m.enable(int).is_ok());
116+
assert_eq!(m.is_enabled(int), Ok(true));
104117

105-
m.disable(int);
106-
assert!(!m.is_enabled(int));
118+
assert!(m.disable(int).is_ok());
119+
assert_eq!(m.is_enabled(int), Ok(false));
107120
});
108121
}
109122
}

0 commit comments

Comments
 (0)