-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
constify some CStr
methods
#100291
constify some CStr
methods
#100291
Conversation
r? @scottmcm (rust-highfive has picked a reviewer for you, use r? to override) |
r? @oli-obk |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Thanks. LGTM. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops forgot to send off the review
library/core/src/ffi/c_str.rs
Outdated
@@ -299,7 +299,8 @@ impl CStr { | |||
/// ``` | |||
/// | |||
#[unstable(feature = "cstr_from_bytes_until_nul", issue = "95027")] | |||
pub fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError> { | |||
#[rustc_const_unstable(feature = "const_cstr_from_bytes", issue = "none")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use the same feature gate and issue as the regular unstable attribute, these can be stabilized together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I open an issues for from_bytes_with_nul
and to_bytes
/to_bytes_with_nul
/to_str
? (they are stable, so I can't reuse the feature gate/issue)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I think it's best to track them. You could just munge them into a generic cstr_consts gate and make one issue for all of them
library/core/src/slice/memchr.rs
Outdated
match bytes { | ||
[] => break None, | ||
&[y, ..] if x == y => break Some(i), | ||
[_, rest @ ..] => { | ||
bytes = rest; | ||
i += 1; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the ingenuity, but why not just while let i < bytes.len()
and index with i
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not remember, probably because of a habit, I think there was a time when you couldn't index in const
fns, but could use patterns.
c54464a
to
cb02b64
Compare
@bors r+ rollup |
…=oli-obk constify some `CStr` methods This PR marks the following public APIs as `const`: ```rust impl CStr { // feature(const_cstr_from_bytes) pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>; pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError>; // feature(const_cstr_to_bytes) pub const fn to_bytes(&self) -> &[u8]; pub const fn to_bytes_with_nul(&self) -> &[u8]; pub const fn to_str(&self) -> Result<&str, str::Utf8Error>; } ``` r? `@oli-obk` (use of `const_eval_select` :P ) cc `@mina86` (you've asked for this <3 )
…=oli-obk constify some `CStr` methods This PR marks the following public APIs as `const`: ```rust impl CStr { // feature(const_cstr_from_bytes) pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>; pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError>; // feature(const_cstr_to_bytes) pub const fn to_bytes(&self) -> &[u8]; pub const fn to_bytes_with_nul(&self) -> &[u8]; pub const fn to_str(&self) -> Result<&str, str::Utf8Error>; } ``` r? ``@oli-obk`` (use of `const_eval_select` :P ) cc ``@mina86`` (you've asked for this <3 )
…llaumeGomez Rollup of 8 pull requests Successful merges: - rust-lang#100185 (Fix `ReErased` leaking into typeck due to `typeof(...)` recovery) - rust-lang#100291 (constify some `CStr` methods) - rust-lang#101677 (Add test for rust-lang#101211) - rust-lang#101723 (Impove diagnostic for `.await`ing non-futures) - rust-lang#101724 (Allow unauthenticated users to add the `const-hack` label) - rust-lang#101731 (rustdoc: improve rustdoc HTML suggestions handling of nested generics) - rust-lang#101732 (Feature gate the `rustdoc::missing_doc_code_examples` lint) - rust-lang#101735 (rustdoc: fix treatment of backslash-escaped HTML) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This PR marks the following public APIs as
const
:r? @oli-obk (use of
const_eval_select
:P )cc @mina86 (you've asked for this <3 )