Skip to content

Commit

Permalink
Reimplemented String::extend with &mut Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
1Git2Clone committed Feb 15, 2025
1 parent baca091 commit c9093cb
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/distr/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,14 @@ impl SampleString for Alphanumeric {
#[cfg(feature = "alloc")]
impl SampleString for Alphabetic {
fn append_string<R: Rng + ?Sized>(&self, rng: &mut R, string: &mut String, len: usize) {
string.extend(self.sample_iter(rng).take(len).map(char::from));
// SAFETY: With this distribution we guarantee that we're working with valid ASCII
// characters.
// See [#1590](https://github.com/rust-random/rand/issues/1590).
unsafe {
let v = string.as_mut_vec();
v.reserve_exact(len);
v.extend(self.sample_iter(rng).take(len));
}
}
}

Expand Down

0 comments on commit c9093cb

Please sign in to comment.