Skip to content

Commit a400b31

Browse files
committed
auto merge of #14319 : kballard/rust/rename_rng_choose_option, r=alexcrichton
Rng.choose() is used so rarely that it doesn't necessitate having two methods, especially since the failing, non-option variant also requires Clone. [breaking-change]
2 parents 082075d + b8270c8 commit a400b31

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/libflate/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ mod tests {
125125
for _ in range(0, 20) {
126126
let mut input = vec![];
127127
for _ in range(0, 2000) {
128-
input.push_all(r.choose(words.as_slice()).as_slice());
128+
input.push_all(r.choose(words.as_slice()).unwrap().as_slice());
129129
}
130130
debug!("de/inflate of {} bytes of random word-sequences",
131131
input.len());

src/librand/lib.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,37 +266,39 @@ pub trait Rng {
266266
0123456789");
267267
let mut s = StrBuf::with_capacity(len);
268268
for _ in range(0, len) {
269-
s.push_char(self.choose(GEN_ASCII_STR_CHARSET) as char)
269+
s.push_char(*self.choose(GEN_ASCII_STR_CHARSET).unwrap() as char)
270270
}
271271
s
272272
}
273273

274-
/// Choose an item randomly, failing if `values` is empty.
275-
fn choose<T: Clone>(&mut self, values: &[T]) -> T {
276-
self.choose_option(values).expect("Rng.choose: `values` is empty").clone()
277-
}
278-
279-
/// Choose `Some(&item)` randomly, returning `None` if values is
280-
/// empty.
274+
/// Return a random element from `values`.
275+
///
276+
/// Return `None` if `values` is empty.
281277
///
282278
/// # Example
283279
///
284-
/// ```rust
280+
/// ```
285281
/// use rand::{task_rng, Rng};
286282
///
287283
/// let choices = [1, 2, 4, 8, 16, 32];
288284
/// let mut rng = task_rng();
289-
/// println!("{:?}", rng.choose_option(choices));
290-
/// println!("{:?}", rng.choose_option(choices.slice_to(0)));
285+
/// println!("{}", rng.choose(choices));
286+
/// assert_eq!(rng.choose(choices.slice_to(0)), None);
291287
/// ```
292-
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
288+
fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
293289
if values.is_empty() {
294290
None
295291
} else {
296292
Some(&values[self.gen_range(0u, values.len())])
297293
}
298294
}
299295

296+
/// Deprecated name for `choose()`.
297+
#[deprecated = "replaced by .choose()"]
298+
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
299+
self.choose(values)
300+
}
301+
300302
/// Shuffle a mutable slice in place.
301303
///
302304
/// # Example
@@ -793,18 +795,10 @@ mod test {
793795
#[test]
794796
fn test_choose() {
795797
let mut r = task_rng();
796-
assert_eq!(r.choose([1, 1, 1]), 1);
797-
}
798+
assert_eq!(r.choose([1, 1, 1]).map(|&x|x), Some(1));
798799

799-
#[test]
800-
fn test_choose_option() {
801-
let mut r = task_rng();
802800
let v: &[int] = &[];
803-
assert!(r.choose_option(v).is_none());
804-
805-
let i = 1;
806-
let v = [1,1,1];
807-
assert_eq!(r.choose_option(v), Some(&i));
801+
assert_eq!(r.choose(v), None);
808802
}
809803

810804
#[test]

0 commit comments

Comments
 (0)