-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add repeat method on slice #48999
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
src/liballoc/slice.rs
Outdated
|
||
// `2^expn` repetition is done by doubling `buf` `expn`-times. | ||
buf.extend(self); | ||
|
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.
Can you replace the implementation of |
@sinkuu: That's what I thought about doing but they seem to make specific |
@GuillaumeGomez Valid |
715de99
to
3a1a6a7
Compare
src/liballoc/slice.rs
Outdated
|
||
/// Creates a [`Vec`] by repeating a slice `n` times. | ||
/// | ||
/// [`Vec`]: ../../std/vec/struct.Vec.html |
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.
[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])`: |
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.
Hm, why is the code here not just literally buf.extend(&buf[..rem_len])
? Worse codegen?
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.
Because you cannot have &mut buf
and &buf
at the same time. The comment was meant to be like a pseudocode.
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.
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`. | ||
|
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 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?
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 just moved the code around, didn't modify it. It's exactly like it was before in str
.
4fc6276
to
234375b
Compare
It passed tests. Is there anything else I should do? |
src/liballoc/slice.rs
Outdated
/// ``` | ||
/// assert_eq!([1, 2].repeat(3), vec![1, 2, 1, 2, 1, 2]); | ||
/// ``` | ||
#[stable(feature = "repeat_slice", since = "1.27.0")] |
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.
All new APIs should start off as unstable. Just invent a new feature name for it, like "repeat_generic_slice" 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!
234375b
to
5ec2a43
Compare
Updated. |
5ec2a43
to
17801c6
Compare
Ah, sorry I didn't notice their absence before, but if you could add some basic tests that check that |
Sure. I'll add them later in the week. |
17801c6
to
d8863d7
Compare
Added a test. |
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.
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: | ||
/// | ||
/// ``` |
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.
[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
d8863d7
to
3c1fea9
Compare
Fixed doc example. |
cc @Kimundi |
Ping from triage @Kimundi! The author pushed new commits, can you review this PR again? |
Anyone? |
Can someone from @rust-lang/libs review this PR? Thanks! |
Ping from triage! Can @Kimundi (or someone else from @rust-lang/libs) review this? |
@bors: r+ |
📌 Commit 3c1fea9 has been approved by |
⌛ Testing commit 3c1fea9 with merge bc1114ed3ed5e533ce9d4a1efb807e9753fdc38d... |
💔 Test failed - status-travis |
I can't see which test failed. Just that we got a signal 11... |
Possibly a resurgence of #49775? cc @alexcrichton But probably a spurious failure, seems somewhat unlikely to be caused by these changes. @bors retry |
Add repeat method on slice Fixes #48784.
☀️ Test successful - status-appveyor, status-travis |
Fixes #48784.