1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
pub fn int(buf: &[u8]) -> i64 { let len = buf.len(); assert!(len <= 8); let mut res: i64 = 0; for (i, b) in buf.iter().enumerate() { res |= (*b as i64) << (8 * (len - i - 1)) } res } #[cfg(test)] mod tests { use super::*; #[test] fn test_int() { for i in 0..=8 { assert_eq!(int(&*vec![0u8; i]), 0); } } #[test] #[should_panic(expected = "assertion failed: len <= 8")] fn test_int_panic() { assert_eq!(int(&*vec![0u8; 9]), 0); } }