Skip to content

Commit

Permalink
Add Read from mutable slices
Browse files Browse the repository at this point in the history
Signed-off-by: kaidokert <[email protected]>
  • Loading branch information
kaidokert committed Dec 30, 2024
1 parent 19c20be commit 9fc9581
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ciborium-io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ impl Write for &mut [u8] {
}
}

#[cfg(not(feature = "std"))]
impl Read for &mut [u8] {
type Error = EndOfFile;

#[inline]
fn read_exact(&mut self, data: &mut [u8]) -> Result<(), Self::Error> {
let mut immutable : &[u8] = self;
immutable.read_exact(data)?;
*self = &mut core::mem::take(self)[data.len()..];
Ok(())
}
}

#[cfg(all(not(feature = "std"), feature = "alloc"))]
impl Write for alloc::vec::Vec<u8> {
type Error = core::convert::Infallible;
Expand Down Expand Up @@ -205,6 +218,20 @@ mod test {
reader.read_exact(&mut buffer[..]).unwrap_err();
}

#[test]
fn read_mut_two() {
let mut reader = &mut [1u8; 2][..];
let mut buffer = [0u8; 1];

reader.read_exact(&mut buffer[..]).unwrap();
assert_eq!(buffer[0], 1);

reader.read_exact(&mut buffer[..]).unwrap();
assert_eq!(buffer[0], 1);

reader.read_exact(&mut buffer[..]).unwrap_err();
}

#[test]
#[cfg(feature = "std")]
fn read_std() {
Expand Down

0 comments on commit 9fc9581

Please sign in to comment.