use crate::lib_enum;
use crate::result::{Error, Result};
lib_enum! {
BlockLength: u8 {
default: Bytes512,
error: Error,
Bytes512 = 9,
Bytes1024 = 10,
Bytes2048 = 11,
}
}
impl BlockLength {
#[inline]
pub const fn block_len(self) -> usize {
1usize << (self as u8)
}
pub const fn try_from_inner(val: u8) -> Result<Self> {
Self::from_raw(val)
}
pub const fn into_inner(self) -> u8 {
self.into_raw()
}
}
#[cfg(test)]
mod ext_tests {
use super::*;
#[test]
fn test_block_len() {
[
BlockLength::Bytes512,
BlockLength::Bytes1024,
BlockLength::Bytes2048,
]
.into_iter()
.zip([512, 1024, 2048].into_iter().zip([9, 10, 11]))
.for_each(|(exp_block_len, (block_len, raw_block_len))| {
assert_eq!(BlockLength::from_raw(raw_block_len), Ok(exp_block_len));
assert_eq!(exp_block_len.into_raw(), raw_block_len);
assert_eq!(exp_block_len.block_len(), block_len);
});
}
}