Fixed the slice code, too
Getty Ritter
8 years ago
8 | 8 | |
9 | 9 | #[test] |
10 | 10 | fn reader_tests() { |
11 | assert!(ByteReader::from_vec(vec![0x00]).read_twip() == Ok(-32.0)); | |
12 | assert!(ByteReader::from_vec(vec![0x00]).read_prefix_int() == Ok(0x00)); | |
13 | assert!(ByteReader::from_vec(vec![0x7f]).read_prefix_int() == Ok(0x7f)); | |
14 | assert!(ByteReader::from_vec(vec![0x80,0xff]).read_prefix_int() == Ok(0xff)); | |
11 | assert!(ByteReader::from_slice(&[0x00]).read_twip() == Ok(-32.0)); | |
12 | assert!(ByteReader::from_slice(&[0x00]).read_prefix_int() == Ok(0x00)); | |
13 | assert!(ByteReader::from_slice(&[0x7f]).read_prefix_int() == Ok(0x7f)); | |
14 | assert!(ByteReader::from_slice(&[0x80,0xff]).read_prefix_int() == Ok(0xff)); | |
15 | 15 | } |
16 | 16 | |
17 | 17 | const MK_OK: &'static Fn(io::Result<u8>) -> Option<u8> = &|s| s.ok(); |
44 | 44 | } |
45 | 45 | } |
46 | 46 | |
47 | impl<'a> ByteReader<slice::Iter<'a, u8>> { | |
47 | const DEREF: &'static Fn(&u8) -> u8 = &|s| *s; | |
48 | ||
49 | impl<'a> ByteReader<iter::Map<slice::Iter<'a, u8>, &'static Fn(&u8) -> u8>> { | |
50 | /// Create a reader from a borrowed slice, with a copy on each access | |
48 | 51 | pub fn from_slice(lst: &'a [u8]) -> Self { |
49 |
ByteReader { bytes: lst.iter() |
|
52 | ByteReader { bytes: lst.iter().map(DEREF) } | |
50 | 53 | } |
51 | 54 | } |
52 | 55 |