mirror of https://git.sr.ht/~stygianentity/bincode
SliceBox: allow Deref into &[T]
Allows more convenient use of SliceBox where one doesn't care whether it represents a Vec<T> or &[T] and can handle using a &[T].
This commit is contained in:
parent
9ad558de5f
commit
9ab3320db7
|
|
@ -322,3 +322,14 @@ impl <'a, T> Deref for RefBox<'a, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <'a, T> Deref for SliceBox<'a, T> {
|
||||||
|
type Target = [T];
|
||||||
|
|
||||||
|
fn deref(&self) -> &[T] {
|
||||||
|
match &self.inner {
|
||||||
|
&RefBoxInner::Ref(ref t) => t,
|
||||||
|
&RefBoxInner::Box(ref b) => b.deref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -311,6 +311,10 @@ fn test_slicebox() {
|
||||||
let slice = [1u32, 2, 3 ,4, 5];
|
let slice = [1u32, 2, 3 ,4, 5];
|
||||||
let encoded = encode(&SliceBox::new(&slice), Infinite).unwrap();
|
let encoded = encode(&SliceBox::new(&slice), Infinite).unwrap();
|
||||||
let decoded: SliceBox<'static, u32> = decode(&encoded[..]).unwrap();
|
let decoded: SliceBox<'static, u32> = decode(&encoded[..]).unwrap();
|
||||||
|
{
|
||||||
|
let sb: &[u32] = &decoded;
|
||||||
|
assert!(slice == sb);
|
||||||
|
}
|
||||||
let vecx: Vec<u32> = decoded.take();
|
let vecx: Vec<u32> = decoded.take();
|
||||||
assert!(slice == &vecx[..]);
|
assert!(slice == &vecx[..]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue