Skip to content

Commit 1290a05

Browse files
committed
No more forced references
1 parent ed457f4 commit 1290a05

File tree

6 files changed

+30
-51
lines changed

6 files changed

+30
-51
lines changed

src/de/decoder.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ use crate::{config::Config, error::DecodeError, utils::Sealed};
2121
/// // this u32 can be any Decode
2222
/// let value = u32::decode(&mut decoder).unwrap();
2323
/// ```
24-
pub struct DecoderImpl<'context, R, C: Config, Context> {
24+
pub struct DecoderImpl<R, C: Config, Context> {
2525
reader: R,
2626
config: C,
2727
bytes_read: usize,
28-
context: &'context mut Context,
28+
context: Context,
2929
}
3030

31-
impl<'context, R: Reader, C: Config, Context> DecoderImpl<'context, R, C, Context> {
31+
impl<R: Reader, C: Config, Context> DecoderImpl<R, C, Context> {
3232
/// Construct a new Decoder
33-
pub fn new(
34-
reader: R,
35-
config: C,
36-
context: &'context mut Context,
37-
) -> DecoderImpl<'context, R, C, Context> {
33+
pub fn new(reader: R, config: C, context: Context) -> DecoderImpl<R, C, Context> {
3834
DecoderImpl {
3935
reader,
4036
config,
@@ -44,10 +40,10 @@ impl<'context, R: Reader, C: Config, Context> DecoderImpl<'context, R, C, Contex
4440
}
4541
}
4642

47-
impl<R, C: Config, Context> Sealed for DecoderImpl<'_, R, C, Context> {}
43+
impl<R, C: Config, Context> Sealed for DecoderImpl<R, C, Context> {}
4844

4945
impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de>
50-
for DecoderImpl<'_, R, C, Context>
46+
for DecoderImpl<R, C, Context>
5147
{
5248
type BR = R;
5349

@@ -56,7 +52,7 @@ impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de>
5652
}
5753
}
5854

59-
impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<'_, R, C, Context> {
55+
impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<R, C, Context> {
6056
type R = R;
6157

6258
type C = C;
@@ -99,7 +95,7 @@ impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<'_, R, C, Context> {
9995
}
10096

10197
fn context(&mut self) -> &mut Self::Context {
102-
self.context
98+
&mut self.context
10399
}
104100
}
105101

src/features/impl_std.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn decode_from_std_read<D: Decode<()>, C: Config, R: std::io::Read>(
2727
src: &mut R,
2828
config: C,
2929
) -> Result<D, DecodeError> {
30-
decode_from_std_read_with_context(src, config, &mut ())
30+
decode_from_std_read_with_context(src, config, ())
3131
}
3232

3333
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
@@ -39,7 +39,7 @@ pub fn decode_from_std_read_with_context<
3939
>(
4040
src: &mut R,
4141
config: C,
42-
context: &mut Context,
42+
context: Context,
4343
) -> Result<D, DecodeError> {
4444
let reader = IoReader::new(src);
4545
let mut decoder = DecoderImpl::<_, C, Context>::new(reader, config, context);

src/features/serde/de_borrowed.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ impl<'de, DE: BorrowDecoder<'de>> BorrowedSerdeDecoder<'de, DE> {
2525
}
2626
}
2727

