Skip to content

Commit

Permalink
Merge pull request #1161 from cuviper/unindexed_len
Browse files Browse the repository at this point in the history
Rename `UnindexedRangeLen::len` to `unindexed_len`
  • Loading branch information
cuviper authored Apr 24, 2024
2 parents 8ccfda3 + 1f399d0 commit 0e8d45d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,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 @@ -250,15 +250,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 @@ -382,11 +382,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

0 comments on commit 0e8d45d

Please sign in to comment.