use crate::lib_bitfield;
use crate::result::Result;
use super::{ExtCsd, ExtCsdIndex};
lib_bitfield! {
pub HpiManagement(u8): u8 {
pub hpi: 0;
}
}
impl HpiManagement {
pub const MASK: u8 = 0b1;
pub const DEFAULT: u8 = 0b0;
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
pub const fn from_inner(val: u8) -> Self {
Self(val & Self::MASK)
}
pub const fn try_from_inner(val: u8) -> Result<Self> {
Ok(Self::from_inner(val))
}
pub const fn into_inner(self) -> u8 {
self.0
}
}
impl Default for HpiManagement {
fn default() -> Self {
Self::new()
}
}
impl From<bool> for HpiManagement {
fn from(val: bool) -> Self {
Self::from_inner(val as u8)
}
}
impl From<HpiManagement> for bool {
fn from(val: HpiManagement) -> Self {
val.into_inner() != 0
}
}
impl From<u8> for HpiManagement {
fn from(val: u8) -> Self {
Self::from_inner(val)
}
}
impl From<HpiManagement> for u8 {
fn from(val: HpiManagement) -> Self {
val.into_inner()
}
}
impl ExtCsd {
pub const fn hpi_mgmt(&self) -> HpiManagement {
HpiManagement::from_inner(self.0[ExtCsdIndex::HpiMgmt.into_inner()])
}
pub fn set_hpi_mgmt(&mut self, val: HpiManagement) {
self.0[ExtCsdIndex::HpiMgmt.into_inner()] = val.into_inner();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hpi_mgmt() {
let mut ext_csd = ExtCsd::new();
assert_eq!(ext_csd.hpi_mgmt(), HpiManagement::new());
(0..=u8::MAX)
.map(HpiManagement::from_inner)
.for_each(|hpi| {
ext_csd.set_hpi_mgmt(hpi);
assert_eq!(ext_csd.hpi_mgmt(), hpi);
});
}
}