Skip to content

Commit 051a662

Browse files
committed
Use allocator parameter on String
1 parent 4447ae5 commit 051a662

File tree

3 files changed

+466
-219
lines changed

3 files changed

+466
-219
lines changed

src/liballoc/boxed.rs

+21
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ use core::task::{Context, Poll};
148148
use crate::alloc::{self, AllocInit, AllocRef, Global};
149149
use crate::raw_vec::RawVec;
150150
use crate::str::from_boxed_utf8_unchecked;
151+
use crate::string::String;
151152
use crate::vec::Vec;
152153

153154
/// A pointer type for heap allocation.
@@ -1001,6 +1002,26 @@ impl From<&str> for Box<str> {
10011002
}
10021003
}
10031004

1005+
#[stable(feature = "box_from_str", since = "1.20.0")]
1006+
impl<A: AllocRef> From<String<A>> for Box<str, A> {
1007+
/// Converts the given `String` to a boxed `str` slice that is owned.
1008+
///
1009+
/// # Examples
1010+
///
1011+
/// Basic usage:
1012+
///
1013+
/// ```
1014+
/// let s1: String = String::from("hello world");
1015+
/// let s2: Box<str> = Box::from(s1);
1016+
/// let s3: String = String::from(s2);
1017+
///
1018+
/// assert_eq!("hello world", s3)
1019+
/// ```
1020+
fn from(s: String<A>) -> Self {
1021+
s.into_boxed_str()
1022+
}
1023+
}
1024+
10041025
#[stable(feature = "boxed_str_conv", since = "1.19.0")]
10051026
impl<A: AllocRef> From<Box<str, A>> for Box<[u8], A> {
10061027
/// Converts a `Box<str>>` into a `Box<[u8]>`

src/liballoc/str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ where
183183
}
184184

185185
#[stable(feature = "rust1", since = "1.0.0")]
186-
impl Borrow<str> for String {
186+
impl<A: AllocRef> Borrow<str> for String<A> {
187187
#[inline]
188188
fn borrow(&self) -> &str {
189189
&self[..]
190190
}
191191
}
192192

193193
#[stable(feature = "string_borrow_mut", since = "1.36.0")]
194-
impl BorrowMut<str> for String {
194+
impl<A: AllocRef> BorrowMut<str> for String<A> {
195195
#[inline]
196196
fn borrow_mut(&mut self) -> &mut str {
197197
&mut self[..]

0 commit comments

Comments
 (0)