From 245206d93e94503d5e9d457210fec0b92c86cf9f Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 25 Aug 2024 11:20:42 +0100 Subject: [PATCH] Add boxed_str.as_str() --- library/alloc/src/boxed.rs | 9 +++++++++ library/alloc/src/rc/tests.rs | 1 + 2 files changed, 10 insertions(+) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 7de412595993a..f4795f7667857 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -647,6 +647,15 @@ impl Box { } } +impl Box { + #[stable(feature = "box_as_str", since = "CURRENT_RUSTC_VERSION")] + #[inline] + /// Extracts a string slice containing the entire `Box`. + pub fn as_str(&self) -> &str { + &*self + } +} + impl Box<[T]> { /// Constructs a new boxed slice with uninitialized contents. /// diff --git a/library/alloc/src/rc/tests.rs b/library/alloc/src/rc/tests.rs index 84e8b325f71fc..19d1113735c88 100644 --- a/library/alloc/src/rc/tests.rs +++ b/library/alloc/src/rc/tests.rs @@ -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 = Rc::from(s); assert_eq!(&r[..], "foo");