Skip to content

Commit c0b4440

Browse files
committed
Implement TrustedLen for ranges of all integer types
1 parent 9d4dc29 commit c0b4440

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/libcore/iter/range.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ macro_rules! step_integer_impls {
6161
impl Step for $narrower_unsigned {
6262
#[inline]
6363
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
64+
// NOTE: the safety of `unsafe impl TrustedLen` depends on
65+
// this being correct!
6466
if *start < *end {
6567
// This relies on $narrower_unsigned <= usize
6668
Some((*end - *start) as usize)
@@ -92,6 +94,8 @@ macro_rules! step_integer_impls {
9294
impl Step for $narrower_signed {
9395
#[inline]
9496
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
97+
// NOTE: the safety of `unsafe impl TrustedLen` depends on
98+
// this being correct!
9599
if *start < *end {
96100
// This relies on $narrower_signed <= usize
97101
//
@@ -156,6 +160,8 @@ macro_rules! step_integer_impls {
156160
impl Step for $wider_unsigned {
157161
#[inline]
158162
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
163+
// NOTE: the safety of `unsafe impl TrustedLen` depends on
164+
// this being correct!
159165
if *start < *end {
160166
usize::try_from(*end - *start).ok()
161167
} else {
@@ -180,6 +186,8 @@ macro_rules! step_integer_impls {
180186
impl Step for $wider_signed {
181187
#[inline]
182188
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
189+
// NOTE: the safety of `unsafe impl TrustedLen` depends on
190+
// this being correct!
183191
if *start < *end {
184192
match end.checked_sub(*start) {
185193
Some(diff) => usize::try_from(diff).ok(),
@@ -274,6 +282,7 @@ impl<A: Step> Iterator for ops::Range<A> {
274282

275283
#[inline]
276284
fn size_hint(&self) -> (usize, Option<usize>) {
285+
// NOTE: the safety of `unsafe impl TrustedLen` depends on this being correct!
277286
match Step::steps_between(&self.start, &self.end) {
278287
Some(hint) => (hint, Some(hint)),
279288
None => (0, None)
@@ -306,8 +315,14 @@ range_incl_exact_iter_impl!(u8 u16 i8 i16);
306315
//
307316
// They need to guarantee that .size_hint() is either exact, or that
308317
// the upper bound is None when it does not fit the type limits.
309-
range_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
310-
range_incl_trusted_len_impl!(usize isize u8 i8 u16 i16 u32 i32 i64 u64);
318+
range_trusted_len_impl! {
319+
usize u8 u16 u32 u64 u128
320+
isize i8 i16 i32 i64 i128
321+
}
322+
range_incl_trusted_len_impl! {
323+
usize u8 u16 u32 u64 u128
324+
isize i8 i16 i32 i64 i128
325+
}
311326

312327
#[stable(feature = "rust1", since = "1.0.0")]
313328
impl<A: Step> DoubleEndedIterator for ops::Range<A> {
@@ -387,6 +402,8 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
387402

388403
#[inline]
389404
fn size_hint(&self) -> (usize, Option<usize>) {
405+
// NOTE: the safety of `unsafe impl TrustedLen` depends on this being correct!
406+
390407
if !(self.start <= self.end) {
391408
return (0, Some(0));
392409
}

0 commit comments

Comments
 (0)