Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize feature cstr_from_bytes_until_nul #107429

Merged
merged 2 commits into from
Feb 9, 2023

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jan 28, 2023

This PR seeks to stabilize cstr_from_bytes_until_nul.

Partially addresses #95027

This function has only been on nightly for about 10 months, but I think it is simple enough that there isn't harm discussing stabilization. It has also had at least a handful of mentions on both the user forum and the discord, so it seems like it's already in use or at least known.

This needs FCP still.

Comment on potential discussion points:

  • eventual conversion of CStr to be a single thin pointer: this function will still be useful to provide a safe way to create a CStr after this change.
  • should this return a length too, to address concerns about the CStr change? I don't see it as being particularly useful, and it seems less ergonomic (i.e. returning Result<(&CStr, usize), FromBytesUntilNulError>). I think users that also need this length without the additional strlen call are likely better off using a combination of other methods, but this is up for discussion
  • CString::from_vec_until_nul: this is also useful, but it doesn't even have a nightly implementation merged yet. I propose feature gating that separately, as opposed to blocking this CStr implementation on that

Possible alternatives:

A user can use from_bytes_with_nul on a slice up to my_slice[..my_slice.iter().find(|c| c == 0).unwrap()]. However; that is significantly less ergonomic, and is a bit more work for the compiler to optimize compared the direct memchr call that this wraps.

New stable API

// both in core::ffi

pub struct FromBytesUntilNulError(());

impl CStr {
    pub const fn from_bytes_until_nul(
        bytes: &[u8]
    ) -> Result<&CStr, FromBytesUntilNulError>
}

cc @ericseppanen original author, @Mark-Simulacrum original reviewer, @m-ou-se brought up some issues on the thin pointer CStr

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

@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 28, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 28, 2023

The Miri subtree was changed

cc @rust-lang/miri

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rustbot rustbot added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 28, 2023
@cuviper
Copy link
Member

cuviper commented Jan 28, 2023

r? libs-api

@rustbot rustbot assigned m-ou-se and unassigned cuviper Jan 28, 2023
@tgross35 tgross35 force-pushed the from-bytes-until-null-stabilization branch 2 times, most recently from 36956fc to c223206 Compare January 28, 2023 23:13
@joshtriplett joshtriplett removed the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jan 29, 2023
@joshtriplett
Copy link
Member

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Jan 29, 2023

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jan 29, 2023
@rfcbot
Copy link

rfcbot commented Jan 29, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@joshtriplett
Copy link
Member

@tgross35 Per @oli-obk's comment, could you update the PR to stabilize the constness as well?

@tgross35
Copy link
Contributor Author

@joshtriplett I just replied to that comment, but I wasn’t able to do so because memchr isn’t const stable. Is there a way around that since it’s an internal call?

@mejrs
Copy link
Contributor

mejrs commented Jan 30, 2023

@joshtriplett I just replied to that comment, but I wasn’t able to do so because memchr isn’t const stable. Is there a way around that since it’s an internal call?

It is const. See core::slice::memchr::memchr.

@tgross35
Copy link
Contributor Author

It is const. See core::slice::memchr::memchr.

Hm, you're right. But marking it const stable gets this failure https://github.com/rust-lang/rust/actions/runs/4049015645/jobs/6964924272#step:26:777 any clue why?

The slice index is also an issue but I think that could be worked around with unsafe from_raw_parts if needed

@mejrs
Copy link
Contributor

mejrs commented Jan 30, 2023

@rust-log-analyzer

This comment has been minimized.

@tgross35
Copy link
Contributor Author

Halfway there; #[rustc_allow_const_fn_unstable(const_slice_index)] fixed the complaint about slice indexing. But do you have any idea what the const feature flag for memchr would be @mejrs? Doesn't look feature gated in the repo, and slice_internals doesn't work

@oli-obk
Copy link
Contributor

oli-obk commented Jan 31, 2023

Ah this is an oddity of our const stability checks. You need to add a #[rustc_const_stable] attribute on memchr (and probably recursively on memchr_naive and memchr_aligned, too). Even if the function isn't stable, it must be const stable to be usable in a const stable function.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 8, 2023
@rfcbot
Copy link

rfcbot commented Feb 8, 2023

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@dtolnay
Copy link
Member

dtolnay commented Feb 8, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Feb 8, 2023

📌 Commit 877e9f5 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 8, 2023
@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Feb 8, 2023
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…bilization, r=dtolnay

Stabilize feature `cstr_from_bytes_until_nul`

This PR seeks to stabilize `cstr_from_bytes_until_nul`.

Partially addresses rust-lang#95027

This function has only been on nightly for about 10 months, but I think it is simple enough that there isn't harm discussing stabilization. It has also had at least a handful of mentions on both the user forum and the discord, so it seems like it's already in use or at least known.

This needs FCP still.

