Skip to content

Commit 0610312

Browse files
committed
Alphanumeric samples bytes instead of chars
The corresponds more closely to the internally used types and can be easily converted to a `char` via `From` and `Into`, while being more flexible to use. This is a breaking change.
1 parent 7f321fa commit 0610312

File tree

4 files changed

+7
-12
lines changed

4 files changed

+7
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1919
is supported (#744, #1003). Note that `a` and `b` can no longer be references or SIMD types.
2020
- Replace `AsByteSliceMut` with `Fill` (#940)
2121
- Move alias method for `WeightedIndex` to `rand_distr` (#945)
22+
- `Alphanumeric` samples bytes instead of chars (#935)
2223
- Better NaN handling for `WeightedIndex` (#1005)
2324
- Implement `IntoIterator` for `IndexVec`, replacing the `into_iter` method (#1007)
2425
- Reduce packaged crate size (#983)

src/distributions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub trait Distribution<T> {
166166
/// let v: Vec<f32> = Standard.sample_iter(rng).take(16).collect();
167167
///
168168
/// // String:
169-
/// let s: String = Alphanumeric.sample_iter(rng).take(7).collect();
169+
/// let s: String = Alphanumeric.sample_iter(rng).take(7).map(char::from).collect();
170170
///
171171
/// // Dice-rolling:
172172
/// let die_range = Uniform::new_inclusive(1, 6);

src/distributions/other.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde::{Serialize, Deserialize};
1919

2020
// ----- Sampling distributions -----
2121

22-
/// Sample a `char` or `u8`, uniformly distributed over ASCII letters and numbers:
22+
/// Sample a `u8`, uniformly distributed over ASCII letters and numbers:
2323
/// a-z, A-Z and 0-9.
2424
///
2525
/// # Example
@@ -32,6 +32,7 @@ use serde::{Serialize, Deserialize};
3232
/// let mut rng = thread_rng();
3333
/// let chars: String = iter::repeat(())
3434
/// .map(|()| rng.sample(Alphanumeric))
35+
/// .map(char::from)
3536
/// .take(7)
3637
/// .collect();
3738
/// println!("Random chars: {}", chars);
@@ -81,13 +82,6 @@ impl Distribution<char> for Standard {
8182
}
8283
}
8384

84-
impl Distribution<char> for Alphanumeric {
85-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> char {
86-
let byte: u8 = self.sample(rng);
87-
byte as char
88-
}
89-
}
90-
9185
impl Distribution<u8> for Alphanumeric {
9286
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u8 {
9387
const RANGE: u32 = 26 + 26 + 10;
@@ -245,7 +239,7 @@ mod tests {
245239
// take the rejection sampling path.
246240
let mut incorrect = false;
247241
for _ in 0..100 {
248-
let c = rng.sample(Alphanumeric);
242+
let c: char = rng.sample(Alphanumeric).into();
249243
incorrect |= !((c >= '0' && c <= '9') ||
250244
(c >= 'A' && c <= 'Z') ||
251245
(c >= 'a' && c <= 'z') );
@@ -273,7 +267,7 @@ mod tests {
273267
'\u{ed692}',
274268
'\u{35888}',
275269
]);
276-
test_samples(&Alphanumeric, 'a', &['h', 'm', 'e', '3', 'M']);
270+
test_samples(&Alphanumeric, 0, &[104, 109, 101, 51, 77]);
277271
test_samples(&Standard, false, &[true, true, false, true, false]);
278272
test_samples(&Standard, None as Option<bool>, &[
279273
Some(true),

src/rng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub trait Rng: RngCore {
171171
/// let v: Vec<f32> = rng.sample_iter(Standard).take(16).collect();
172172
///
173173
/// // String:
174-
/// let s: String = rng.sample_iter(Alphanumeric).take(7).collect();
174+
/// let s: String = rng.sample_iter(Alphanumeric).take(7).map(char::from).collect();
175175
///
176176
/// // Combined values
177177
/// println!("{:?}", rng.sample_iter(Standard).take(5)

0 commit comments

Comments
 (0)