Skip to content

Commit

Permalink
Remove Rng.choose(), rename Rng.choose_option() to .choose()
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
lilyball committed May 21, 2014
1 parent e546452 commit b8270c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/libflate/lib.rs
Expand Up @@ -125,7 +125,7 @@ mod tests {
for _ in range(0, 20) {
let mut input = vec![];
for _ in range(0, 2000) {
input.push_all(r.choose(words.as_slice()).as_slice());
input.push_all(r.choose(words.as_slice()).unwrap().as_slice());
}
debug!("de/inflate of {} bytes of random word-sequences",
input.len());
Expand Down
38 changes: 16 additions & 22 deletions src/librand/lib.rs
Expand Up @@ -266,37 +266,39 @@ pub trait Rng {
0123456789");
let mut s = StrBuf::with_capacity(len);
for _ in range(0, len) {
s.push_char(self.choose(GEN_ASCII_STR_CHARSET) as char)
s.push_char(*self.choose(GEN_ASCII_STR_CHARSET).unwrap() as char)
}
s
}

/// Choose an item randomly, failing if `values` is empty.
fn choose<T: Clone>(&mut self, values: &[T]) -> T {
self.choose_option(values).expect("Rng.choose: `values` is empty").clone()
}

/// Choose `Some(&item)` randomly, returning `None` if values is
/// empty.
/// Return a random element from `values`.
///
/// Return `None` if `values` is empty.
///
/// # Example
///
/// ```rust
/// ```
/// use rand::{task_rng, Rng};
///
/// let choices = [1, 2, 4, 8, 16, 32];
/// let mut rng = task_rng();
/// println!("{:?}", rng.choose_option(choices));
/// println!("{:?}", rng.choose_option(choices.slice_to(0)));
/// println!("{}", rng.choose(choices));
/// assert_eq!(rng.choose(choices.slice_to(0)), None);
/// ```
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
if values.is_empty() {
None
} else {
Some(&values[self.gen_range(0u, values.len())])
}
}

/// Deprecated name for `choose()`.
#[deprecated = "replaced by .choose()"]
fn choose_option<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> {
self.choose(values)
}

/// Shuffle a mutable slice in place.
///
/// # Example
Expand Down Expand Up @@ -793,18 +795,10 @@ mod test {
#[test]
fn test_choose() {
let mut r = task_rng();
assert_eq!(r.choose([1, 1, 1]), 1);
}
assert_eq!(r.choose([1, 1, 1]).map(|&x|x), Some(1));

#[test]
fn test_choose_option() {
let mut r = task_rng();
let v: &[int] = &[];
assert!(r.choose_option(v).is_none());

let i = 1;
let v = [1,1,1];
assert_eq!(r.choose_option(v), Some(&i));
assert_eq!(r.choose(v), None);
}

#[test]
Expand Down

5 comments on commit b8270c8

@bors
Copy link
Contributor

@bors bors commented on b8270c8 May 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at lilyball@b8270c8

@bors
Copy link
Contributor

@bors bors commented on b8270c8 May 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kballard/rust/rename_rng_choose_option = b8270c8 into auto

@bors
Copy link
Contributor

@bors bors commented on b8270c8 May 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kballard/rust/rename_rng_choose_option = b8270c8 merged ok, testing candidate = a400b31

@bors
Copy link
Contributor

@bors bors commented on b8270c8 May 21, 2014

@bors
Copy link
Contributor

@bors bors commented on b8270c8 May 21, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = a400b31

Please sign in to comment.