foxmark3 0.1.0

Send/receive Proxmark 3 commands
Documentation
use contracts::requires;

pub trait BytesExt {
    fn to_buffer<const N: usize>(&self) -> [u8; N];
}

pub trait SliceExt<T> {
    fn take_array<const N: usize>(&self) -> &[T; N];
    #[allow(unused)]
    fn take_array_mut<const N: usize>(&mut self) -> &mut [T; N];
}

impl<T> SliceExt<T> for [T] {
    fn take_array<const N: usize>(&self) -> &[T; N] {
        // SAFETY:
        //
        // if the slice succeeds, the slice must be length N, and the slice can be converted to an
        // array of length N without any additional checks
        unsafe { self[..N].try_into().unwrap_unchecked() }
    }

    fn take_array_mut<const N: usize>(&mut self) -> &mut [T; N] {
        // SAFETY:
        //
        // if the slice succeeds, the slice must be length N, and the slice can be converted to an
        // array of length N without any additional checks
        unsafe { (&mut self[..N]).try_into().unwrap_unchecked() }
    }
}

impl BytesExt for [u8] {
    #[requires(self.len() <= N, "slice exceeds buffer size")]
    fn to_buffer<const N: usize>(&self) -> [u8; N] {
        let mut buf = [0; N];
        buf[..self.len()].copy_from_slice(self);

        buf
    }
}