Skip to content

Commit

Permalink
Add implementation of Decode for Box<str> and Box<[T]>
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Feb 5, 2024
1 parent 805816a commit 5460a6d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,24 @@ impl<T> WrapperTypeDecode for Box<T> {
}
}

impl<T: Decode> Decode for Box<[T]> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Vec::decode(input)?.into_boxed_slice())
}
}

impl Decode for Box<str> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
// Guaranteed to create a Vec with capacity == len
let vec = Vec::from(Box::<[u8]>::decode(input)?);
// Guaranteed not to reallocate the vec, only transmute to String
let str = String::from_utf8(vec).map_err(|_| "Invalid utf8 sequence")?;

assert_eq!(str.capacity(), str.len());
Ok(str.into_boxed_str())
}
}

impl<T> WrapperTypeDecode for Rc<T> {
type Wrapped = T;

Expand Down Expand Up @@ -1612,6 +1630,28 @@ mod tests {
assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
}

#[test]
fn boxed_str_works() {
let s = "Hello world".to_owned();
let b = s.clone().into_boxed_str();

let encoded = b.encode();
assert_eq!(s.encode(), encoded);

assert_eq!(*b, *Box::<str>::decode(&mut &encoded[..]).unwrap());
}

#[test]
fn boxed_slice_works() {
let v = vec![1u32, 2, 3, 4, 5, 6];
let b = v.clone().into_boxed_slice();

let encoded = b.encode();
assert_eq!(v.encode(), encoded);

assert_eq!(*b, *Box::<[u32]>::decode(&mut &b.encode()[..]).unwrap());
}

#[test]
fn cow_works() {
let x = &[1u32, 2, 3, 4, 5, 6][..];
Expand Down

0 comments on commit 5460a6d

Please sign in to comment.