Allow creating decoder over raw slice pointer without aliasing#644
Open
tremwil wants to merge 4 commits intoicedland:masterfrom
Open
Allow creating decoder over raw slice pointer without aliasing#644tremwil wants to merge 4 commits intoicedland:masterfrom
tremwil wants to merge 4 commits intoicedland:masterfrom
Conversation
Member
|
Yeah 1.79 is too recent so can't merge anything that requires it. I'll bump the msrv but not to 1.79. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Decoderkeeps 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, thedatafield in theDecoderstruct is replaced by a slice pointer, and the reference is moved inside aPhantomData.Since the
slice_ptr_lenfeature 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_partswhich takes a*const u8and length instead.