| 1 |
// We cannot use the below impls and std ones because we'll re-implement the |
| 2 |
// same trait fot &[u8] which is blanketed by write. Ending up with two separate implementations |
| 3 |
#![cfg(not(feature = "std"))] |
| 4 |
use crate::bytestream::{ZByteIoError, ZByteWriterTrait}; |
| 5 |
|
| 6 |
impl ZByteWriterTrait for &mut [u8] { |
| 7 |
fn write_bytes(&mut self, buf: &[u8]) -> Result<usize, ZByteIoError> { |
| 8 |
// got from the write of std |
| 9 |
let amt = core::cmp::min(buf.len(), self.len()); |
| 10 |
let (a, b) = core::mem::take(self).split_at_mut(amt); |
| 11 |
a.copy_from_slice(&buf[..amt]); |
| 12 |
*self = b; |
| 13 |
Ok(amt) |
| 14 |
} |
| 15 |
|
| 16 |
fn write_all_bytes(&mut self, buf: &[u8]) -> Result<(), ZByteIoError> { |
| 17 |
if buf.len() > self.len() { |
| 18 |
return Err(ZByteIoError::NotEnoughBuffer(self.len(), buf.len())); |
| 19 |
} |
| 20 |
let amt = core::cmp::min(buf.len(), self.len()); |
| 21 |
let (a, b) = core::mem::take(self).split_at_mut(amt); |
| 22 |
a.copy_from_slice(&buf[..amt]); |
| 23 |
*self = b; |
| 24 |
|
| 25 |
Ok(()) |
| 26 |
} |
| 27 |
|
| 28 |
fn write_const_bytes<const N: usize>(&mut self, buf: &[u8; N]) -> Result<(), ZByteIoError> { |
| 29 |
if N > self.len() { |
| 30 |
return Err(ZByteIoError::NotEnoughBuffer(self.len(), N)); |
| 31 |
} |
| 32 |
let amt = core::cmp::min(buf.len(), self.len()); |
| 33 |
let (a, b) = core::mem::take(self).split_at_mut(amt); |
| 34 |
a.copy_from_slice(&buf[..amt]); |
| 35 |
*self = b; |
| 36 |
Ok(()) |
| 37 |
} |
| 38 |
|
| 39 |
fn flush_bytes(&mut self) -> Result<(), ZByteIoError> { |
| 40 |
Ok(()) |
| 41 |
} |
| 42 |
fn reserve_capacity(&mut self, _: usize) -> Result<(), ZByteIoError> { |
| 43 |
// can't really pre-allocate anything here |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl ZByteWriterTrait for &mut alloc::vec::Vec<u8> { |
| 49 |
fn write_bytes(&mut self, buf: &[u8]) -> Result<usize, ZByteIoError> { |
| 50 |
self.extend_from_slice(buf); |
| 51 |
Ok(buf.len()) |
| 52 |
} |
| 53 |
|
| 54 |
fn write_all_bytes(&mut self, buf: &[u8]) -> Result<(), ZByteIoError> { |
| 55 |
self.extend_from_slice(buf); |
| 56 |
Ok(()) |
| 57 |
} |
| 58 |
|
| 59 |
fn write_const_bytes<const N: usize>(&mut self, buf: &[u8; N]) -> Result<(), ZByteIoError> { |
| 60 |
self.extend_from_slice(buf); |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
fn flush_bytes(&mut self) -> Result<(), ZByteIoError> { |
| 64 |
Ok(()) |
| 65 |
} |
| 66 |
fn reserve_capacity(&mut self, size: usize) -> Result<(), ZByteIoError> { |
| 67 |
self.reserve(size); |
| 68 |
Ok(()) |
| 69 |
} |
| 70 |
} |