Rename method to `assert_len`
This commit is contained in:
parent
eb63168e00
commit
f055b0bb08
|
@ -1089,7 +1089,7 @@ impl<T> VecDeque<T> {
|
|||
where
|
||||
R: RangeBounds<usize>,
|
||||
{
|
||||
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)
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -1312,7 +1312,7 @@ impl<T> Vec<T> {
|
|||
// 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
|
||||
|
|
|
@ -705,35 +705,6 @@ pub trait RangeBounds<T: ?Sized> {
|
|||
#[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<U>(&self, item: &U) -> bool
|
||||
where
|
||||
T: PartialOrd<U>,
|
||||
U: ?Sized + PartialOrd<T>,
|
||||
{
|
||||
(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<T: ?Sized> {
|
|||
/// # 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<usize>
|
||||
#[unstable(feature = "range_bounds_assert_len", issue = "76393")]
|
||||
fn assert_len(self, len: usize) -> Range<usize>
|
||||
where
|
||||
Self: RangeBounds<usize>,
|
||||
{
|
||||
|
@ -819,6 +790,35 @@ pub trait RangeBounds<T: ?Sized> {
|
|||
|
||||
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<U>(&self, item: &U) -> bool
|
||||
where
|
||||
T: PartialOrd<U>,
|
||||
U: ?Sized + PartialOrd<T>,
|
||||
{
|
||||
(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};
|
||||
|
|
|
@ -2755,7 +2755,7 @@ impl<T> [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,
|
||||
|
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue