Skip to content

Add Step::sub_usize #60542

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

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub trait Step: Clone + PartialOrd + Sized {

/// Adds a `usize`, returning `None` on overflow.
fn add_usize(&self, n: usize) -> Option<Self>;

/// Subtracts a `usize`, returning `None` on underflow.
fn sub_usize(&self, n: usize) -> Option<Self> {
// this default implementation makes the addition of `sub_usize` a non-breaking change
let _ = n;
unimplemented!()
}
}

// These are still macro-generated because the integer literals resolve to different types.
Expand Down Expand Up @@ -85,6 +92,15 @@ macro_rules! step_impl_unsigned {
}
}

#[inline]
#[allow(unreachable_patterns)]
fn sub_usize(&self, n: usize) -> Option<Self> {
match <$t>::try_from(n) {
Ok(n_as_t) => self.checked_sub(n_as_t),
Err(_) => None,
}
}

step_identical_methods!();
}
)*)
Expand Down Expand Up @@ -125,6 +141,25 @@ macro_rules! step_impl_signed {
}
}

#[inline]
#[allow(unreachable_patterns)]
fn sub_usize(&self, n: usize) -> Option<Self> {
match <$unsigned>::try_from(n) {
Ok(n_as_unsigned) => {
// Wrapping in unsigned space handles cases like
// `80_i8.sub_usize(200) == Some(-120_i8)`,
// even though 200_usize is out of range for i8.
let wrapped = (*self as $unsigned).wrapping_sub(n_as_unsigned) as $t;
if wrapped <= *self {
Some(wrapped)
} else {
None // Subtraction underflowed
}
}
Err(_) => None,
}
}

step_identical_methods!();
}
)*)
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_data_structures/indexed_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ macro_rules! newtype_index {
fn add_usize(&self, u: usize) -> Option<Self> {
Idx::index(*self).checked_add(u).map(Self::new)
}

#[inline]
fn sub_usize(&self, u: usize) -> Option<Self> {
Idx::index(*self).checked_sub(u).map(Self::new)
}
}

impl From<$type> for u32 {
Expand Down
4 changes: 4 additions & 0 deletions src/test/run-pass/impl-trait/example-calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl std::iter::Step for NaiveDate {
fn add_usize(&self, _: usize) -> Option<Self> {
unimplemented!()
}

fn sub_usize(&self, _: usize) -> Option<Self> {
unimplemented!()
}
}

#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
Expand Down