Comment on potential discussion points:
- eventual conversion of `CStr` to be a single thin pointer: this function will still be useful to provide a safe way to create a `CStr` after this change.
- should this return a length too, to address concerns about the `CStr` change? I don't see it as being particularly useful, and it seems less ergonomic (i.e. returning `Result<(&CStr, usize), FromBytesUntilNulError>`). I think users that also need this length without the additional `strlen` call are likely better off using a combination of other methods, but this is up for discussion
- `CString::from_vec_until_nul`: this is also useful, but it doesn't even have a nightly implementation merged yet. I propose feature gating that separately, as opposed to blocking this `CStr` implementation on that

Possible alternatives:

A user can use `from_bytes_with_nul` on a slice up to `my_slice[..my_slice.iter().find(|c| c == 0).unwrap()]`. However; that is significantly less ergonomic, and is a bit more work for the compiler to optimize compared the direct `memchr` call that this wraps.

## New stable API

```rs
// both in core::ffi

pub struct FromBytesUntilNulError(());

impl CStr {
    pub const fn from_bytes_until_nul(
        bytes: &[u8]
    ) -> Result<&CStr, FromBytesUntilNulError>
}
```

cc `@ericseppanen` original author, `@Mark-Simulacrum` original reviewer, `@m-ou-se` brought up some issues on the thin pointer CStr

`@rustbot` modify labels: +T-libs-api +needs-fcp
compiler-errors added a commit to compiler-errors/rust that referenced this pull request Feb 9, 2023
…bilization, r=dtolnay

Stabilize feature `cstr_from_bytes_until_nul`

This PR seeks to stabilize `cstr_from_bytes_until_nul`.

Partially addresses rust-lang#95027

This function has only been on nightly for about 10 months, but I think it is simple enough that there isn't harm discussing stabilization. It has also had at least a handful of mentions on both the user forum and the discord, so it seems like it's already in use or at least known.

This needs FCP still.

Comment on potential discussion points:
- eventual conversion of `CStr` to be a single thin pointer: this function will still be useful to provide a safe way to create a `CStr` after this change.
- should this return a length too, to address concerns about the `CStr` change? I don't see it as being particularly useful, and it seems less ergonomic (i.e. returning `Result<(&CStr, usize), FromBytesUntilNulError>`). I think users that also need this length without the additional `strlen` call are likely better off using a combination of other methods, but this is up for discussion
- `CString::from_vec_until_nul`: this is also useful, but it doesn't even have a nightly implementation merged yet. I propose feature gating that separately, as opposed to blocking this `CStr` implementation on that

Possible alternatives:

A user can use `from_bytes_with_nul` on a slice up to `my_slice[..my_slice.iter().find(|c| c == 0).unwrap()]`. However; that is significantly less ergonomic, and is a bit more work for the compiler to optimize compared the direct `memchr` call that this wraps.

## New stable API

```rs
// both in core::ffi

pub struct FromBytesUntilNulError(());

impl CStr {
    pub const fn from_bytes_until_nul(
        bytes: &[u8]
    ) -> Result<&CStr, FromBytesUntilNulError>
}
```

cc ``@ericseppanen`` original author, ``@Mark-Simulacrum`` original reviewer, ``@m-ou-se`` brought up some issues on the thin pointer CStr

``@rustbot`` modify labels: +T-libs-api +needs-fcp
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 9, 2023
…mpiler-errors

Rollup of 9 pull requests

Successful merges:

 - rust-lang#107317 (Implement `AsFd` and `AsRawFd` for `Rc`)
 - rust-lang#107429 (Stabilize feature `cstr_from_bytes_until_nul`)
 - rust-lang#107713 (Extend `BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE`.)
 - rust-lang#107761 (Replace a command line flag with an env var to allow tools to initialize the tracing loggers at their own discretion)
 - rust-lang#107790 ( x.py fails all downloads that use a tempdir with snap curl rust-lang#107722)
 - rust-lang#107799 (correctly update goals in the cache)
 - rust-lang#107813 (Do not eagerly recover for bad `impl Trait` types in macros)
 - rust-lang#107817 (rustdoc: use svgo to shrink `wheel.svg`)
 - rust-lang#107819 (Set `rust-analyzer.check.invocationLocation` to `root`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit aee4570 into rust-lang:master Feb 9, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 9, 2023
@tgross35 tgross35 deleted the from-bytes-until-null-stabilization branch February 9, 2023 09:18
@hkBst
Copy link
Contributor

hkBst commented Feb 13, 2023

Is there any reason for misspelling null as nul in cstr_from_bytes_until_nul?

@ChrisDenton
Copy link
Member

For consistency with the other already stabilized methods.

@m-ou-se
Copy link
Member

m-ou-se commented Feb 13, 2023

It's somewhat common to use "nul" for chars and "null" for pointers.

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 16, 2023
@Stargateur
Copy link
Contributor

the ascii table has always noted the \0 character NUL (null)

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, so needs a completed 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.