about summary refs log tree commit diff
diff options
context:
space:
mode:
authorKevin Ballard <kevin@sb.org>2014-05-20 20:44:45 -0700
committerKevin Ballard <kevin@sb.org>2014-05-20 20:44:45 -0700
commitb8270c8db28edd56ace60141c36a9e6aa380b9da (patch)
tree600c28b85b48897c358419587321611f297273b1
parente546452727379f701f2104eb826141a29d4b39fd (diff)
downloadrust-b8270c8db28edd56ace60141c36a9e6aa380b9da.tar.gz
rust-b8270c8db28edd56ace60141c36a9e6aa380b9da.zip
Remove Rng.choose(), rename Rng.choose_option() to .choose()
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]
-rw-r--r--src/libflate/lib.rs2
-rw-r--r--src/librand/lib.rs38
2 files changed, 17 insertions, 23 deletions
diff --git a/src/libflate/lib.rs b/src/libflate/lib.rs
index 03050794d21..76d803cdacc 100644
--- a/src/libflate/lib.rs
+++ b/src/libflate/lib.rs
@@ -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());
diff --git a/src/librand/lib.rs b/src/librand/lib.rs
index 5b71d5c4da7..c7a29ff7285 100644
--- a/src/librand/lib.rs
+++ b/src/librand/lib.rs
@@ -266,30 +266,26 @@ 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 {
@@ -297,6 +293,12 @@ pub trait Rng {
         }
     }
 
+    /// 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
@@ -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]