Skip to content

Commit

Permalink
require &mut Context instead of owned Context
Browse files Browse the repository at this point in the history
  • Loading branch information
branchseer committed Dec 12, 2024
1 parent 5abc86c commit e74149d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
18 changes: 9 additions & 9 deletions src/de/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, C: Config, Context> {
pub struct DecoderImpl<'context, R, C: Config, Context> {
reader: R,
config: C,
bytes_read: usize,
context: Context,
context: &'context mut Context,
}

impl<R: Reader, C: Config, Context> DecoderImpl<R, C, Context> {
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<R, C, Context> {
pub fn new(reader: R, config: C, context: &'context mut Context) -> DecoderImpl<'context, R, C, Context> {
DecoderImpl {
reader,
config,
Expand All @@ -39,10 +39,10 @@ impl<R: Reader, C: Config, Context> DecoderImpl<R, C, Context> {
}
}

impl<R, C: Config, Context> Sealed for DecoderImpl<R, C, Context> {}
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<R, C, Context>
impl<'context, 'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de>
for DecoderImpl<'context, R, C, Context>
{
type BR = R;

Expand All @@ -51,7 +51,7 @@ impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de>
}
}

impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<R, C, Context> {
impl<'context, R: Reader, C: Config, Context> Decoder for DecoderImpl<'context, R, C, Context> {
type R = R;

type C = C;
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<R, C, Context> {
}

fn context(&mut self) -> &mut Self::Context {
&mut self.context
self.context
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/features/impl_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn decode_from_std_read<D: Decode<()>, C: Config, R: std::io::Read>(
src: &mut R,
config: C,
) -> Result<D, DecodeError> {
decode_from_std_read_with_context(src, config, ())
decode_from_std_read_with_context(src, config, &mut ())
}

#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
Expand All @@ -39,7 +39,7 @@ pub fn decode_from_std_read_with_context<
>(
src: &mut R,
config: C,
context: Context,
context: &mut Context,
) -> Result<D, DecodeError> {
let reader = IoReader::new(src);
let mut decoder = DecoderImpl::<_, C, Context>::new(reader, config, context);
Expand Down
9 changes: 6 additions & 3 deletions src/features/serde/de_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/features/serde/de_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -39,7 +40,8 @@ pub fn decode_from_std_read<D: DeserializeOwned, C: Config, R: std::io::Read>(
config: C,
) -> Result<D, DecodeError> {
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)
}
Expand All @@ -53,7 +55,8 @@ pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
reader: R,
config: C,
) -> Result<D, DecodeError> {
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)
}
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ pub fn decode_from_slice<D: de::Decode<()>, 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<Context, D: de::Decode<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);
Expand All @@ -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<
Expand All @@ -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);
Expand All @@ -202,7 +202,8 @@ pub fn decode_from_reader<D: de::Decode<()>, R: Reader, C: Config>(
reader: R,
config: C,
) -> Result<D, error::DecodeError> {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit e74149d

Please sign in to comment.