Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,7 @@ symbols! {
str_inherent_from_utf8_unchecked_mut,
strict_provenance_lints,
string_deref_patterns,
string_in_global,
stringify,
struct_field_attributes,
struct_inherit,
Expand Down
12 changes: 8 additions & 4 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::raw_vec::RawVec;
#[cfg(not(no_global_oom_handling))]
use crate::str::from_boxed_utf8_unchecked;
#[cfg(not(no_global_oom_handling))]
use crate::vec::Vec;

/// Conversion related impls for `Box<_>` (`From`, `downcast`, etc)
mod convert;
Expand Down Expand Up @@ -2105,11 +2107,13 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl Clone for Box<str> {
impl<A: Allocator + Clone> Clone for Box<str, A> {
fn clone(&self) -> Self {
// this makes a copy of the data
let buf: Box<[u8]> = self.as_bytes().into();
unsafe { from_boxed_utf8_unchecked(buf) }
let alloc = Box::allocator(self).clone();
let len = self.len();
let mut vec: Vec<u8, A> = Vec::with_capacity_in(len, alloc);
vec.extend_from_slice(self.as_bytes());
unsafe { from_boxed_utf8_unchecked(vec.into_boxed_slice()) }
}
}

Expand Down
18 changes: 10 additions & 8 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// It's cleaner to just turn off the unused_imports warning than to fix them.
#![allow(unused_imports)]

use core::alloc::Allocator;
use core::borrow::{Borrow, BorrowMut};
use core::iter::FusedIterator;
use core::mem::MaybeUninit;
Expand Down Expand Up @@ -52,7 +53,7 @@ use core::{mem, ptr};
use crate::borrow::ToOwned;
use crate::boxed::Box;
use crate::slice::{Concat, Join, SliceIndex};
use crate::string::String;
use crate::string::generic::String;
use crate::vec::Vec;

/// Note: `str` in `Concat<str>` is not meaningful here.
Expand Down Expand Up @@ -226,15 +227,15 @@ fn map_uppercase_sigma(from: &str, i: usize) -> char {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Borrow<str> for String {
impl<A: Allocator> Borrow<str> for String<A> {
#[inline]
fn borrow(&self) -> &str {
&self[..]
}
}

#[stable(feature = "string_borrow_mut", since = "1.36.0")]
impl BorrowMut<str> for String {
impl<A: Allocator> BorrowMut<str> for String<A> {
#[inline]
fn borrow_mut(&mut self) -> &mut str {
&mut self[..]
Expand Down Expand Up @@ -274,7 +275,7 @@ impl str {
#[stable(feature = "str_box_extras", since = "1.20.0")]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub fn into_boxed_bytes(self: Box<Self>) -> Box<[u8]> {
pub fn into_boxed_bytes<A: Allocator>(self: Box<Self, A>) -> Box<[u8], A> {
self.into()
}

Expand Down Expand Up @@ -775,8 +776,8 @@ impl str {
#[rustc_allow_incoherent_impl]
#[must_use = "`self` will be dropped if the result is not used"]
#[inline]
pub fn into_string(self: Box<Self>) -> String {
let slice = Box::<[u8]>::from(self);
pub fn into_string<A: Allocator>(self: Box<Self, A>) -> String<A> {
let slice = Box::<[u8], A>::from(self);
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
}

Expand Down Expand Up @@ -892,8 +893,9 @@ impl str {
#[stable(feature = "str_box_extras", since = "1.20.0")]
#[must_use]
#[inline]
pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
pub unsafe fn from_boxed_utf8_unchecked<A: Allocator>(v: Box<[u8], A>) -> Box<str, A> {
let (ptr, alloc) = Box::into_raw_with_allocator(v);
unsafe { Box::from_raw_in(ptr as *mut str, alloc) }
}

/// Converts leading ascii bytes in `s` by calling the `convert` function.
Expand Down
Loading
Loading