-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
base: master
Are you sure you want to change the base?
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
6faca17
to
a2b102a
Compare
| | expected struct `String`, found `&String` | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected struct `String` | ||
found reference `&String` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR changed a lot of the UI tests/diagnostics to have duplicated-ish note messages. I don't know enough about the diagnostics system to really understand why. Are these changes acceptable?
(Several similar changes, e.g. expected struct String, found type str
in src/test/ui/issues/issue-53348.stderr, expected struct String, found unit type ()
in src/test/ui/block-result/consider-removing-last-semi.stderr)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also don't know the cause here, but it feels like it might be a larger bug -- maybe worth trying to reproduce on stable/nightly outside of the String type (e.g., have a struct and add a generic parameter to it) and see if that causes similar behavior on regular rustc compilers? If so, then you could file an issue for that.
If this is something specific to String that's probably worth poking at too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not appear to be specific to String; I have opened #101992 about it.
This comment has been minimized.
This comment has been minimized.
a2b102a
to
7ab2548
Compare
This comment has been minimized.
This comment has been minimized.
Things that are the same as 79500:The following were made allocator-aware (or added) in the same way as in 79500:
DIfferences from 79500 :
Additions from 79500:
|
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
It passes the checks now, so I'm marking this as ready for review. Feedback is appreciated! (especially on the unresolved questions in the Todos). (CC'ing people from 79500) |
(I think this is right?) |
library/alloc/src/string.rs
Outdated
#[inline] | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
#[must_use] | ||
pub fn with_capacity_in(capacity: usize, alloc: A) -> String<A> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sort of feel like we shouldn't provide these APIs and instead encourage String::from(Vec::with_capacity_in(....))
, since they do add noise to documentation and most users aren't using them. But that can be handled at stabilization time, I suppose, and is a broader question about everything being allocator-generic.
library/alloc/src/string.rs
Outdated
|
||
#[cfg(not(no_global_oom_handling))] | ||
#[stable(feature = "from_char_for_string", since = "1.46.0")] | ||
impl From<char> for String { | ||
// FIXME(zachs18): A: Allocator + Default? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add this comment; there's many APIs which could do this (even on Vec) and it doesn't seem worth leaving comments strewn around std for that future possibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I had intended to remove or implement most of the FIXME
s based on feedback before the PR gets merged. I'll just remove the ones that don't currently exist for Vec
for this PR.
A few notes:
I think this is not observable on stable, though I'm not entirely confident. The primary area where it matters that I know of is pattern matching with a const value, but that requires StructuralPartialEq too.
In order to check whether this is a breaking change we need to know whether any const context on stable can get the value -- I think the answer may be no, but I'm not confident. Also left some comments inline, going to @rustbot author for now but please mark it as ready for review if the ACP goes through and you have addressed comments (or posted back with questions). |
0445581
to
311f4a5
Compare
This comment has been minimized.
This comment has been minimized.
311f4a5
to
c75bd1f
Compare
This comment has been minimized.
This comment has been minimized.
Still more work to do to to get clippy/tests/etc working with having |
@zachs18 any updates on this? thanks |
More `allocator_api`-aware `String` implementations (Most allocator-irrelevant associated functions, Drain, and FromUtf8Error). More `allocator_api`-aware `String` implementations, including added `new_in` etc inherent impls. tidy, new_in etc, raw_parts_in etc, allocator(&self), FromUtf8Error impls, Extend, Pattern, PartialEq &str etc with String<A>, Display, Debug, Hash, Add(Assign), ToString, AsRef/AsMut, conversions Box<str, A> -> String<A>, &String -> Cow<str>, String<A> -> Vec<u8, A>; fmt::Write. Fix gdb/lldb integration for String<A>. Add some simple trait impls I missed. Borrow(Mut)<str> for String, From<String<A>> for Rc<str>/Arc<str>, Clone for Box<str, A>. Remove FIXMEs for String<A> APIs that don't already exist for Vec<T,A>. Rewrite `Clone for Box<str, A>` to avoid extra capacity/length checks converting from Vec. Also implement `clone_from` to re-use the allocation when the lengths are the same. Remove `From<String<A>> for Rc<str>`/`Arc` impls to allow for future alloc-ification of `Rc`/`Arc` Note: find out how to make "the full type name has been written to" errors be deterministic (./x.py test alone didn't do it). AsRef<OsStr> and AsRef<Path> for String<A: Allocator>
…alias with the generic filled in.
TODO: try to modify the compiler to remove the <A> from "help: the trait `Clone` is implemented for `String<A>`".
…eneric parameter to String.
… to fix them right now, so just adding the changes so I can look at other errors. Fix these before merging.
This comment has been minimized.
This comment has been minimized.
…str, A> to A = Global.
This comment has been minimized.
This comment has been minimized.
…o the new path. Also reverts some UI test changes that were due to on_unimplemented path being wrong.
This comment has been minimized.
This comment has been minimized.
The job Click to see the possible cause of the failure (guessed by this bot)
|
☔ The latest upstream changes (presumably #125443) made this pull request unmergeable. Please resolve the merge conflicts. |
Roadmap for allocator support.
API Change Proposal: rust-lang/libs-team#101 [ Accepted ]
A subset/bring-to-current of #79500 utilizing a workaround(?) to the issue (#79499) that was blocking it.
(Note: I previously tried rebasing that (2 year old) PR onto current master, which was not my best idea).
Blocked on:
String
libs-team#101Todo/Unresolved Questions:
Fix UI tests.Done?(see my question below)(see Apparent duplicated diagnostic note for type mismatch involving type with defaulted type parameter. #101992)String<A>
.ShouldremovedFrom<String<A>> for Rc<str>
/Arc
be removed from this PR? (Corresponding impl does not exist forVec
/Rc<[T]>
, and also it may preclude a futureFrom<String<A>> for Rc<str, A>
)impl StructuralEq for String<_>
be re-added? This PR removes it (because it no longerderive(Eq)
), but it may not be observable becauseString: !StructuralPartialEq
FromUtf8Error<_>
, but that derived bothPartialEq
andEq
so it had bothStructuralPartialEq
andStructuralEq
.CString
, which derivesPartialEq
andEq
.)impl<A: Allocator> From<String<A>> to Box<str, A>
breaks orphan rules? (becauseBox
andstr
are fundamental?)String<A>
?fn String<A>::from_str_in(s: &str, alloc: A) -> String<A>
?fn str::to_string_in(alloc: A) -> String<A>
(parallelsslice::to_vec_in
)to_string_in
toToString
? (Probably a non-starter, since that would make ToString not object-safe, unless we put awhere Self: Sized
, which would precludestr
)[ ] Other(removed FIXME comments, APIs moved to future work)FIXME
s (implement or remove comments)Possible Future work
from_utf8_lossy
, which returns a non-allocator-awareCow<str>
?from_utf16(_lossy)
, which returnString
andResult<String, _>
?String<A> where A: Default
from_utf16(_lossy)_in
, (Add support for custom allocator for(C)String
#79500 only addedfrom_utf16_in
, not the lossy version)From<Cow<str>> for String<A> where A: Default
could be implemented asfrom_str_in(&*cow, A::default())
, and specialized forA = Global
to the current implementationcow.into_owned()
.From<&String<A>> for String<A> where A: Clone
orFrom<&String<A>> for String<B> where B: Default
(I don't think both can exist).FromIterator<_> for String
,FromStr for String
, andFrom<&str-like> for String
could befor String<A> where A: Default
.impl const Default for String
could beimpl<A: _ + ~const Default> Default for String<A>
, except thatGlobal
doesn't implementconst Default
currently (but I don't see any reason it couldn't).