Skip to content

Commit a023a19

Browse files
committed
Add str.as_str() for easy dereferencing of Box<str>
1 parent 13b5a4e commit a023a19

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
#![feature(slice_ptr_get)]
148148
#![feature(slice_range)]
149149
#![feature(std_internals)]
150+
#![feature(str_as_str)]
150151
#![feature(str_internals)]
151152
#![feature(strict_provenance)]
152153
#![feature(trusted_fused)]

library/alloc/src/rc/tests.rs

+4
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ fn test_from_box_str() {
448448
use std::string::String;
449449

450450
let s = String::from("foo").into_boxed_str();
451+
assert_eq!((&&&s).as_str(), "foo");
452+
451453
let r: Rc<str> = Rc::from(s);
454+
assert_eq!((&r).as_str(), "foo");
455+
assert_eq!(r.as_str(), "foo");
452456

453457
assert_eq!(&r[..], "foo");
454458
}

library/core/src/str/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,17 @@ impl str {
27362736
pub fn substr_range(&self, substr: &str) -> Option<Range<usize>> {
27372737
self.as_bytes().subslice_range(substr.as_bytes())
27382738
}
2739+
2740+
/// Returns the same string as a string slice `&str`.
2741+
///
2742+
/// This method is redundant when used directly on `&str`, but
2743+
/// it helps dereferencing other string-like types to string slices,
2744+
/// for example references to `Box<str>` or `Arc<str>`.
2745+
#[inline]
2746+
#[unstable(feature = "str_as_str", issue = "130366")]
2747+
pub fn as_str(&self) -> &str {
2748+
self
2749+
}
27392750
}
27402751

27412752
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
#![feature(slice_ptr_get)]
352352
#![feature(slice_range)]
353353
#![feature(std_internals)]
354+
#![feature(str_as_str)]
354355
#![feature(str_internals)]
355356
#![feature(strict_provenance)]
356357
#![feature(strict_provenance_atomic_ptr)]

0 commit comments

Comments
 (0)