I have at least four structures that are all the same, but with different names. Each struct holds a single usize field.
#[derive(Clone, Copy, Debug)]
pub struct Pmpcfg0 {
bits: usize,
}
These usize fields, 32 bits, are densely packed meaning that each byte of the register controls separate functionality. I want to return another structure from this one that takes a single byte from the bits
field that describes the configuration via a couple of enums and a bool.
/// Pmpconfig struct to hold pmp register's current settings of a certain byte
#[derive(Clone, Copy, Debug)]
pub struct Pmpconfig {
pub permission: Permission,
pub range_type: Range,
pub locked: bool,
}
I have never used traits before, and I'm not sure how to structure it, my idea is to define a trait that can be implemented on all of the four Pmpconfig<x> structs
.
pub trait Pmp {
fn pmp_byte(&self, index: usize) -> PmpByte {}
fn permission(&self) -> Option<Permission> {}
fn locked(&self) -> bool {}
fn pmp_cfg(&self, index: usize) -> Pmpconfig {}
}
My question is on the pmp_cfg
method as it needs to return a struct with fields populated by the other methods. I feel like this isn't quite the setup I need. Any insight or recommendations?
More of the code I am playing with
https://github.com/dkhayes117/riscv/blob/master/src/register/pmpcfgx.rs