-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
[proof of concept] give String support for CoW wrapping of literals #46993
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
How to test this: replace instances of:
with |
Ok(ptr) => (new_cap, ptr), | ||
Err(e) => self.a.oom(e), | ||
if used_cap == 0 { | ||
// skip to 4 because tiny Vec's are dumb; but not if that |
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.
Isn't it "Vecs", not "Vec's"?
You only need the apostrophe if the "Vec" is possessing something.
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 is a pre-existing comment, but yes
@@ -384,6 +384,38 @@ impl String { | |||
String { vec: Vec::new() } | |||
} | |||
|
|||
#[inline] | |||
#[unstable(feature = "lit_strings", reason = "its TOO lit", issue = "42069")] |
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.
You might want to change this to something more informative.
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 isn't intended to be landed any time soon, so I opted for a joke :)
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 isn't intended to be landed any time soon
Do you say this because there is more implementation work to be done, or because it hasn't been decided that this is a good thing? (I'm pretty out of the loop.) If the former, cool. If the latter, has it been discussed somewhere?
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's a thread on internals about landing this feature as a coercion (rather than a method): https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029
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.
Oh hey, that's pretty cool! Thanks.
https://github.com/manishearth/rust/tree/literally-sed has it all sed-ed. The sed commands are in each commit ( There's a |
I don't really know how to triage this. You uh... hmm. Uh, what do you think we should do? |
@shepmaster I'm gonna mark as waiting on (libs) team. cc @rust-lang/libs |
The libs team discussed this PR some today, and the following points came up:
Modulo these concerns, no one saw a blocking objection, and we'd love to see this proceed to an RFC. |
We only found examples of people assuming vec's capacity. We halted progress because it was possible to use String in the same way. It still is. That's one of the reasons this PR exists: so we can test this change out on other crates. Sadly it's hard to cargo-bomb this since nothing in the ecosystem uses it, so nothing would trip it! I am vaguely considered about stable address assumptions, but also we could reasonably just phase it out of the ecosystem, like we've done for other implementation-defined assumptions that we've broken (heap::EMPTY). |
Unless I misunderstood something, the assumption in question is This PR breaks the assumption, but I believe it's easy to avoid breaking it. The trick is to implement fn capacity(&self) -> usize {
cmp::max(self.vec.capacity(), self.vec.len())
} This way the assumption is still valid, and we can still internally disambiguate owned and literal strings by checking |
No, this will break anyone using the _raw apis. |
Rental assumes that string's derefmut derefs to a stable address. I spoke briefly with the author once; rental has a I'm really eager to see this change, but I think it needs to be epoch gated. |
I thought library changes can't be epoch-specific, because crates of all epochs share the same standard library (and other libraries, for that matter)? |
We can introduce new APIs only in a new epoch; in this case |
Can't such a String get moved into a library that is written against the 2015 epoch though? |
Absolutely. To be clearer, I don't think we need an epoch to change this (from a legalistic perspective). Rental is currently making an assumption that Often, we will say our hands are tied by the unsafe code that exists and is relying on this thing, so we now guarantee this thing we didn't originally say we would. I don't think that needs to be the case here, since the guarantee failing can only be observed in very particular edge cases - assuming that calling However, I'd like to very clearly and loudly message to users about the fact that these assumptions about String are now invalidated. One way to do that is have it be one of the changes we're making in the epoch transition. So I'd like to use the epoch as a social force in amplifying information about this potential incorrect assumption that as far as we know no one is actually relying on. |
☔ The latest upstream changes (presumably #46952) made this pull request unmergeable. Please resolve the merge conflicts. |
I'm going to unilaterally move this to be nominated for discussion by the team, otherwise it seems like this will just languish forever. (or you know, what @aturon said in response to me earlier that I forgot about...) |
@shepmaster I'm going to go a step further and close this PR -- not because we don't want it, but because the next step is an RFC. I don't have the bandwidth to write such an RFC myself, but I'd really like to see this at least attempted. Is there anyone who would like to take on an RFC with me mentoring/serving as backup? |
I can do an RFC, if Gankro doesn't want to.
But if someone else wants to be mentored it's better if they do it (I can
help mentor).
On Jan 27, 2018 10:47 PM, "Aaron Turon" <[email protected]> wrote:
@shepmaster <https://github.com/shepmaster> I'm going to go a step further
and *close* this PR -- not because we don't want it, but because the next
step is an RFC.
I don't have the bandwidth to write such an RFC myself, but I'd really like
to see this at least attempted. Is there anyone who would like to take on
an RFC with me mentoring/serving as backup?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#46993 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABivSC4dRuAXJLARGdZrb8f-0I0JImLfks5tO1o1gaJpZM4RMFN5>
.
|
I don't have any interest in working on an RFC. I made this PoC so ergonomics people couldn't complain to me about implementation issues anymore. Yer problem now! |
I'm interested in shepherding (but not writing) an RFC that adds this as a coercion (rather than the |
Why not both a coercion and an explicit contructor? Coercion sounds like it’d work from any |
I don't really have an objection to having a constructor. I laid out an argument in favor of epoch gating a few posts up. More generally these seem like good questions for an RFC thread 😉. |
This is a proof of concept implementation for people who want to test this out. It isn't fully tested, and the RawVec changes probably need more work. In addition the implementation isn't as optimized as it could be (see FIXME's).
This adds a
String::literally
constructor that wraps up a literal without allocating. It in turn makes Vec partially support this (with more robust capacity checking) and RawVec partially support this (by copying the source if len > cap). String also gains some make_unique guards for methods which don't naturally do this themselves (ones which make mutable views or mutate the middle of the buffer).