From 35f23e8211147372f1e8917f7b41593a1aec9865 Mon Sep 17 00:00:00 2001 From: djzin Date: Fri, 23 Dec 2016 19:15:56 +0000 Subject: [PATCH] have RangeArgument return a Bound<&T> from each of its methods --- src/libcollections/range.rs | 25 +++++++++++++------------ src/libcollections/string.rs | 13 +++++++++++-- src/libcollections/vec.rs | 13 +++++++++++-- src/libcollections/vec_deque.rs | 13 +++++++++++-- 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 { /// 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 @@ fn start(&self) -> Option<&T> { /// assert_eq!((3..10).end(), Some(&10)); /// # } /// ``` - fn end(&self) -> Option<&T> { - None + fn end(&self) -> Bound<&T> { + Unbounded } } @@ -71,22 +72,22 @@ fn end(&self) -> Option<&T> { impl RangeArgument for RangeFull {} impl RangeArgument for RangeFrom { - fn start(&self) -> Option<&T> { - Some(&self.start) + fn start(&self) -> Bound<&T> { + Included(&self.start) } } impl RangeArgument for RangeTo { - fn end(&self) -> Option<&T> { - Some(&self.end) + fn end(&self) -> Bound<&T> { + Excluded(&self.end) } } impl RangeArgument for Range { - 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 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 @@ pub fn drain(&mut self, range: R) -> Drain // 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::slice; use super::range::RangeArgument; +use Bound::{Excluded, Included, Unbounded}; /// A contiguous growable array type, written `Vec` but pronounced 'vector'. /// @@ -1060,8 +1061,16 @@ pub fn drain(&mut self, range: R) -> Drain // 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 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 @@ pub fn drain(&mut self, range: R) -> Drain // 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");