Skip to content

Remove Rng.choose(), rename Rng.choose_option() to .choose() #14319

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

Merged
merged 1 commit into from
May 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libflate/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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