diff --git a/library/alloc/src/collections/vec_deque.rs b/library/alloc/src/collections/vec_deque.rs index 0cb8d7a891a0d..4d6c681e44c1c 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 002c770277970..512a4a1cc1b44 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 26124e301115a..7b0ec1c43c0a4 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 e668b17c46c0e..90c2708b9c9b2 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 d9420616b4bc6..16d86c8197803 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 5ad57b23c4a40..43185bae3daec 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 0000000000000..0e95d5ded9296 --- /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 47a1bd8dff1b8..0000000000000 --- 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