Skip to content

Commit

Permalink
Add boxed_str.as_str()
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 25, 2024
1 parent 697d953 commit 245206d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,15 @@ impl<T, A: Allocator> Box<T, A> {
}
}

impl<A: Allocator> Box<str, A> {
#[stable(feature = "box_as_str", since = "CURRENT_RUSTC_VERSION")]
#[inline]
/// Extracts a string slice containing the entire `Box<str>`.
pub fn as_str(&self) -> &str {
&*self
}
}

impl<T> Box<[T]> {
/// Constructs a new boxed slice with uninitialized contents.
///
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/rc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ fn test_from_box_str() {
use std::string::String;

let s = String::from("foo").into_boxed_str();
assert_eq!((&&&s).as_str(), "foo");
let r: Rc<str> = Rc::from(s);

assert_eq!(&r[..], "foo");
Expand Down

0 comments on commit 245206d

Please sign in to comment.