-
Notifications
You must be signed in to change notification settings - Fork 13.3k
core: Fix size_hint for signed integer Range<T> iterators #24865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2407,12 +2407,14 @@ pub trait Step: PartialOrd { | |
/// `start` should always be less than `end`, so the result should never | ||
/// be negative. | ||
/// | ||
/// `by` must be > 0. | ||
/// | ||
/// Returns `None` if it is not possible to calculate steps_between | ||
/// without overflow. | ||
fn steps_between(start: &Self, end: &Self, by: &Self) -> Option<usize>; | ||
} | ||
|
||
macro_rules! step_impl { | ||
macro_rules! step_impl_unsigned { | ||
($($t:ty)*) => ($( | ||
impl Step for $t { | ||
#[inline] | ||
|
@@ -2423,7 +2425,33 @@ macro_rules! step_impl { | |
#[allow(trivial_numeric_casts)] | ||
fn steps_between(start: &$t, end: &$t, by: &$t) -> Option<usize> { | ||
if *start <= *end { | ||
Some(((*end - *start) / *by) as usize) | ||
// Note: We assume $t <= usize here | ||
Some((*end - *start) as usize / (*by as usize)) | ||
} else { | ||
Some(0) | ||
} | ||
} | ||
} | ||
)*) | ||
} | ||
macro_rules! step_impl_signed { | ||
($($t:ty)*) => ($( | ||
impl Step for $t { | ||
#[inline] | ||
fn step(&self, by: &$t) -> Option<$t> { | ||
(*self).checked_add(*by) | ||
} | ||
#[inline] | ||
#[allow(trivial_numeric_casts)] | ||
fn steps_between(start: &$t, end: &$t, by: &$t) -> Option<usize> { | ||
if *start <= *end { | ||
// Note: We assume $t <= isize here | ||
// Use .wrapping_sub and cast to usize to compute the | ||
// difference that may not fit inside the range of isize. | ||
Some( | ||
((*end as isize).wrapping_sub(*start as isize) as usize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you ask, I should probably insert a comment there. It's needed because the length of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh so this is handling the case where you're calculating the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I do agree though that a comment would be nice) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the test cases. It shouldn't be able to be larger than usize::MAX. (We guarantee ExactSizeIterator here and would have to revoke that). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, my patch is totally broken because I've been thinking only about the signed case. Need to account for the unsigned case too, I understand if it's puzzling to read. I mean the cast to isize will be correct without overflow checks, but not with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to add a few test cases that use integer types larger than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm adding a test case for isize now. |
||
/ (*by as usize)) | ||
) | ||
} else { | ||
Some(0) | ||
} | ||
|
@@ -2447,9 +2475,12 @@ macro_rules! step_impl_no_between { | |
)*) | ||
} | ||
|
||
step_impl!(usize u8 u16 u32 isize i8 i16 i32); | ||
step_impl_unsigned!(usize u8 u16 u32); | ||
step_impl_signed!(isize i8 i16 i32); | ||
#[cfg(target_pointer_width = "64")] | ||
step_impl_unsigned!(u64); | ||
#[cfg(target_pointer_width = "64")] | ||
step_impl!(u64 i64); | ||
step_impl_signed!(i64); | ||
#[cfg(target_pointer_width = "32")] | ||
step_impl_no_between!(u64 i64); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explanation: "Result should never be negative" above implies this.