Skip to content

Commit f055b0b

Browse files
committed
Rename method to assert_len
1 parent eb63168 commit f055b0b

File tree

8 files changed

+57
-57
lines changed

8 files changed

+57
-57
lines changed

library/alloc/src/collections/vec_deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ impl<T> VecDeque<T> {
10891089
where
10901090
R: RangeBounds<usize>,
10911091
{
1092-
let Range { start, end } = range.for_length(self.len());
1092+
let Range { start, end } = range.assert_len(self.len());
10931093
let tail = self.wrap_add(self.tail, start);
10941094
let head = self.wrap_add(self.tail, end);
10951095
(tail, head)

library/alloc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
#![feature(or_patterns)]
117117
#![feature(pattern)]
118118
#![feature(ptr_internals)]
119-
#![feature(range_bounds_for_length)]
119+
#![feature(range_bounds_assert_len)]
120120
#![feature(raw_ref_op)]
121121
#![feature(rustc_attrs)]
122122
#![feature(receiver_trait)]

library/alloc/src/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1506,14 +1506,14 @@ impl String {
15061506
// of the vector version. The data is just plain bytes.
15071507
// Because the range removal happens in Drop, if the Drain iterator is leaked,
15081508
// the removal will not happen.
1509-
let Range { start, end } = range.for_length(self.len());
1509+
let Range { start, end } = range.assert_len(self.len());
15101510
assert!(self.is_char_boundary(start));
15111511
assert!(self.is_char_boundary(end));
15121512

15131513
// Take out two simultaneous borrows. The &mut String won't be accessed
15141514
// until iteration is over, in Drop.
15151515
let self_ptr = self as *mut _;
1516-
// SAFETY: `for_length` and `is_char_boundary` do the appropriate bounds checks.
1516+
// SAFETY: `assert_len` and `is_char_boundary` do the appropriate bounds checks.
15171517
let chars_iter = unsafe { self.get_unchecked(start..end) }.chars();
15181518

15191519
Drain { start, end, iter: chars_iter, string: self_ptr }

library/alloc/src/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ impl<T> Vec<T> {
13121312
// the hole, and the vector length is restored to the new length.
13131313
//
13141314
let len = self.len();
1315-
let Range { start, end } = range.for_length(len);
1315+
let Range { start, end } = range.assert_len(len);
13161316

13171317
unsafe {
13181318
// set self.vec length's to start, to be safe in case Drain is leaked

library/core/src/ops/range.rs

+41-41
Original file line numberDiff line numberDiff line change
@@ -705,35 +705,6 @@ pub trait RangeBounds<T: ?Sized> {
705705
#[stable(feature = "collections_range", since = "1.28.0")]
706706
fn end_bound(&self) -> Bound<&T>;
707707

708-
/// Returns `true` if `item` is contained in the range.
709-
///
710-
/// # Examples
711-
///
712-
/// ```
713-
/// assert!( (3..5).contains(&4));
714-
/// assert!(!(3..5).contains(&2));
715-
///
716-
/// assert!( (0.0..1.0).contains(&0.5));
717-
/// assert!(!(0.0..1.0).contains(&f32::NAN));
718-
/// assert!(!(0.0..f32::NAN).contains(&0.5));
719-
/// assert!(!(f32::NAN..1.0).contains(&0.5));
720-
#[stable(feature = "range_contains", since = "1.35.0")]
721-
fn contains<U>(&self, item: &U) -> bool
722-
where
723-
T: PartialOrd<U>,
724-
U: ?Sized + PartialOrd<T>,
725-
{
726-
(match self.start_bound() {
727-
Included(ref start) => *start <= item,
728-
Excluded(ref start) => *start < item,
729-
Unbounded => true,
730-
}) && (match self.end_bound() {
731-
Included(ref end) => item <= *end,
732-
Excluded(ref end) => item < *end,
733-
Unbounded => true,
734-
})
735-
}
736-
737708
/// Performs bounds-checking of this range.
738709
///
739710
/// The returned [`Range`] is safe to pass to [`slice::get_unchecked`] and
@@ -749,46 +720,46 @@ pub trait RangeBounds<T: ?Sized> {
749720
/// # Examples
750721
///
751722
/// ```
752-
/// #![feature(range_bounds_for_length)]
723+
/// #![feature(range_bounds_assert_len)]
753724
///
754725
/// use std::ops::RangeBounds;
755726
///
756727
/// let v = [10, 40, 30];
757-
/// assert_eq!(1..2, (1..2).for_length(v.len()));
758-
/// assert_eq!(0..2, (..2).for_length(v.len()));
759-
/// assert_eq!(1..3, (1..).for_length(v.len()));
728+
/// assert_eq!(1..2, (1..2).assert_len(v.len()));
729+
/// assert_eq!(0..2, (..2).assert_len(v.len()));
730+
/// assert_eq!(1..3, (1..).assert_len(v.len()));
760731
/// ```
761732
///
762733
/// Panics when [`Index::index`] would panic:
763734
///
764735
/// ```should_panic
765-
/// #![feature(range_bounds_for_length)]
736+
/// #![feature(range_bounds_assert_len)]
766737
///
767738
/// use std::ops::RangeBounds;
768739
///
769-
/// (2..1).for_length(3);
740+
/// (2..1).assert_len(3);
770741
/// ```
771742
///
772743
/// ```should_panic
773-
/// #![feature(range_bounds_for_length)]
744+
/// #![feature(range_bounds_assert_len)]
774745
///
775746
/// use std::ops::RangeBounds;
776747
///
777-
/// (1..4).for_length(3);
748+
/// (1..4).assert_len(3);
778749
/// ```
779750
///
780751
/// ```should_panic
781-
/// #![feature(range_bounds_for_length)]
752+
/// #![feature(range_bounds_assert_len)]
782753
///
783754
/// use std::ops::RangeBounds;
784755
///
785-
/// (1..=usize::MAX).for_length(3);
756+
/// (1..=usize::MAX).assert_len(3);
786757
/// ```
787758
///
788759
/// [`Index::index`]: crate::ops::Index::index
789760
#[track_caller]
790-
#[unstable(feature = "range_bounds_for_length", issue = "76393")]
791-
fn for_length(self, len: usize) -> Range<usize>
761+
#[unstable(feature = "range_bounds_assert_len", issue = "76393")]
762+
fn assert_len(self, len: usize) -> Range<usize>
792763
where
793764
Self: RangeBounds<usize>,
794765
{
@@ -819,6 +790,35 @@ pub trait RangeBounds<T: ?Sized> {
819790

820791
Range { start, end }
821792
}
793+
794+
/// Returns `true` if `item` is contained in the range.
795+
///
796+
/// # Examples
797+
///
798+
/// ```
799+
/// assert!( (3..5).contains(&4));
800+
/// assert!(!(3..5).contains(&2));
801+
///
802+
/// assert!( (0.0..1.0).contains(&0.5));
803+
/// assert!(!(0.0..1.0).contains(&f32::NAN));
804+
/// assert!(!(0.0..f32::NAN).contains(&0.5));
805+
/// assert!(!(f32::NAN..1.0).contains(&0.5));
806+
#[stable(feature = "range_contains", since = "1.35.0")]
807+
fn contains<U>(&self, item: &U) -> bool
808+
where
809+
T: PartialOrd<U>,
810+
U: ?Sized + PartialOrd<T>,
811+
{
812+
(match self.start_bound() {
813+
Included(ref start) => *start <= item,
814+
Excluded(ref start) => *start < item,
815+
Unbounded => true,
816+
}) && (match self.end_bound() {
817+
Included(ref end) => item <= *end,
818+
Excluded(ref end) => item < *end,
819+
Unbounded => true,
820+
})
821+
}
822822
}
823823

824824
use self::Bound::{Excluded, Included, Unbounded};

library/core/src/slice/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,7 @@ impl<T> [T] {
27552755
where
27562756
T: Copy,
27572757
{
2758-
let Range { start: src_start, end: src_end } = src.for_length(self.len());
2758+
let Range { start: src_start, end: src_end } = src.assert_len(self.len());
27592759
let count = src_end - src_start;
27602760
assert!(dest <= self.len() - count, "dest is out of bounds");
27612761
// SAFETY: the conditions for `ptr::copy` have all been checked above,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# `range_bounds_assert_len`
2+
3+
The tracking issue for this feature is: [#76393]
4+
5+
------------------------
6+
7+
This adds [`RangeBounds::assert_len`].
8+
9+
[#76393]: https://github.com/rust-lang/rust/issues/76393
10+
[`RangeBounds::assert_len`]: https://doc.rust-lang.org/nightly/std/ops/trait.RangeBounds.html#method.assert_len

src/doc/unstable-book/src/library-features/range-bounds-for-length.md

-10
This file was deleted.

0 commit comments

Comments
 (0)