Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
With the current
Decode
/BorrowDecode
, it's impossible to implement them on a type that borrows something other than the input data. A concrete example would be aVec<'bump, T: 'bump>
which is allocated by an arena allocator.How
This PR introduces the following changes:
decode_from_slice_with_ctx<Ctx, D: de::Decode<Ctx>, C: Config>(src: &[u8], config: C, ctx: Ctx)
. In that arenaVec
example, the decode context would be the allocator&Bump
.DecoderImpl
and exposed toDecode
implements viaDecoder::ctx
method.Decode
trait is changed toDecode<C>
, which allows types to implementDecode
only under some specific context. (Vec<'bump, T: 'bump>
would implementDecode<&'bump Bump>
)Decode
is changed to generatingimpl<Ctx> Decode<Ctx> for ...
instead ofimpl Decode for ...
. A container attributedecode_context
is added to allow derivingDecode
with a concrete context type. (for example#[bincode(decode_context = "&'bump Bump")]
)BorrowDecoder
.You can see how these changes work together in
tests/ctx.rs
.I know this is a big change and would break existing
2.0.0
users. But it's stillrc
so I guess it's not too late to discuss it? ;)Alternative Design
Leaves
(Borrow)Decode
untouched, and add(Borrow)DecodeWithContext<C>
.Pros:
(Borrow)Decode
.Con:
TODOs
This PR hasn't been polished to a mergeable state yet. I will do that if the overall design is accepted.
impl<Ctx>
. This would need a separate PR.Decode
allowsCtx
to be either a concrete type or totally unbounded. How about allowingCtx: ...
? Is it useful enough to be implemented?