1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 // Copyright by contributors to this project. 3 // SPDX-License-Identifier: (Apache-2.0 OR MIT) 4 5 use crate::Error; 6 7 use alloc::vec::Vec; 8 9 pub trait Writer { write(&mut self, bytes: &[u8]) -> Result<(), Error>10 fn write(&mut self, bytes: &[u8]) -> Result<(), Error>; 11 } 12 13 impl<T: Writer + ?Sized> Writer for &mut T { 14 #[inline] write(&mut self, bytes: &[u8]) -> Result<(), Error>15 fn write(&mut self, bytes: &[u8]) -> Result<(), Error> { 16 (**self).write(bytes) 17 } 18 } 19 20 impl Writer for &mut [u8] { write(&mut self, bytes: &[u8]) -> Result<(), Error>21 fn write(&mut self, bytes: &[u8]) -> Result<(), Error> { 22 if bytes.len() > self.len() { 23 return Err(Error::UnexpectedEOF); 24 } 25 26 let (a, b) = core::mem::take(self).split_at_mut(bytes.len()); 27 a.copy_from_slice(bytes); 28 *self = b; 29 30 Ok(()) 31 } 32 } 33 34 impl Writer for Vec<u8> { 35 #[inline] write(&mut self, bytes: &[u8]) -> Result<(), Error>36 fn write(&mut self, bytes: &[u8]) -> Result<(), Error> { 37 self.extend_from_slice(bytes); 38 Ok(()) 39 } 40 } 41