Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ macro_rules! indexed_range_impl {
}

trait UnindexedRangeLen<L> {
fn len(&self) -> L;
fn unindexed_len(&self) -> L;
}

macro_rules! unindexed_range_impl {
( $t:ty, $len_t:ty ) => {
impl UnindexedRangeLen<$len_t> for Range<$t> {
fn len(&self) -> $len_t {
fn unindexed_len(&self) -> $len_t {
let &Range { start, end } = self;
if end > start {
end.wrapping_sub(start) as $len_t
Expand Down Expand Up @@ -253,15 +253,15 @@ macro_rules! unindexed_range_impl {
}

fn opt_len(iter: &Iter<$t>) -> Option<usize> {
usize::try_from(iter.range.len()).ok()
usize::try_from(iter.range.unindexed_len()).ok()
}
}

impl UnindexedProducer for IterProducer<$t> {
type Item = $t;

fn split(mut self) -> (Self, Option<Self>) {
let index = self.range.len() / 2;
let index = self.range.unindexed_len() / 2;
if index > 0 {
let mid = self.range.start.wrapping_add(index as $t);
let right = mid..self.range.end;
Expand Down Expand Up @@ -387,11 +387,14 @@ fn test_i128_len_doesnt_overflow() {
range: 0..octillion,
};

assert_eq!(octillion as u128, producer.range.len());
assert_eq!(octillion as u128, (0..octillion).len());
assert_eq!(2 * octillion as u128, (-octillion..octillion).len());
assert_eq!(octillion as u128, producer.range.unindexed_len());
assert_eq!(octillion as u128, (0..octillion).unindexed_len());
assert_eq!(
2 * octillion as u128,
(-octillion..octillion).unindexed_len()
);

assert_eq!(u128::MAX, (i128::MIN..i128::MAX).len());
assert_eq!(u128::MAX, (i128::MIN..i128::MAX).unindexed_len());
}

#[test]
Expand Down