riscv_macros

Attribute Macro pac_enum

source
#[pac_enum]
Expand description

Attribute-like macro that implements the traits of the riscv-pac crate for a given enum.

As these traits are unsafe, the macro must be called with the unsafe keyword followed by the trait name. In this way, we warn callers that they must comply with the requirements of the trait.

The trait name must be one of ExceptionNumber, InterruptNumber, PriorityNumber, or HartIdNumber. Marker traits CoreInterruptNumber and ExternalInterruptNumber cannot be implemented using this macro.

§Safety

The struct to be implemented must comply with the requirements of the specified trait.

§Example

use riscv::*;

#[repr(usize)]
#[pac_enum(unsafe ExceptionNumber)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Exception {
    E1 = 1,
    E3 = 3,
}

fn main() {
    assert_eq!(Exception::E1.number(), 1);
    assert_eq!(Exception::E3.number(), 3);

    assert_eq!(Exception::from_number(1), Ok(Exception::E1));
    assert_eq!(Exception::from_number(2), Err(2));
    assert_eq!(Exception::from_number(3), Ok(Exception::E3));

    assert_eq!(Exception::MAX_EXCEPTION_NUMBER, 3);
}