diff --git a/src/de/decoder.rs b/src/de/decoder.rs index 63983d3e..80da88dc 100644 --- a/src/de/decoder.rs +++ b/src/de/decoder.rs @@ -20,16 +20,16 @@ use crate::{config::Config, error::DecodeError, utils::Sealed}; /// // this u32 can be any Decode /// let value = u32::decode(&mut decoder).unwrap(); /// ``` -pub struct DecoderImpl { +pub struct DecoderImpl<'context, R, C: Config, Context> { reader: R, config: C, bytes_read: usize, - context: Context, + context: &'context mut Context, } -impl DecoderImpl { +impl<'context, R: Reader, C: Config, Context> DecoderImpl<'context, R, C, Context> { /// Construct a new Decoder - pub const fn new(reader: R, config: C, context: Context) -> DecoderImpl { + pub fn new(reader: R, config: C, context: &'context mut Context) -> DecoderImpl<'context, R, C, Context> { DecoderImpl { reader, config, @@ -39,10 +39,10 @@ impl DecoderImpl { } } -impl Sealed for DecoderImpl {} +impl<'context, R, C: Config, Context> Sealed for DecoderImpl<'context, R, C, Context> {} -impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de> - for DecoderImpl +impl<'context, 'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de> + for DecoderImpl<'context, R, C, Context> { type BR = R; @@ -51,7 +51,7 @@ impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de> } } -impl Decoder for DecoderImpl { +impl<'context, R: Reader, C: Config, Context> Decoder for DecoderImpl<'context, R, C, Context> { type R = R; type C = C; @@ -94,7 +94,7 @@ impl Decoder for DecoderImpl { } fn context(&mut self) -> &mut Self::Context { - &mut self.context + self.context } } diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index db927a89..7cf74e72 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -27,7 +27,7 @@ pub fn decode_from_std_read, C: Config, R: std::io::Read>( src: &mut R, config: C, ) -> Result { - decode_from_std_read_with_context(src, config, ()) + decode_from_std_read_with_context(src, config, &mut ()) } #[cfg_attr(docsrs, doc(cfg(feature = "std")))] @@ -39,7 +39,7 @@ pub fn decode_from_std_read_with_context< >( src: &mut R, config: C, - context: Context, + context: &mut Context, ) -> Result { let reader = IoReader::new(src); let mut decoder = DecoderImpl::<_, C, Context>::new(reader, config, context); diff --git a/src/features/serde/de_borrowed.rs b/src/features/serde/de_borrowed.rs index 4e98f8f4..ee0815eb 100644 --- a/src/features/serde/de_borrowed.rs +++ b/src/features/serde/de_borrowed.rs @@ -19,7 +19,8 @@ where C: Config, { let reader = crate::de::read::SliceReader::new(slice); - let mut decoder = crate::de::DecoderImpl::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder, pd: PhantomData, @@ -37,7 +38,8 @@ where C: Config, { let reader = crate::de::read::SliceReader::new(slice); - let mut decoder = crate::de::DecoderImpl::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder, pd: PhantomData, @@ -56,7 +58,8 @@ where C: Config, { let reader = crate::de::read::SliceReader::new(slice); - let mut decoder = crate::de::DecoderImpl::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder, pd: PhantomData, diff --git a/src/features/serde/de_owned.rs b/src/features/serde/de_owned.rs index 9b25e5c9..292efb3c 100644 --- a/src/features/serde/de_owned.rs +++ b/src/features/serde/de_owned.rs @@ -20,7 +20,8 @@ where C: Config, { let reader = crate::de::read::SliceReader::new(slice); - let mut decoder = crate::de::DecoderImpl::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder }; let result = D::deserialize(serde_decoder)?; let bytes_read = slice.len() - decoder.reader().slice.len(); @@ -39,7 +40,8 @@ pub fn decode_from_std_read( config: C, ) -> Result { let reader = crate::IoReader::new(src); - let mut decoder = crate::de::DecoderImpl::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder }; D::deserialize(serde_decoder) } @@ -53,7 +55,8 @@ pub fn decode_from_reader( reader: R, config: C, ) -> Result { - let mut decoder = crate::de::DecoderImpl::<_, C, ()>::new(reader, config, ()); + let mut context = (); + let mut decoder = crate::de::DecoderImpl::<_, C, ()>::new(reader, config, &mut context); let serde_decoder = SerdeDecoder { de: &mut decoder }; D::deserialize(serde_decoder) } diff --git a/src/lib.rs b/src/lib.rs index 695ee0dd..89974a4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,13 +149,13 @@ pub fn decode_from_slice, C: Config>( src: &[u8], config: C, ) -> Result<(D, usize), error::DecodeError> { - decode_from_slice_with_context(src, config, ()) + decode_from_slice_with_context(src, config, &mut ()) } pub fn decode_from_slice_with_context, C: Config>( src: &[u8], config: C, - context: Context, + context: &mut Context, ) -> Result<(D, usize), error::DecodeError> { let reader = de::read::SliceReader::new(src); let mut decoder = de::DecoderImpl::<_, C, Context>::new(reader, config, context); @@ -173,7 +173,7 @@ pub fn borrow_decode_from_slice<'a, D: de::BorrowDecode<'a, ()>, C: Config>( src: &'a [u8], config: C, ) -> Result<(D, usize), error::DecodeError> { - borrow_decode_from_slice_with_context(src, config, ()) + borrow_decode_from_slice_with_context(src, config, &mut ()) } pub fn borrow_decode_from_slice_with_context< @@ -184,7 +184,7 @@ pub fn borrow_decode_from_slice_with_context< >( src: &'a [u8], config: C, - context: Context, + context: &mut Context, ) -> Result<(D, usize), error::DecodeError> { let reader = de::read::SliceReader::new(src); let mut decoder = de::DecoderImpl::<_, C, Context>::new(reader, config, context); @@ -202,7 +202,8 @@ pub fn decode_from_reader, R: Reader, C: Config>( reader: R, config: C, ) -> Result { - let mut decoder = de::DecoderImpl::<_, C, ()>::new(reader, config, ()); + let mut context = (); + let mut decoder = de::DecoderImpl::<_, C, ()>::new(reader, config, &mut context); D::decode(&mut decoder) } diff --git a/tests/context.rs b/tests/context.rs index 68ec25cc..145453f2 100644 --- a/tests/context.rs +++ b/tests/context.rs @@ -96,7 +96,7 @@ fn decode_with_context() { let bytes = encode_to_vec(&container, config).unwrap(); let (decoded_container, _) = - decode_from_slice_with_context::<_, Container, _>(&bytes, config, &bump).unwrap(); + decode_from_slice_with_context::<_, Container, _>(&bytes, config, &mut &bump).unwrap(); assert_eq!(container, decoded_container);