Add Allocator support for String
#101
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
rust-lang/rust#101551
Add an unstable, defaulted generic parameter to
alloc::string::String
to allow use with custom allocators under#![feature(allocator_api)]
, and generalize most implementations onString
to be generic over the allocator.(There's a tracking issue rust-lang/wg-allocators#7 on wg-allocators, so I wasn't sure if this needed an ACP. I made one just to be safe.)
Problem statement
Currently,
Vec
andBox
have a second, unstable, defaulted generic parameterA: Allocator = Global
, allowing them to support custom allocators.String
does not currently have this, so it does not yet support custom allocators.Motivation, use-cases
Part of the effort to expand
allocator_api
to support more collection types. It could be useful to allow using common container types with different allocators that are better suited to different allocation patterns.Solution sketches
Add an
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global
generic toalloc::string::String
,alloc::string::FromUtf8Error
, andalloc::string::Drain
, and make most inherent functions and trait implementations generic over this parameter.Also make some corresponding non-allocator-generic functions and implementations on
Box<str>
allocator-generic.StructuralEq for String
:String
previously usedderive(Eq)
, but manually implementedPartialEq
. To prevent anA: Eq
bound, I removed thederive
and replaced it with a manualimpl Eq
without that bound. It should not be observable on stable, becauseString
does not implementStructuralPartialEq
, which is also required for structural matching.impl<A: Allocator> StructuralEq for String<A> {}
, etc. can be added if necessary.StructuralEq
/StructralPartialEq for FromUtf8Error
:FromUtf8Error
previously usedderive(PartialEq, Eq)
. However, it is not possible to get a value of typeFromUtf8Error
in constant evaluation on stable, so there is no way to construct anything that could even be used as a structural match pattern, so I don't think these removals are observable.String
functions (that already exist forVec
)new_in
/with_capacity_in
from_raw_parts_in
/into_raw_parts_with_alloc
allocator
new
/with_capacity
/from_utf16
/from_utf16_lossy
: These could useA: (~const) Default
, but seemed out of scope. Also, correspondingVec
functions are not allocator-generic.from_utf8_lossy
: This returns aCow<str>
, which is not allocator-generic.from_raw_parts
/into_raw_parts
: These are tied to the global allocator.Vec
/[T]
-like.Clone for Box<str, A> where A: Clone
Vec
/[T]
-likeBorrow/BorrowMut<str> for String<A>
edit: These have been removed from the linked PR to make room for futureFrom<String<A>> for Rc<str>
/Arc<str>
From<String<A>> for Rc<str, A>
-like impls.Default where A: Default
: this would require makingGlobal
implementconst Default
, which is probably fine, but seemed out-of-scope. Also, the correspondingVec
implementation is not allocator-generic.impl const Default for Box<str>
(andBox<[T]>
).From<&String> for String
: Unclear if it should beFrom<&String<A>> for String<A> where A: Clone
orFrom<&String<A2>> for String<A1> where A1: Default
FromStr
,From<&str-like>
,From<char>
,FromIterator<_>
: Could befor String<A> where A: Default
but seemed out-of-scope. Also, correspondingVec
implementations are not allocator-generic.From<&str> for Box<str>
andFrom<&[T]> for Box<T> where T: Clone
CString
, e.g.CString::into_string
alloc
crate, e.g.std::io::Read::read_to_string
,AsRef<OsStr> for String
,ToSocketAddrs for String
All unmentioned inherent functions and trait implementations on
String
,FromUtf8Error
, andDrain
are made allocator-generic (with appropriate bounds, e.g.A: Allocator + Clone
forString::split_off
).Several implementations involving
Box<str>
are also made allocator-genericFrom<Box<str, A>> for Box<[u8], A>
From<Box<str, A>> for String<A>
From<String<A>> for Box<str, A>
is not added currently because of trait coherence rules (help?).Links and related work
String
rust#101551(C)String
rust#79500 (closed in favor of 101551 above)Vec
rust#78461CString
and other containers.What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: