Skip to main content

dotnet_io_binary/slice/
prim.rs

1use num_traits::FromBytes;
2
3pub fn split_prim<const N: usize, T: FromBytes<Bytes = [u8; N]>>(buf: &[u8]) -> Option<(T, &[u8])> {
4    let (bytes, buf1) = buf.split_first_chunk()?;
5    Some((FromBytes::from_le_bytes(bytes), buf1))
6}
7
8#[cfg(test)]
9mod tests {
10    use crate::slice::prim::split_prim;
11
12    #[test]
13    fn test_split_prim() {
14        let source = [0x01u8, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
15        let (int, tail): (u32, _) = split_prim(&source).unwrap();
16        assert_eq!(int, 0x67452301);
17        assert_eq!(tail, &[0x89, 0xab, 0xcd, 0xef]);
18    }
19}