Skip to content

Stabilize const_cstr_methods#107624

Merged
bors merged 1 commit intorust-lang:masterfrom
tgross35:const-cstr-methods
Jun 30, 2023
Merged

Stabilize const_cstr_methods#107624
bors merged 1 commit intorust-lang:masterfrom
tgross35:const-cstr-methods

Conversation

@tgross35
Copy link
Contributor

@tgross35 tgross35 commented Feb 3, 2023

This PR seeks to stabilize const_cstr_methods. Fixes most of #101719

New const stable API

impl CStr {
    // depends: memchr
    pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError> {...}
    // depends: const_slice_index
    pub const fn to_bytes(&self) -> &[u8] {}
    // depends: pointer casts
    pub const fn to_bytes_with_nul(&self) -> &[u8] {}
    // depends: str::from_utf8
    pub const fn to_str(&self) -> Result<&str, str::Utf8Error> {}
}

I don't think any of these methods will have any issue when CStr becomes a thin pointer as long as memchr is const (which also allows for const strlen) .

Notes

  • from_bytes_until_nul relies on const_slice_index, which relies on const_trait_impls, and generally this should be avoided. After talking with Oli, it should be OK in this case because we could replace the ranges with pointer tricks if needed (worst case being those feature gates disappear). Stabilize const_cstr_methods #107624 (comment)
  • Making from_ptr const is deferred because it depends on const_eval_select. I have moved this under the new flag const_cstr_from_ptr Stabilize const_cstr_methods #107624 (comment)

cc @oli-obk I think you're the const expert

@rustbot modify labels: +T-libs-api +needs-fcp

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.