| 1 |
#![cfg(all(feature = "alloc", feature = "derive"))] |
| 2 |
|
| 3 |
use bincode::{ |
| 4 |
config, de::BorrowDecoder, decode_from_slice, decode_from_slice_with_context, encode_to_vec, |
| 5 |
error::DecodeError, BorrowDecode, Decode, Encode, |
| 6 |
}; |
| 7 |
use bumpalo::{collections::Vec, vec, Bump}; |
| 8 |
|
| 9 |
#[derive(PartialEq, Eq, Debug)] |
| 10 |
struct CodableVec<'bump, T: 'bump>(Vec<'bump, T>); |
| 11 |
|
| 12 |
impl<'bump, T: Encode> Encode for CodableVec<'bump, T> { |
| 13 |
fn encode<E: bincode::enc::Encoder>( |
| 14 |
&self, |
| 15 |
encoder: &mut E, |
| 16 |
) -> Result<(), bincode::error::EncodeError> { |
| 17 |
self.0.as_slice().encode(encoder) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
impl<'bump, T: Decode<&'bump Bump>> Decode<&'bump Bump> for CodableVec<'bump, T> { |
| 22 |
fn decode<D: bincode::de::Decoder<Context = &'bump Bump>>( |
| 23 |
decoder: &mut D, |
| 24 |
) -> Result<Self, bincode::error::DecodeError> { |
| 25 |
let len = u64::decode(decoder)?; |
| 26 |
let len = usize::try_from(len).map_err(|_| DecodeError::OutsideUsizeRange(len))?; |
| 27 |
decoder.claim_container_read::<T>(len)?; |
| 28 |
let mut vec = Vec::with_capacity_in(len, decoder.context()); |
| 29 |
for _ in 0..len { |
| 30 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 31 |
vec.push(T::decode(decoder)?); |
| 32 |
} |
| 33 |
Ok(Self(vec)) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
impl<'de, 'bump, T: BorrowDecode<'de, &'bump Bump>> BorrowDecode<'de, &'bump Bump> |
| 38 |
for CodableVec<'bump, T> |
| 39 |
{ |
| 40 |
fn borrow_decode<D: BorrowDecoder<'de, Context = &'bump Bump>>( |
| 41 |
decoder: &mut D, |
| 42 |
) -> Result<Self, DecodeError> { |
| 43 |
let len = u64::decode(decoder)?; |
| 44 |
let len = usize::try_from(len).map_err(|_| DecodeError::OutsideUsizeRange(len))?; |
| 45 |
|
| 46 |
decoder.claim_container_read::<T>(len)?; |
| 47 |
|
| 48 |
let mut vec = Vec::with_capacity_in(len, decoder.context()); |
| 49 |
for _ in 0..len { |
| 50 |
// See the documentation on `unclaim_bytes_read` as to why we're doing this here |
| 51 |
decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
| 52 |
|
| 53 |
vec.push(T::borrow_decode(decoder)?); |
| 54 |
} |
| 55 |
Ok(Self(vec)) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[derive(Encode, Decode, PartialEq, Eq, Debug)] |
| 60 |
#[bincode(decode_context = "&'bump Bump")] |
| 61 |
struct Container<'bump> { |
| 62 |
vec: CodableVec<'bump, u32>, |
| 63 |
} |
| 64 |
|
| 65 |
#[derive(Encode, Decode, PartialEq, Eq, Debug)] |
| 66 |
#[bincode(decode_context = "&'bump Bump")] |
| 67 |
enum _EnumContainer<'bump> { |
| 68 |
Vec(CodableVec<'bump, u32>), |
| 69 |
} |
| 70 |
|
| 71 |
#[ouroboros::self_referencing] |
| 72 |
struct SelfReferencing { |
| 73 |
bump: Bump, |
| 74 |
#[borrows(bump)] |
| 75 |
#[not_covariant] |
| 76 |
container: Container<'this>, |
| 77 |
} |
| 78 |
|
| 79 |
impl<Context> Decode<Context> for SelfReferencing { |
| 80 |
fn decode<D: bincode::de::Decoder<Context = Context>>( |
| 81 |
decoder: &mut D, |
| 82 |
) -> Result<Self, DecodeError> { |
| 83 |
SelfReferencing::try_new(Bump::new(), |bump| { |
| 84 |
Container::decode(&mut decoder.with_context(bump)) |
| 85 |
}) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn decode_with_context() { |
| 91 |
let config = config::standard(); |
| 92 |
let bump = Bump::new(); |
| 93 |
let container = Container { |
| 94 |
vec: CodableVec(vec![in ≎ 1, 2, 3]), |
| 95 |
}; |
| 96 |
|
| 97 |
let bytes = encode_to_vec(&container, config).unwrap(); |
| 98 |
let (decoded_container, _) = |
| 99 |
decode_from_slice_with_context::<_, Container, _>(&bytes, config, &bump).unwrap(); |
| 100 |
|
| 101 |
assert_eq!(container, decoded_container); |
| 102 |
|
| 103 |
let self_referencing: SelfReferencing = decode_from_slice(&bytes, config).unwrap().0; |
| 104 |
self_referencing.with_container(|c| assert_eq!(&container, c)) |
| 105 |
} |