about summary refs log tree commit diff
diff options
context:
space:
mode:
authordjzin <noreply@github.com>2016-12-23 19:15:56 +0000
committerdjzin <noreply@github.com>2017-01-14 16:51:50 +0000
commit35f23e8211147372f1e8917f7b41593a1aec9865 (patch)
tree87c5f9b99a705a6ccd648ea6553d6a75176ab970
parentef04fc82b198487b0ffb1832304714519f9f1a35 (diff)
downloadrust-35f23e8211147372f1e8917f7b41593a1aec9865.tar.gz
rust-35f23e8211147372f1e8917f7b41593a1aec9865.zip
have RangeArgument return a Bound<&T> from each of its methods
-rw-r--r--src/libcollections/range.rs25
-rw-r--r--src/libcollections/string.rs13
-rw-r--r--src/libcollections/vec.rs13
-rw-r--r--src/libcollections/vec_deque.rs13
4 files changed, 46 insertions, 18 deletions
diff --git a/src/libcollections/range.rs b/src/libcollections/range.rs
index d331ead2c5e..06ddcd13d55 100644
--- a/src/libcollections/range.rs
+++ b/src/libcollections/range.rs
@@ -15,6 +15,7 @@
 //! Range syntax.
 
 use core::ops::{RangeFull, Range, RangeTo, RangeFrom};
+use Bound::{self, Excluded, Included, Unbounded};
 
 /// **RangeArgument** is implemented by Rust's built-in range types, produced
 /// by range syntax like `..`, `a..`, `..b` or `c..d`.
@@ -38,8 +39,8 @@ pub trait RangeArgument<T> {
     /// assert_eq!((3..10).start(), Some(&3));
     /// # }
     /// ```
-    fn start(&self) -> Option<&T> {
-        None
+    fn start(&self) -> Bound<&T> {
+        Unbounded
     }
 
     /// End index (exclusive)
@@ -61,8 +62,8 @@ pub trait RangeArgument<T> {
     /// assert_eq!((3..10).end(), Some(&10));
     /// # }
     /// ```
-    fn end(&self) -> Option<&T> {
-        None
+    fn end(&self) -> Bound<&T> {
+        Unbounded
     }
 }
 
@@ -71,22 +72,22 @@ pub trait RangeArgument<T> {
 impl<T> RangeArgument<T> for RangeFull {}
 
 impl<T> RangeArgument<T> for RangeFrom<T> {
-    fn start(&self) -> Option<&T> {
-        Some(&self.start)
+    fn start(&self) -> Bound<&T> {
+        Included(&self.start)
     }
 }
 
 impl<T> RangeArgument<T> for RangeTo<T> {
-    fn end(&self) -> Option<&T> {
-        Some(&self.end)
+    fn end(&self) -> Bound<&T> {
+        Excluded(&self.end)
     }
 }
 
 impl<T> RangeArgument<T> for Range<T> {
-    fn start(&self) -> Option<&T> {
-        Some(&self.start)
+    fn start(&self) -> Bound<&T> {
+        Included(&self.start)
     }
-    fn end(&self) -> Option<&T> {
-        Some(&self.end)
+    fn end(&self) -> Bound<&T> {
+        Excluded(&self.end)
     }
 }
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs
index 157c762b4a7..5210c25b4e5 100644
--- a/src/libcollections/string.rs
+++ b/src/libcollections/string.rs
@@ -68,6 +68,7 @@ use std_unicode::str as unicode_str;
 
 use borrow::{Cow, ToOwned};
 use range::RangeArgument;
+use Bound::{Excluded, Included, Unbounded};
 use str::{self, FromStr, Utf8Error, Chars};
 use vec::Vec;
 use boxed::Box;
@@ -1350,8 +1351,16 @@ impl String {
         // Because the range removal happens in Drop, if the Drain iterator is leaked,
         // the removal will not happen.
         let len = self.len();
-        let start = *range.start().unwrap_or(&0);
-        let end = *range.end().unwrap_or(&len);
+        let start = match range.start() {
+            Included(&n) => n,
+            Excluded(&n) => n + 1,
+            Unbounded => 0,
+        };
+        let end = match range.end() {
+            Included(&n) => n + 1,
+            Excluded(&n) => n,
+            Unbounded => len,
+        };
 
         // Take out two simultaneous borrows. The &mut String won't be accessed
         // until iteration is over, in Drop.
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index 27cf0268c99..4b05f8062e8 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -84,6 +84,7 @@ use core::ptr::Shared;
 use core::slice;
 
 use super::range::RangeArgument;
+use Bound::{Excluded, Included, Unbounded};
 
 /// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
 ///
@@ -1060,8 +1061,16 @@ impl<T> Vec<T> {
         // the hole, and the vector length is restored to the new length.
         //
         let len = self.len();
-        let start = *range.start().unwrap_or(&0);
-        let end = *range.end().unwrap_or(&len);
+        let start = match range.start() {
+            Included(&n) => n,
+            Excluded(&n) => n + 1,
+            Unbounded    => 0,
+        };
+        let end = match range.end() {
+            Included(&n) => n + 1,
+            Excluded(&n) => n,
+            Unbounded    => len,
+        };
         assert!(start <= end);
         assert!(end <= len);
 
diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs
index 76e44c81579..fea2d111f47 100644
--- a/src/libcollections/vec_deque.rs
+++ b/src/libcollections/vec_deque.rs
@@ -33,6 +33,7 @@ use core::cmp;
 use alloc::raw_vec::RawVec;
 
 use super::range::RangeArgument;
+use Bound::{Excluded, Included, Unbounded};
 use super::vec::Vec;
 
 const INITIAL_CAPACITY: usize = 7; // 2^3 - 1
@@ -852,8 +853,16 @@ impl<T> VecDeque<T> {
         // and the head/tail values will be restored correctly.
         //
         let len = self.len();
-        let start = *range.start().unwrap_or(&0);
-        let end = *range.end().unwrap_or(&len);
+        let start = match range.start() {
+            Included(&n) => n,
+            Excluded(&n) => n + 1,
+            Unbounded    => 0,
+        };
+        let end = match range.end() {
+            Included(&n) => n + 1,
+            Excluded(&n) => n,
+            Unbounded    => len,
+        };
         assert!(start <= end, "drain lower bound was too large");
         assert!(end <= len, "drain upper bound was too large");