|
| 1 | +use crate::{ |
| 2 | + len_type::LenType, |
| 3 | + vec::{VecInner, VecStorage}, |
| 4 | + CapacityError, |
| 5 | +}; |
| 6 | +use embedded_io::{Write, Error, ErrorType, ErrorKind}; |
| 7 | + |
| 8 | +impl Error for CapacityError { |
| 9 | + fn kind(&self) -> ErrorKind { |
| 10 | + ErrorKind::OutOfMemory |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +impl<LenT: LenType, S: VecStorage<u8> + ?Sized> ErrorType for VecInner<u8, LenT, S> { |
| 15 | + type Error = CapacityError; |
| 16 | +} |
| 17 | + |
| 18 | +impl<LenT: LenType, S: VecStorage<u8> + ?Sized> Write for VecInner<u8, LenT, S> { |
| 19 | + #[inline] |
| 20 | + fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 21 | + self.extend_from_slice(buf)?; |
| 22 | + Ok(buf.len()) |
| 23 | + } |
| 24 | + |
| 25 | + #[inline] |
| 26 | + fn flush(&mut self) -> Result<(), Self::Error> { |
| 27 | + Ok(()) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[cfg(test)] |
| 32 | +mod tests { |
| 33 | + use crate::Vec; |
| 34 | + use embedded_io::{Write, Error, ErrorKind}; |
| 35 | + |
| 36 | + fn write(w: &mut impl Write, data: &[u8]) -> Result<(), ErrorKind> { |
| 37 | + w.write_all(data).map_err(|e| e.kind()) |
| 38 | + } |
| 39 | + |
| 40 | + #[test] |
| 41 | + fn test_write() { |
| 42 | + let mut v: Vec<u8, 4> = Vec::new(); |
| 43 | + assert_eq!(v.len(), 0); |
| 44 | + write(&mut v, &[1, 2]).unwrap(); |
| 45 | + assert_eq!(v.len(), 2); |
| 46 | + assert_eq!(v.as_slice(), &[1, 2]); |
| 47 | + write(&mut v, &[3]).unwrap(); |
| 48 | + assert_eq!(v.len(), 3); |
| 49 | + assert_eq!(v.as_slice(), &[1, 2, 3]); |
| 50 | + assert!(write(&mut v, &[4, 5]).is_err()); |
| 51 | + assert_eq!(v.len(), 3); |
| 52 | + assert_eq!(v.as_slice(), &[1, 2, 3]); |
| 53 | + } |
| 54 | +} |
0 commit comments