-
Notifications
You must be signed in to change notification settings - Fork 13k
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
repeat
method for (byte) slices?
#48784
Comments
Possibly generalized (or not, but let's entertain the notion...) pub trait Repeat {
type Repeated;
fn repeat(&self, times: usize) -> Self::Repeated;
} |
Seems reasonable to add this to |
Doesn't the general one exist already? That said, #48657 could easily be moved to |
@scottmcm What's the efficiency of that one like? I bet it's nothing close to |
@alexreg That's what I meant by my second paragraph. |
@scottmcm Yeah, that's certainly a fair idea. Do you know if |
We can implement it for |
@sfackler Yep, that sounds like a good solution to be honest. Even if it's no more efficient that the iterator approach for |
@alexreg Unfortunately I know for sure that literally |
@scottmcm Fair enough, though I think in that case why might as well provide that as a default fn on the |
Add repeat method on slice Fixes #48784.
@rfcbot poll @rust-lang/libs I'd like to stabilize The signature in #48999 is fn repeat(&self, n: usize) -> Vec<T> where T: Copy; and the implementation uses on
OTOH it is really simple to extend the bound to // note: private trait
trait RepeatSlice: Sized {
fn repeat_slice(slice: &[Self], n: usize) -> Vec<Self>;
}
impl<T: Clone> RepeatSlice for T {
default fn repeat_slice(slice: &[Self], n: usize) -> Vec<Self> {
let mut res = Vec::with_capacity(n * slice.len());
for _ in 0..n {
res.extend_from_slice(slice);
}
res
}
}
impl<T: Copy> RepeatSlice for T {
fn repeat_slice(slice: &[Self], n: usize) -> Vec<Self> {
// the original copy_nonoverlapping implementation
}
}
impl<T> [T] {
pub fn repeat(&self, n: usize) -> Vec<T>
where
T: Clone,
{
T::repeat_slice(self, n)
}
} |
Team member @kennytm has asked teams: T-libs, for consensus on:
|
From the implementation: // If `n` is larger than zero, it can be split as
// `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`. // `2^expn` repetition is done by doubling `buf` `expn`-times. I guess that it’s this way because fewer calls to Do we have evidence one way or another to justify such complex code? |
Is there anything blocking stabilization here? It looks like @SimonSapin's comment above here is "just" an implementation nit. |
@SimonSapin The code came from |
Right, my comment should not be taken as blocking stabilization. It’s purely an implementation detail and can change later. @dtolnay @withoutboats @sfackler This pFCP awaits your checkboxes above. |
@sfackler @withoutboats I came across this again and read through the comments to find nothing blocking stabilization other than your votes which have been absent for 9 months. It would seem prudent to comment before it is further forgotten. |
#48784 (comment) is a poll, not a pFCP. These days rfcbot starts FCP when it has not more than two check boxes missing, so I think this one should be considered accepted. Feel free to send a stabilization PR. |
I confirmed that run-time of
|
@sinkuu Would you be interested in working on a PR to improve the implementation? |
@SimonSapin Yes, I'll try to add the fallback to the old impl for large parameters. |
It may be worth looking at the number of bytes being copied per loop iteration, rather than simply the number of repetitions. |
There exists a
str::repeat
method, but no equivalent for (byte) slices. This would seem like a useful feature. Should we consider adding it to the stdlib?The text was updated successfully, but these errors were encountered: