Skip to content

Commit

Permalink
Allow decode with custom DeserializeSeed (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Oct 2, 2022
1 parent 1dda2a5 commit bdbc051
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/features/serde/de_borrowed.rs
Expand Up @@ -25,6 +25,28 @@ where
T::deserialize(serde_decoder)
}

/// Decode a borrowed type from the given slice using a seed. Some parts of the decoded type are expected to be referring to the given slice
pub fn decode_seed_from_slice<'de, T, C>(
seed: T,
slice: &'de [u8],
config: C,
) -> Result<T::Value, DecodeError>
where
T: DeserializeSeed<'de>,
C: Config,
{
if C::SKIP_FIXED_ARRAY_LENGTH {
return Err(SerdeDecodeError::SkipFixedArrayLengthNotSupported.into());
}
let reader = crate::de::read::SliceReader::new(slice);
let mut decoder = crate::de::DecoderImpl::new(reader, config);
let serde_decoder = SerdeDecoder {
de: &mut decoder,
pd: PhantomData,
};
seed.deserialize(serde_decoder)
}

pub(super) struct SerdeDecoder<'a, 'de, DE: BorrowDecoder<'de>> {
pub(super) de: &'a mut DE,
pub(super) pd: PhantomData<&'de ()>,
Expand Down

0 comments on commit bdbc051

Please sign in to comment.