-
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
Make [T]::len
and str::len
const fn
#50863
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2166,7 +2166,8 @@ impl str { | |
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[inline] | ||
pub fn len(&self) -> usize { | ||
#[rustc_const_unstable(feature = "const_str_len")] | ||
pub const fn len(&self) -> usize { | ||
self.as_bytes().len() | ||
} | ||
|
||
|
@@ -2185,7 +2186,8 @@ impl str { | |
/// ``` | ||
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn is_empty(&self) -> bool { | ||
#[rustc_const_unstable(feature = "const_str_len")] | ||
pub const fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
|
@@ -2242,8 +2244,15 @@ impl str { | |
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[inline(always)] | ||
pub fn as_bytes(&self) -> &[u8] { | ||
unsafe { &*(self as *const str as *const [u8]) } | ||
#[rustc_const_unstable(feature="const_str_as_bytes")] | ||
pub const fn as_bytes(&self) -> &[u8] { | ||
unsafe { | ||
union Slices<'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can/should the union be moved outside the unsafe block? it's definition isn't unsafe iirc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While that could certainly be done, there's no difference here and this seems very self-contained to me. YMMV, so feel free to open a PR about that if you want :) |
||
str: &'a str, | ||
slice: &'a [u8], | ||
} | ||
Slices { str: self }.slice | ||
} | ||
} | ||
|
||
/// Converts a mutable string slice to a mutable byte slice. To convert the | ||
|
@@ -2303,7 +2312,8 @@ impl str { | |
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[inline] | ||
pub fn as_ptr(&self) -> *const u8 { | ||
#[rustc_const_unstable(feature = "const_str_as_ptr")] | ||
pub const fn as_ptr(&self) -> *const u8 { | ||
self as *const str as *const u8 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// run-pass | ||
|
||
#![feature(const_str_len, const_str_as_bytes)] | ||
|
||
const S: &str = "foo"; | ||
pub const B: &[u8] = S.as_bytes(); | ||
|
||
pub fn foo() -> [u8; S.len()] { | ||
let mut buf = [0; S.len()]; | ||
for (i, &c) in S.as_bytes().iter().enumerate() { | ||
buf[i] = c; | ||
} | ||
buf | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this assert some equality? |
||
|
||
fn main() { | ||
assert_eq!(&foo()[..], b"foo"); | ||
assert_eq!(foo().len(), S.len()); | ||
const LEN: usize = S.len(); | ||
assert_eq!(LEN, S.len()); | ||
assert_eq!(B, foo()); | ||
assert_eq!(B, b"foo"); | ||
} |
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.
Nice!