-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Implement Vec::splice and String::splice (RFC 1432) #40434
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I suppose I should duplicate the |
src/libcollections/string.rs
Outdated
/// The given string doesn’t need to be the same length as the range. | ||
/// | ||
/// Note: The element range is removed even if the iterator is not | ||
/// consumed until the end. |
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 contradicted by the comment under "Memory safety" below.
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.
How about
/// Note: The element range is removed when the Splice is dropped,
/// even if the iterator is not consumed until the end.
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.
Looks like this applies to Drain
as well
/// consumed until the end. | ||
/// | ||
/// Note 2: It is unspecified how many elements are removed from the vector, | ||
/// if the `Splice` value is leaked. |
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.
Nit: this comma is unnecessary.
Yeah that sounds better!
…On Fri, Mar 10, 2017 at 10:12 PM, Matt Ickstadt ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/libcollections/string.rs
<#40434 (comment)>:
> @@ -1382,6 +1382,71 @@ impl String {
}
}
+ /// Creates a splicing iterator that removes the specified range in the string,
+ /// replaces with the given string, and yields the removed chars.
+ /// The given string doesn’t need to be the same length as the range.
+ ///
+ /// Note: The element range is removed even if the iterator is not
+ /// consumed until the end.
Looks like this applies to Drain as well
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#40434 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAC3n6KJfprI9F5n14S7h1x_qaNK6TbXks5rkhEPgaJpZM4MaD4H>
.
|
Looks like Drain removes the range and tail right away, and moves the tail
back at drop. So if you leak it, the tail doesn't come back.
…On Fri, Mar 10, 2017 at 10:20 PM, Alex Burka ***@***.***> wrote:
Yeah that sounds better!
On Fri, Mar 10, 2017 at 10:12 PM, Matt Ickstadt ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
> ------------------------------
>
> In src/libcollections/string.rs
> <#40434 (comment)>:
>
> > @@ -1382,6 +1382,71 @@ impl String {
> }
> }
>
> + /// Creates a splicing iterator that removes the specified range in the string,
> + /// replaces with the given string, and yields the removed chars.
> + /// The given string doesn’t need to be the same length as the range.
> + ///
> + /// Note: The element range is removed even if the iterator is not
> + /// consumed until the end.
>
> Looks like this applies to Drain as well
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#40434 (comment)>, or mute
> the thread
> <https://github.com/notifications/unsubscribe-auth/AAC3n6KJfprI9F5n14S7h1x_qaNK6TbXks5rkhEPgaJpZM4MaD4H>
> .
>
|
Thank you for picking this up! |
Let's get this landed once the beta-madness settles down 😉. |
(Feel free to reassign, I just know you're around) |
sorry for being slow to review @mattico! I wanted to be sure to thank you for the PR, and just wanted to give you a heads up that I'll be traveling for the next week so it'll take some time to get around to reviewing this. |
No problem, @alexcrichton. This RFC has waited a year, what's another week or so 😉. |
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.
Ok got a chance to look this over and it looks fantastic to me, thanks @mattico!
Just one minor nit but other than that I'd be ready to r+
impl<'a, 'b> Drop for Splice<'a, 'b> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
let vec = (*self.string).as_mut_vec(); |
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.
Would it be possible for this to use Vec::splice
?
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.
Do you mean using the Vec implementation for String in general, or using Vec::splice() in the string splice drop impl?
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.
In general it may be pretty hard due to char decoding and whatnot, but here in drop it seems like it'd be a one-liner to call the vec impl for splicing I think, right?
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'll look into it after I rebase
☔ The latest upstream changes (presumably #41102) made this pull request unmergeable. Please resolve the merge conflicts. |
50cfe26
to
9878aa0
Compare
I broke something during rebase, I'll ping you once I've fixed it. |
@mattico ok just ping me whenever it's green and I'll take a final pass before r+ |
I found these comments interesting re: sharing drop impl https://github.com/rust-lang/rust/pull/32355/files#r57401834 |
Note to self: add forget() test: https://github.com/rust-lang/rust/pull/32355/files#r57402156 |
I think we don't need to worry about leaks in this case b/c we're already in a destructor and we're just dropping the splice value that we ourselves are creating, so I don't think there's leak issues? If the split iterator is leaked then I think the string is just safely truncated, right? |
Hi @mattico! Friendly ping to keep this on your radar! |
Haven't forgotten about this, just haven't had much time. Will try to get back to this this week sometime. |
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Oh whoops, I just pushed a forget() test, which codifies the current behavior. Technically for Vec it's unspecified what happens if you forget the Splice, but the test will at least make sure it doesn't change behavior accidentally. |
These new tests pass locally so it should be safe to r+ |
@bors: r+ Thanks! |
📌 Commit feae5a0 has been approved by |
⌛ Testing commit feae5a0 with merge 05a2d62... |
💔 Test failed - status-travis |
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: rust-lang#32310 A rebase of rust-lang#32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
@bors: retry |
Implement Vec::splice and String::splice (RFC 1432) RFC: rust-lang/rfcs#1432, tracking issue: #32310 A rebase of #32355 with a few more tests. Let me know if you have any ideas for more tests. cc @SimonSapin
☀️ Test successful - status-appveyor, status-travis |
RFC: rust-lang/rfcs#1432, tracking issue: #32310
A rebase of #32355 with a few more tests.
Let me know if you have any ideas for more tests.
cc @SimonSapin