Skip to content
New issue

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

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

Already on GitHub? # to your account

Add repeat method on slice #48999

Merged
merged 2 commits into from
Apr 24, 2018
Merged

Conversation

GuillaumeGomez
Copy link
Member

Fixes #48784.

@rust-highfive
Copy link
Collaborator

r? @Kimundi

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 13, 2018

// `2^expn` repetition is done by doubling `buf` `expn`-times.
buf.extend(self);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sinkuu
Copy link
Contributor

sinkuu commented Mar 14, 2018

Can you replace the implementation of str::repeat with String::from_utf8_unchecked(self.as_bytes().repeat(n)) to avoid code duplication?

@GuillaumeGomez
Copy link
Member Author

@sinkuu: That's what I thought about doing but they seem to make specific str operations (checking if they're at the end of a char, etc...). Did I misunderstood?

@sinkuu
Copy link
Contributor

sinkuu commented Mar 14, 2018

@GuillaumeGomez Valid Strings must have char boundary at their ends, so repeating them does not require special considerations.
Just like String::push_str simply extends their internal Vec<u8> with the other.
https://doc.rust-lang.org/1.22.0/src/alloc/string.rs.html#804-808


/// Creates a [`Vec`] by repeating a slice `n` times.
///
/// [`Vec`]: ../../std/vec/struct.Vec.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[01:26:06] std/primitive.slice.html:1018: broken link - /checkout/obj/build/x86_64-unknown-linux-gnu/std/vec/struct.Vec.html
[01:26:20] thread 'main' panicked at 'found some broken links', tools/linkchecker/main.rs:49:9

// first `rem` repetitions from `buf` itself.
let rem_len = self.len() * n - buf.len(); // `self.len() * rem`
if rem_len > 0 {
// `buf.extend(buf[0 .. rem_len])`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why is the code here not just literally buf.extend(&buf[..rem_len])? Worse codegen?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you cannot have &mut buf and &buf at the same time. The comment was meant to be like a pseudocode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course. I somehow forgot about the borrow checker for a minute there, too much C++ recently...

// `n = 2^expn + rem (2^expn > rem, expn >= 0, rem >= 0)`.
// `2^expn` is the number represented by the leftmost '1' bit of `n`,
// and `rem` is the remaining part of `n`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a few minutes to figure out why this function is so complex - maybe add a comment that explicitly explains that its trying to minimize the copy_nonoverlapping calls by always copying as large chunks as possible?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just moved the code around, didn't modify it. It's exactly like it was before in str.

@GuillaumeGomez GuillaumeGomez force-pushed the add-repeat-on-slice branch 2 times, most recently from 4fc6276 to 234375b Compare March 15, 2018 23:14
@GuillaumeGomez
Copy link
Member Author

It passed tests. Is there anything else I should do?

/// ```
/// assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]);
/// ```
#[stable(feature = "repeat_slice", since = "1.27.0")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All new APIs should start off as unstable. Just invent a new feature name for it, like "repeat_generic_slice" or so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

@GuillaumeGomez
Copy link
Member Author

Updated.

@Kimundi
Copy link
Member

Kimundi commented Mar 19, 2018

Ah, sorry I didn't notice their absence before, but if you could add some basic tests that check that repeat works as expected on a few different slice types that would be good. (Even if its the same code, just moved around, there might always be something unexpected going on)

@GuillaumeGomez
Copy link
Member Author

Sure. I'll add them later in the week.

@shepmaster shepmaster added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 23, 2018
@GuillaumeGomez
Copy link
Member Author

Added a test.

Copy link
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I mentioned this on IRC yesterday, and someone tried it out and got really excited that it was like 90x faster than what they were doing before 😄

///
/// Basic usage:
///
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[01:10:05] ---- slice.rs - slice::[T]::repeat (line 1732) stdout ----
[01:10:05] 	error[E0658]: use of unstable library feature 'repeat_generic_slice': it's on str, why not on slice? (see issue #48784)
[01:10:05]  --> slice.rs:1733:19
[01:10:05]   |
[01:10:05] 4 | assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]);
[01:10:05]   |                   ^^^^^^
[01:10:05]   |
[01:10:05]   = help: add #![feature(repeat_generic_slice)] to the crate attributes to enable

@GuillaumeGomez
Copy link
Member Author

Fixed doc example.

@GuillaumeGomez
Copy link
Member Author

cc @Kimundi

@scottmcm scottmcm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 5, 2018
@pietroalbini
Copy link
Member

Ping from triage @Kimundi! The author pushed new commits, can you review this PR again?

@GuillaumeGomez
Copy link
Member Author

Anyone?

@pietroalbini
Copy link
Member

Can someone from @rust-lang/libs review this PR? Thanks!

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 16, 2018
@pietroalbini
Copy link
Member

Ping from triage! Can @Kimundi (or someone else from @rust-lang/libs) review this?

@Kimundi
Copy link
Member

Kimundi commented Apr 23, 2018

@bors: r+

@bors
Copy link
Contributor

bors commented Apr 23, 2018

📌 Commit 3c1fea9 has been approved by Kimundi

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 23, 2018
@bors
Copy link
Contributor

bors commented Apr 23, 2018

⌛ Testing commit 3c1fea9 with merge bc1114ed3ed5e533ce9d4a1efb807e9753fdc38d...

@bors
Copy link
Contributor

bors commented Apr 23, 2018

💔 Test failed - status-travis

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 23, 2018
@GuillaumeGomez
Copy link
Member Author

I can't see which test failed. Just that we got a signal 11...

@Mark-Simulacrum
Copy link
Member

Possibly a resurgence of #49775? cc @alexcrichton

But probably a spurious failure, seems somewhat unlikely to be caused by these changes.

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 24, 2018
@bors
Copy link
Contributor

bors commented Apr 24, 2018

⌛ Testing commit 3c1fea9 with merge f305b02...

bors added a commit that referenced this pull request Apr 24, 2018
@bors
Copy link
Contributor

bors commented Apr 24, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: Kimundi
Pushing f305b02 to master...

@bors bors merged commit 3c1fea9 into rust-lang:master Apr 24, 2018
@GuillaumeGomez GuillaumeGomez deleted the add-repeat-on-slice branch April 24, 2018 08:42
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants