sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::lib_enum;
use crate::result::{Error, Result};

lib_enum! {
    /// Represents the maximum read/write data block length.
    BlockLength: u8 {
        default: Bytes512,
        error: Error,
        /// Maximum read block length: 512 bytes.
        Bytes512 = 9,
        /// Maximum read block length: 1024 bytes.
        Bytes1024 = 10,
        /// Maximum read block length: 2048 bytes.
        Bytes2048 = 11,
    }
}

impl BlockLength {
    /// Gets the block length in bytes for the [BlockLength].
    #[inline]
    pub const fn block_len(self) -> usize {
        1usize << (self as u8)
    }

    /// Attempts to convert an inner representation into a [BlockLength].
    pub const fn try_from_inner(val: u8) -> Result<Self> {
        Self::from_raw(val)
    }

    /// Converts a [BlockLength] into an inner representation.
    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);
        });
    }
}