liballoc: introduce String, Vec const-slicing#128399
Merged
Merged
Conversation
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.
This change
const-qualifies many methods onVecandString, notablyas_slice,as_str,len. These changes are made behind the unstable feature flagconst_vec_string_slice.Motivation
This is to support simultaneous variance over ownership and constness. I have an enum type that may contain either
Stringor&str, and I want to produce a&strfrom it in a possibly-constcontext.Currently
StringandVecdon't support this, but can without functional changes. Similar logic applies forlen,capacity,is_empty.Changes
The essential thing enabling this change is that
Unique::as_ptrisconst. This lets us convertRawVec::ptr->Vec::as_ptr->Vec::as_slice->String::as_str.I had to move the
Derefimplementations intoas_{str,slice}becauseDerefisn't#[const_trait], but I would expect this change to be invisible up to inlining. I moved theDerefMutimplementations as well for uniformity.