Allow creating decoder over raw slice pointer without aliasing #644
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.
Currently, the
Decoder
keeps a reference to the data slice passed in constructors. This means that there is no way to unsafely create a decoder over a partially valid or aliased memory range (where we assert that the specific subslices at which we'll be decoding are safe to read) without immediately causing UB. Because the decoder uses raw pointers internally in its logic, this seems like an unnecessary restriction which forces re-creating decoders every time we want to decode at an arbitrary address.This PR adds an unsafe constructor,
try_with_slice_ptr
, which lets one construct a decoder from a raw slice pointer. To avoid aliasing, thedata
field in theDecoder
struct is replaced by a slice pointer, and the reference is moved inside aPhantomData
.Since the
slice_ptr_len
feature was only stabilized in 1.79, this does require bumping the MSRV, which I understand is probably not desirable. In this case the constructor could be replaced bytry_with_raw_parts
which takes a*const u8
and length instead.