Skip to content

Commit

Permalink
Allow decode with custom DeserializeSeed
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Oct 2, 2022
1 parent 1dda2a5 commit 3505807
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/features/serde/de_borrowed.rs
Expand Up @@ -25,6 +25,24 @@ 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 Expand Up @@ -439,4 +457,4 @@ impl<'de, 'a, DE: BorrowDecoder<'de>> VariantAccess<'de> for SerdeDecoder<'a, 'd
{
Deserializer::deserialize_tuple(self, fields.len(), visitor)
}
}
}

0 comments on commit 3505807

Please sign in to comment.