Skip to content

Commit a46894e

Browse files
committed
remove remaining 'context lifetimes
1 parent 1290a05 commit a46894e

File tree

6 files changed

+38
-34
lines changed

6 files changed

+38
-34
lines changed

src/de/decoder.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,22 @@ impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<R, C, Context> {
9999
}
100100
}
101101

102-
pub struct WithContext<'a, 'context, D: ?Sized, C> {
102+
pub struct WithContext<'a, D: ?Sized, C> {
103103
pub(crate) decoder: &'a mut D,
104-
pub(crate) context: &'context mut C,
104+
pub(crate) context: C,
105105
}
106106

107-
impl<C, D: Decoder + ?Sized> Sealed for WithContext<'_, '_, D, C> {}
107+
impl<C, D: Decoder + ?Sized> Sealed for WithContext<'_, D, C> {}
108108

109-
impl<Context, D: Decoder + ?Sized> Decoder for WithContext<'_, '_, D, Context> {
109+
impl<Context, D: Decoder + ?Sized> Decoder for WithContext<'_, D, Context> {
110110
type R = D::R;
111111

112112
type C = D::C;
113113

114114
type Context = Context;
115115

116116
fn context(&mut self) -> &mut Self::Context {
117-
self.context
117+
&mut self.context
118118
}
119119

120120
fn reader(&mut self) -> &mut Self::R {
@@ -134,7 +134,7 @@ impl<Context, D: Decoder + ?Sized> Decoder for WithContext<'_, '_, D, Context> {
134134
}
135135
}
136136

137-
impl<'de, C, D: BorrowDecoder<'de>> BorrowDecoder<'de> for WithContext<'_, '_, D, C> {
137+
impl<'de, C, D: BorrowDecoder<'de>> BorrowDecoder<'de> for WithContext<'_, D, C> {
138138
type BR = D::BR;
139139
fn borrow_reader(&mut self) -> &mut Self::BR {
140140
self.decoder.borrow_reader()

src/de/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub use self::decoder::DecoderImpl;
2525
///
2626
/// This trait should be implemented for types which do not have references to data in the reader. For types that contain e.g. `&str` and `&[u8]`, implement [BorrowDecode] instead.
2727
///
28-
/// Whenever you implement `Decode` for your type, the base trait `BorrowDecode` is automatically implemented.
28+
/// Whenever you derive `Decode` for your type, the base trait `BorrowDecode` is automatically implemented.
2929
///
3030
/// This trait will be automatically implemented with unbounded `Context` if you enable the `derive` feature and add `#[derive(bincode::Decode)]` to your type. Note that if the type contains any lifetimes, `BorrowDecode` will be implemented instead.
3131
///
@@ -131,6 +131,7 @@ macro_rules! impl_borrow_decode {
131131
};
132132
}
133133

134+
/// Helper macro to implement `BorrowDecode` for any type that implements `Decode`.
134135
#[macro_export]
135136
macro_rules! impl_borrow_decode_with_context {
136137
($ty:ty, $context:ty $(, $param:tt)*) => {
@@ -158,10 +159,8 @@ pub trait Decoder: Sealed {
158159
/// Returns the decoding context
159160
fn context(&mut self) -> &mut Self::Context;
160161

161-
fn with_context<'a, 'context, C>(
162-
&'a mut self,
163-
context: &'context mut C,
164-
) -> WithContext<'a, 'context, Self, C> {
162+
/// Wraps decoder with a context
163+
fn with_context<'a, C>(&'a mut self, context: C) -> WithContext<'a, Self, C> {
165164
WithContext {
166165
decoder: self,
167166
context,

src/features/impl_std.rs

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub fn decode_from_std_read<D: Decode<()>, C: Config, R: std::io::Read>(
3030
decode_from_std_read_with_context(src, config, ())
3131
}
3232

33+
/// Decode type `D` from the given reader with the given `Config` and `Context`. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`.
34+
///
35+
/// See the [config] module for more information about config options.
36+
///
37+
/// [config]: config/index.html
3338
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3439
pub fn decode_from_std_read_with_context<
3540
Context,

src/features/serde/de_owned.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,28 @@ impl<DE: Decoder> OwnedSerdeDecoder<DE> {
2424
}
2525

2626
#[cfg(feature = "std")]
27-
impl<'r, C: Config, R: std::io::Read, Context>
28-
OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, Context>>
29-
{
27+
impl<'r, C: Config, R: std::io::Read> OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, ()>> {
3028
/// Creates the decoder from an `std::io::Read` implementor.
3129
pub fn from_std_read(
3230
src: &'r mut R,
3331
config: C,
34-
context: Context,
35-
) -> OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, Context>>
32+
) -> OwnedSerdeDecoder<DecoderImpl<IoReader<&'r mut R>, C, ()>>
3633
where
3734
C: Config,
3835
{
3936
let reader = IoReader::new(src);
40-
let decoder = DecoderImpl::new(reader, config, context);
37+
let decoder = DecoderImpl::new(reader, config, ());
4138
Self { de: decoder }
4239
}
4340
}
4441

45-
impl<C: Config, R: Reader, Context> OwnedSerdeDecoder<DecoderImpl<R, C, Context>> {
42+
impl<C: Config, R: Reader> OwnedSerdeDecoder<DecoderImpl<R, C, ()>> {
4643
/// Creates the decoder from a [`Reader`] implementor.
47-
pub fn from_reader(
48-
reader: R,
49-
config: C,
50-
context: Context,
51-
) -> OwnedSerdeDecoder<DecoderImpl<R, C, Context>>
44+
pub fn from_reader(reader: R, config: C) -> OwnedSerdeDecoder<DecoderImpl<R, C, ()>>
5245
where
5346
C: Config,
5447
{
55-
let decoder = DecoderImpl::new(reader, config, context);
48+
let decoder = DecoderImpl::new(reader, config, ());
5649
Self { de: decoder }
5750
}
5851
}
@@ -85,11 +78,7 @@ pub fn decode_from_std_read<'r, D: DeserializeOwned, C: Config, R: std::io::Read
8578
config: C,
8679
) -> Result<D, DecodeError> {
8780
let mut serde_decoder =
88-
OwnedSerdeDecoder::<DecoderImpl<IoReader<&'r mut R>, C, ()>>::from_std_read(
89-
src,
90-
config,
91-
(),
92-
);
81+
OwnedSerdeDecoder::<DecoderImpl<IoReader<&'r mut R>, C, ()>>::from_std_read(src, config);
9382
D::deserialize(serde_decoder.as_deserializer())
9483
}
9584

@@ -102,8 +91,7 @@ pub fn decode_from_reader<D: DeserializeOwned, R: Reader, C: Config>(
10291
reader: R,
10392
config: C,
10493
) -> Result<D, DecodeError> {
105-
let mut serde_decoder =
106-
OwnedSerdeDecoder::<DecoderImpl<R, C, ()>>::from_reader(reader, config, ());
94+
let mut serde_decoder = OwnedSerdeDecoder::<DecoderImpl<R, C, ()>>::from_reader(reader, config);
10795
D::deserialize(serde_decoder.as_deserializer())
10896
}
10997

src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![no_std]
2-
#![warn(unused_lifetimes)]
2+
#![warn(missing_docs, unused_lifetimes)]
33
#![cfg_attr(docsrs, feature(doc_cfg))]
44

55
//! Bincode is a crate for encoding and decoding using a tiny binary
@@ -152,6 +152,13 @@ pub fn decode_from_slice<D: de::Decode<()>, C: Config>(
152152
decode_from_slice_with_context(src, config, ())
153153
}
154154

155+
/// Attempt to decode a given type `D` from the given slice with `Context`. Returns the decoded output and the amount of bytes read.
156+
///
157+
/// Note that this does not work with borrowed types like `&str` or `&[u8]`. For that use [borrow_decode_from_slice].
158+
///
159+
/// See the [config] module for more information on configurations.
160+
///
161+
/// [config]: config/index.html
155162
pub fn decode_from_slice_with_context<Context, D: de::Decode<Context>, C: Config>(
156163
src: &[u8],
157164
config: C,
@@ -176,6 +183,11 @@ pub fn borrow_decode_from_slice<'a, D: de::BorrowDecode<'a, ()>, C: Config>(
176183
borrow_decode_from_slice_with_context(src, config, ())
177184
}
178185

186+
/// Attempt to decode a given type `D` from the given slice with `Context`. Returns the decoded output and the amount of bytes read.
187+
///
188+
/// See the [config] module for more information on configurations.
189+
///
190+
/// [config]: config/index.html
179191
pub fn borrow_decode_from_slice_with_context<
180192
'a,
181193
Context,

tests/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl<Context> Decode<Context> for SelfReferencing {
8080
fn decode<D: bincode::de::Decoder<Context = Context>>(
8181
decoder: &mut D,
8282
) -> Result<Self, DecodeError> {
83-
SelfReferencing::try_new(Bump::new(), |mut bump| {
84-
Container::decode(&mut decoder.with_context(&mut bump))
83+
SelfReferencing::try_new(Bump::new(), |bump| {
84+
Container::decode(&mut decoder.with_context(bump))
8585
})
8686
}
8787
}

0 commit comments

Comments
 (0)