28-
impl<'de, 'context, C: Config, Context>
29-
BorrowedSerdeDecoder<'de, DecoderImpl<'context, SliceReader<'de>, C, Context>>
30-
{
28+
impl<'de, C: Config, Context> BorrowedSerdeDecoder<'de, DecoderImpl<SliceReader<'de>, C, Context>> {
3129
/// Creates the decoder from a borrowed slice.
3230
pub fn from_slice(
3331
slice: &'de [u8],
3432
config: C,
35-
context: &'context mut Context,
36-
) -> BorrowedSerdeDecoder<'de, DecoderImpl<'context, SliceReader<'de>, C, Context>>
33+
context: Context,
34+
) -> BorrowedSerdeDecoder<'de, DecoderImpl<SliceReader<'de>, C, Context>>
3735
where
3836
C: Config,
3937
{
@@ -57,13 +55,8 @@ where
5755
D: Deserialize<'de>,
5856
C: Config,
5957
{
60-
let mut context = ();
6158
let mut serde_decoder =
62-
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C, ()>>::from_slice(
63-
slice,
64-
config,
65-
&mut context,
66-
);
59+
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C, ()>>::from_slice(slice, config, ());
6760
let result = D::deserialize(serde_decoder.as_deserializer())?;
6861
let bytes_read = slice.len() - serde_decoder.de.borrow_reader().slice.len();
6962
Ok((result, bytes_read))
@@ -79,13 +72,8 @@ where
7972
D: DeserializeSeed<'de>,
8073
C: Config,
8174
{
82-
let mut context = ();
8375
let mut serde_decoder =
84-
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C, ()>>::from_slice(
85-
slice,
86-
config,
87-
&mut context,
88-
);
76+
BorrowedSerdeDecoder::<DecoderImpl<SliceReader<'de>, C, ()>>::from_slice(slice, config, ());
8977
let result = seed.deserialize(serde_decoder.as_deserializer())?;
9078
let bytes_read = slice.len() - serde_decoder.de.borrow_reader().slice.len();
9179
Ok((result, bytes_read))

src/features/serde/de_owned.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ impl<DE: Decoder> OwnedSerdeDecoder<DE> {
2424
}
2525

2626
#[cfg(feature = "std")]
27-
impl<'r, 'context, C: Config, R: std::io::Read, Context>
28-
OwnedSerdeDecoder<DecoderImpl<'context, IoReader<&'r mut R>, C, Context>>
27+
impl<'r, C: Config, R: std::io::Read, Context>
28+
OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, Context>>
2929
{
3030
/// Creates the decoder from an `std::io::Read` implementor.
3131
pub fn from_std_read(
3232
src: &'r mut R,
3333
config: C,
34-
context: &'context mut Context,
35-
) -> OwnedSerdeDecoder<DecoderImpl<'context, IoReader<&'r mut R>, C, Context>>
34+
context: Context,
35+
) -> OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, Context>>
3636
where
3737
C: Config,
3838
{
@@ -42,15 +42,13 @@ impl<'r, 'context, C: Config, R: std::io::Read, Context>
4242
}
4343
}
4444

45-
impl<'context, C: Config, R: Reader, Context>
46-
OwnedSerdeDecoder<DecoderImpl<'context, R, C, Context>>
47-
{
45+
impl<C: Config, R: Reader, Context> OwnedSerdeDecoder<DecoderImpl<R, C, Context>> {
4846
/// Creates the decoder from a [`Reader`] implementor.
4947
pub fn from_reader(
5048
reader: R,
5149
config: C,
52-
context: &'context mut Context,
53-
) -> OwnedSerdeDecoder<DecoderImpl<'context, R, C, Context>>
50+
context: Context,
51+
) -> OwnedSerdeDecoder<DecoderImpl<R, C, Context>>
5452
where
5553
C: Config,
5654
{
@@ -86,12 +84,11 @@ pub fn decode_from_std_read<'r, D: DeserializeOwned, C: Config, R: std::io::Read
8684
src: &'r mut R,
8785
config: C,
8886
) -> Result<D, DecodeError> {
89-
let mut context = ();
9087
let mut serde_decoder =
9188
OwnedSerdeDecoder::<DecoderImpl<IoReader<&'r mut R>, C, ()>>::from_std_read(
9289
src,
9390
config,
94-
&mut context,
91+
(),
9592
);
9693
D::deserialize(serde_decoder.as_deserializer())
9794
}
@@ -105,9 +102,8 @@ pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
105102
reader: R,
106103
config: C,
107104
) -> Result<D, DecodeError> {
108-
let mut context = ();
109105
let mut serde_decoder =
110-
OwnedSerdeDecoder::<DecoderImpl<R, C, ()>>::from_reader(reader, config, &mut context);
106+
OwnedSerdeDecoder::<DecoderImpl<R, C, ()>>::from_reader(reader, config, ());
111107
D::deserialize(serde_decoder.as_deserializer())
112108
}
113109

src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ pub fn decode_from_slice<D: de::Decode<()>, C: Config>(
149149
src: &[u8],
150150
config: C,
151151
) -> Result<(D, usize), error::DecodeError> {
152-
decode_from_slice_with_context(src, config, &mut ())
152+
decode_from_slice_with_context(src, config, ())
153153
}
154154

155155
pub fn decode_from_slice_with_context<Context, D: de::Decode<Context>, C: Config>(
156156
src: &[u8],
157157
config: C,
158-
context: &mut Context,
158+
context: Context,
159159
) -> Result<(D, usize), error::DecodeError> {
160160
let reader = de::read::SliceReader::new(src);
161161
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>(
173173
src: &'a [u8],
174174
config: C,
175175
) -> Result<(D, usize), error::DecodeError> {
176-
borrow_decode_from_slice_with_context(src, config, &mut ())
176+
borrow_decode_from_slice_with_context(src, config, ())
177177
}
178178

179179
pub fn borrow_decode_from_slice_with_context<
@@ -184,7 +184,7 @@ pub fn borrow_decode_from_slice_with_context<
184184
>(
185185
src: &'a [u8],
186186
config: C,
187-
context: &mut Context,
187+
context: Context,
188188
) -> Result<(D, usize), error::DecodeError> {
189189
let reader = de::read::SliceReader::new(src);
190190
let mut decoder = de::DecoderImpl::<_, C, Context>::new(reader, config, context);
@@ -202,8 +202,7 @@ pub fn decode_from_reader<D: de::Decode<()>, R: Reader, C: Config>(
202202
reader: R,
203203
config: C,
204204
) -> Result<D, error::DecodeError> {
205-
let mut context = ();
206-
let mut decoder = de::DecoderImpl::<_, C, ()>::new(reader, config, &mut context);
205+
let mut decoder = de::DecoderImpl::<_, C, ()>::new(reader, config, ());
207206
D::decode(&mut decoder)
208207
}
209208

tests/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn decode_with_context() {
9696

9797
let bytes = encode_to_vec(&container, config).unwrap();
9898
let (decoded_container, _) =
99-
decode_from_slice_with_context::<_, Container, _>(&bytes, config, &mut &bump).unwrap();
99+
decode_from_slice_with_context::<_, Container, _>(&bytes, config, &bump).unwrap();
100100

101101
assert_eq!(container, decoded_container);
102102

0 commit comments

Comments
 (0)