Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add support for custom allocator for String #101551

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
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
20 changes: 16 additions & 4 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,11 +1463,23 @@ 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 buf = RawVec::with_capacity_in(len, alloc);
unsafe {
ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len);
from_boxed_utf8_unchecked(buf.into_box(len).assume_init())
}
}

fn clone_from(&mut self, other: &Self) {
if self.len() == other.len() {
unsafe { self.as_bytes_mut().copy_from_slice(other.as_bytes()) }
} else {
*self = other.clone();
}
}
}

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 @@ -14,10 +14,11 @@ use core::ptr;
use core::str::pattern::{DoubleEndedSearcher, Pattern, ReverseSearcher, Searcher};
use core::unicode::conversions;

use crate::alloc::{Allocator, Global};
use crate::borrow::ToOwned;
use crate::boxed::Box;
use crate::slice::{Concat, Join, SliceIndex};
use crate::string::String;
use crate::string::string::String;
use crate::vec::Vec;

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -187,15 +188,15 @@ where
}

#[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 @@ -236,7 +237,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<str>) -> Box<[u8]> {
pub fn into_boxed_bytes<A: Allocator>(self: Box<str, A>) -> Box<[u8], A> {
self.into()
}

Expand Down Expand Up @@ -500,8 +501,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<str>) -> String {
let slice = Box::<[u8]>::from(self);
pub fn into_string<A: Allocator>(self: Box<str, A>) -> String<A> {
let slice = Box::<[u8], A>::from(self);
unsafe { String::from_utf8_unchecked(slice.into_vec()) }
}

Expand Down Expand Up @@ -612,8 +613,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 (raw, alloc) = Box::into_raw_with_allocator(v);
unsafe { Box::from_raw_in(raw as *mut str, alloc) }
}

/// Converts the bytes while the bytes are still ascii.
Expand Down
Loading
Loading