From f055b0bb0847ecf08fe452a270faae8324b55b05 Mon Sep 17 00:00:00 2001 From: dylni <46035563+dylni@users.noreply.github.com> Date: Fri, 18 Sep 2020 13:55:03 -0400 Subject: [PATCH] Rename method to `assert_len` --- library/alloc/src/collections/vec_deque.rs | 2 +- library/alloc/src/lib.rs | 2 +- library/alloc/src/string.rs | 4 +- library/alloc/src/vec.rs | 2 +- library/core/src/ops/range.rs | 82 +++++++++---------- library/core/src/slice/mod.rs | 2 +- .../range-bounds-assert-len.md | 10 +++ .../range-bounds-for-length.md | 10 --- 8 files changed, 57 insertions(+), 57 deletions(-) create mode 100644 src/doc/unstable-book/src/library-features/range-bounds-assert-len.md delete mode 100644 src/doc/unstable-book/src/library-features/range-bounds-for-length.md diff --git a/library/alloc/src/collections/vec_deque.rs b/library/alloc/src/collections/vec_deque.rs index 0cb8d7a891a..4d6c681e44c 100644 --- a/library/alloc/src/collections/vec_deque.rs +++ b/library/alloc/src/collections/vec_deque.rs @@ -1089,7 +1089,7 @@ impl VecDeque { where R: RangeBounds, { - let Range { start, end } = range.for_length(self.len()); + let Range { start, end } = range.assert_len(self.len()); let tail = self.wrap_add(self.tail, start); let head = self.wrap_add(self.tail, end); (tail, head) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 002c7702779..512a4a1cc1b 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -116,7 +116,7 @@ #![feature(or_patterns)] #![feature(pattern)] #![feature(ptr_internals)] -#![feature(range_bounds_for_length)] +#![feature(range_bounds_assert_len)] #![feature(raw_ref_op)] #![feature(rustc_attrs)] #![feature(receiver_trait)] diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 26124e30111..7b0ec1c43c0 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1506,14 +1506,14 @@ impl String { // of the vector version. The data is just plain bytes. // Because the range removal happens in Drop, if the Drain iterator is leaked, // the removal will not happen. - let Range { start, end } = range.for_length(self.len()); + let Range { start, end } = range.assert_len(self.len()); assert!(self.is_char_boundary(start)); assert!(self.is_char_boundary(end)); // Take out two simultaneous borrows. The &mut String won't be accessed // until iteration is over, in Drop. let self_ptr = self as *mut _; - // SAFETY: `for_length` and `is_char_boundary` do the appropriate bounds checks. + // SAFETY: `assert_len` and `is_char_boundary` do the appropriate bounds checks. let chars_iter = unsafe { self.get_unchecked(start..end) }.chars(); Drain { start, end, iter: chars_iter, string: self_ptr } diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index e668b17c46c..90c2708b9c9 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1312,7 +1312,7 @@ impl Vec { // the hole, and the vector length is restored to the new length. // let len = self.len(); - let Range { start, end } = range.for_length(len); + let Range { start, end } = range.assert_len(len); unsafe { // set self.vec length's to start, to be safe in case Drain is leaked diff --git a/library/core/src/ops/range.rs b/library/core/src/ops/range.rs index d9420616b4b..16d86c81978 100644 --- a/library/core/src/ops/range.rs +++ b/library/core/src/ops/range.rs @@ -705,35 +705,6 @@ pub trait RangeBounds { #[stable(feature = "collections_range", since = "1.28.0")] fn end_bound(&self) -> Bound<&T>; - /// Returns `true` if `item` is contained in the range. - /// - /// # Examples - /// - /// ``` - /// assert!( (3..5).contains(&4)); - /// assert!(!(3..5).contains(&2)); - /// - /// assert!( (0.0..1.0).contains(&0.5)); - /// assert!(!(0.0..1.0).contains(&f32::NAN)); - /// assert!(!(0.0..f32::NAN).contains(&0.5)); - /// assert!(!(f32::NAN..1.0).contains(&0.5)); - #[stable(feature = "range_contains", since = "1.35.0")] - fn contains(&self, item: &U) -> bool - where - T: PartialOrd, - U: ?Sized + PartialOrd, - { - (match self.start_bound() { - Included(ref start) => *start <= item, - Excluded(ref start) => *start < item, - Unbounded => true, - }) && (match self.end_bound() { - Included(ref end) => item <= *end, - Excluded(ref end) => item < *end, - Unbounded => true, - }) - } - /// Performs bounds-checking of this range. /// /// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and @@ -749,46 +720,46 @@ pub trait RangeBounds { /// # Examples /// /// ``` - /// #![feature(range_bounds_for_length)] + /// #![feature(range_bounds_assert_len)] /// /// use std::ops::RangeBounds; /// /// let v = [10, 40, 30]; - /// assert_eq!(1..2, (1..2).for_length(v.len())); - /// assert_eq!(0..2, (..2).for_length(v.len())); - /// assert_eq!(1..3, (1..).for_length(v.len())); + /// assert_eq!(1..2, (1..2).assert_len(v.len())); + /// assert_eq!(0..2, (..2).assert_len(v.len())); + /// assert_eq!(1..3, (1..).assert_len(v.len())); /// ``` /// /// Panics when [`Index::index`] would panic: /// /// ```should_panic - /// #![feature(range_bounds_for_length)] + /// #![feature(range_bounds_assert_len)] /// /// use std::ops::RangeBounds; /// - /// (2..1).for_length(3); + /// (2..1).assert_len(3); /// ``` /// /// ```should_panic - /// #![feature(range_bounds_for_length)] + /// #![feature(range_bounds_assert_len)] /// /// use std::ops::RangeBounds; /// - /// (1..4).for_length(3); + /// (1..4).assert_len(3); /// ``` /// /// ```should_panic - /// #![feature(range_bounds_for_length)] + /// #![feature(range_bounds_assert_len)] /// /// use std::ops::RangeBounds; /// - /// (1..=usize::MAX).for_length(3); + /// (1..=usize::MAX).assert_len(3); /// ``` /// /// [`Index::index`]: crate::ops::Index::index #[track_caller] - #[unstable(feature = "range_bounds_for_length", issue = "76393")] - fn for_length(self, len: usize) -> Range + #[unstable(feature = "range_bounds_assert_len", issue = "76393")] + fn assert_len(self, len: usize) -> Range where Self: RangeBounds, { @@ -819,6 +790,35 @@ pub trait RangeBounds { Range { start, end } } + + /// Returns `true` if `item` is contained in the range. + /// + /// # Examples + /// + /// ``` + /// assert!( (3..5).contains(&4)); + /// assert!(!(3..5).contains(&2)); + /// + /// assert!( (0.0..1.0).contains(&0.5)); + /// assert!(!(0.0..1.0).contains(&f32::NAN)); + /// assert!(!(0.0..f32::NAN).contains(&0.5)); + /// assert!(!(f32::NAN..1.0).contains(&0.5)); + #[stable(feature = "range_contains", since = "1.35.0")] + fn contains(&self, item: &U) -> bool + where + T: PartialOrd, + U: ?Sized + PartialOrd, + { + (match self.start_bound() { + Included(ref start) => *start <= item, + Excluded(ref start) => *start < item, + Unbounded => true, + }) && (match self.end_bound() { + Included(ref end) => item <= *end, + Excluded(ref end) => item < *end, + Unbounded => true, + }) + } } use self::Bound::{Excluded, Included, Unbounded}; diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 5ad57b23c4a..43185bae3da 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2755,7 +2755,7 @@ impl [T] { where T: Copy, { - let Range { start: src_start, end: src_end } = src.for_length(self.len()); + let Range { start: src_start, end: src_end } = src.assert_len(self.len()); let count = src_end - src_start; assert!(dest <= self.len() - count, "dest is out of bounds"); // SAFETY: the conditions for `ptr::copy` have all been checked above, diff --git a/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md b/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md new file mode 100644 index 00000000000..0e95d5ded92 --- /dev/null +++ b/src/doc/unstable-book/src/library-features/range-bounds-assert-len.md @@ -0,0 +1,10 @@ +# `range_bounds_assert_len` + +The tracking issue for this feature is: [#76393] + +------------------------ + +This adds [`RangeBounds::assert_len`]. + +[#76393]: https://github.com/rust-lang/rust/issues/76393 +[`RangeBounds::assert_len`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.assert_len diff --git a/src/doc/unstable-book/src/library-features/range-bounds-for-length.md b/src/doc/unstable-book/src/library-features/range-bounds-for-length.md deleted file mode 100644 index 47a1bd8dff1..00000000000 --- a/src/doc/unstable-book/src/library-features/range-bounds-for-length.md +++ /dev/null @@ -1,10 +0,0 @@ -# `range_bounds_for_length` - -The tracking issue for this feature is: [#76393] - ------------------------- - -This adds [`RangeBounds::for_length`]. - -[#76393]: https://github.com/rust-lang/rust/issues/76393 -[`RangeBounds::for_length`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.for_length