|
| 1 | +use binread::io::{Cursor, Error, ErrorKind, Read, Result}; |
| 2 | + |
| 3 | +#[derive(Debug)] |
| 4 | +struct MalfunctioningEddie<'data> { |
| 5 | + error: Option<Error>, |
| 6 | + data: Cursor<&'data [u8]>, |
| 7 | +} |
| 8 | + |
| 9 | +impl <'data> MalfunctioningEddie<'data> { |
| 10 | + fn new(data: &'data[u8]) -> Self { |
| 11 | + Self { |
| 12 | + error: None, |
| 13 | + data: Cursor::new(data), |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + fn nice_to_meet_you(&mut self) { |
| 18 | + self.error = Some(Error::new(ErrorKind::BrokenPipe, "")); |
| 19 | + } |
| 20 | + |
| 21 | + fn what_a_surprise(&mut self) { |
| 22 | + self.error = Some(Error::new(ErrorKind::Interrupted, "")); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl Read for MalfunctioningEddie<'_> { |
| 27 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize> { |
| 28 | + if let Some(error) = self.error.take() { |
| 29 | + Err(error) |
| 30 | + } else { |
| 31 | + self.data.read(buf) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[test] |
| 37 | +fn bytes() { |
| 38 | + let mut cursor = MalfunctioningEddie::new(b"\0\x01\x02\x03\x04\x05"); |
| 39 | + { |
| 40 | + let mut bytes = cursor.by_ref().bytes(); |
| 41 | + assert!(matches!(bytes.next(), Some(Ok(0)))); |
| 42 | + assert!(matches!(bytes.next(), Some(Ok(1)))); |
| 43 | + } |
| 44 | + |
| 45 | + // Interrupted error should cause a retry |
| 46 | + cursor.what_a_surprise(); |
| 47 | + { |
| 48 | + let mut bytes = cursor.by_ref().bytes(); |
| 49 | + assert!(matches!(bytes.next(), Some(Ok(2)))); |
| 50 | + } |
| 51 | + |
| 52 | + // Reads through Bytes should have advanced the underlying stream |
| 53 | + let mut raw_read_data = [0u8; 2]; |
| 54 | + assert_eq!(cursor.read(&mut raw_read_data).unwrap(), 2); |
| 55 | + assert_eq!(raw_read_data, [3, 4]); |
| 56 | + |
| 57 | + // Errors other than Interrupted should be returned |
| 58 | + cursor.nice_to_meet_you(); |
| 59 | + { |
| 60 | + let mut bytes = cursor.bytes(); |
| 61 | + assert_eq!(bytes.next().unwrap().unwrap_err().kind(), ErrorKind::BrokenPipe); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn read_exact() { |
| 67 | + let mut cursor = MalfunctioningEddie::new(b"\0\x01\x02\x03\x04\x05"); |
| 68 | + |
| 69 | + let mut raw_read_data = [0u8; 2]; |
| 70 | + cursor.read_exact(&mut raw_read_data).unwrap(); |
| 71 | + assert_eq!(raw_read_data, [0, 1]); |
| 72 | + |
| 73 | + // Interrupted error should cause a retry |
| 74 | + cursor.what_a_surprise(); |
| 75 | + cursor.read_exact(&mut raw_read_data).unwrap(); |
| 76 | + assert_eq!(raw_read_data, [2, 3]); |
| 77 | + |
| 78 | + // Errors other than Interrupted should be returned |
| 79 | + cursor.nice_to_meet_you(); |
| 80 | + assert_eq!(cursor.read_exact(&mut raw_read_data).unwrap_err().kind(), ErrorKind::BrokenPipe); |
| 81 | + |
| 82 | + // Read through a mutable reference should work as if it were directly on |
| 83 | + // the cursor |
| 84 | + cursor.by_ref().read_exact(&mut raw_read_data).unwrap(); |
| 85 | + assert_eq!(raw_read_data, [4, 5]); |
| 86 | + |
| 87 | + // EOF reads should not succeed |
| 88 | + assert_eq!(cursor.read_exact(&mut raw_read_data).unwrap_err().kind(), ErrorKind::UnexpectedEof); |
| 89 | +} |
0 commit comments