use crate::result::Result;
pub use crate::{CoreInterruptNumber, ExceptionNumber, InterruptNumber};
pub mod machine;
pub mod supervisor;
#[cfg(not(feature = "s-mode"))]
pub use machine::*;
#[cfg(feature = "s-mode")]
pub use supervisor::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Trap<I, E> {
Interrupt(I),
Exception(E),
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TrapError {
InvalidInterrupt(usize),
InvalidException(usize),
}
impl Trap<usize, usize> {
#[inline]
pub fn from<I: CoreInterruptNumber, E: ExceptionNumber>(trap: Trap<I, E>) -> Self {
match trap {
Trap::Interrupt(interrupt) => Trap::Interrupt(interrupt.number()),
Trap::Exception(exception) => Trap::Exception(exception.number()),
}
}
#[inline]
pub fn try_into<I, E>(self) -> Result<Trap<I, E>>
where
I: CoreInterruptNumber,
E: ExceptionNumber,
{
match self {
Trap::Interrupt(code) => Ok(Trap::Interrupt(I::from_number(code)?)),
Trap::Exception(code) => Ok(Trap::Exception(E::from_number(code)?)),
}
}
}
impl<I: CoreInterruptNumber, E: ExceptionNumber> Trap<I, E> {
#[inline]
pub fn into(self) -> Trap<usize, usize> {
Trap::from(self)
}
#[inline]
pub fn try_from(trap: Trap<usize, usize>) -> Result<Self> {
trap.try_into()
}
}