Skip to content

Rollup of 9 pull requests #61303

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

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a35cdd4
Implement `iter::Sum` and `iter::Product` for `Option`
jtdowney Mar 6, 2019
8c69876
Update stable attribute to be since 1.35.0
Centril Mar 19, 2019
422a4c0
Add improved doc example for Sum<Option<T>>
jtdowney Mar 21, 2019
4d9b9e9
wip nth_back on chunks
Apr 26, 2019
73ca8bc
hopefully working nth_back on chunks
May 1, 2019
5eb0e08
Implement nth_back for RChunks(Exact)(Mut)
timvermeulen May 5, 2019
02a148d
wip nth_back on chunks
Apr 26, 2019
aff83c8
hopefully working nth_back on chunks
May 1, 2019
16223e4
new implementation for nth_back for chunks
wizAmit May 14, 2019
fd15405
fixed merge conflicts
wizAmit May 14, 2019
dc82626
wip nth_back on chunks
Apr 26, 2019
2080b86
hopefully working nth_back on chunks
May 1, 2019
29a103d
wip nth_back on chunks
Apr 26, 2019
9309447
succint implementation
wizAmit May 22, 2019
bcfd1f3
fix merge conflicts
wizAmit May 22, 2019
f1d0829
Add Step::sub_usize
timvermeulen May 4, 2019
f9d328d
sync::Weak::{as,from,into}_raw
vorner May 12, 2019
4f1dcb3
rc::Weak::{as,from,into}_raw
vorner May 19, 2019
96e3fb2
librustc_errors: Move annotation collection to own impl
phansch May 25, 2019
0631d19
Avoid unneeded bug!() call
spastorino May 28, 2019
120ce12
Rename `TraitOrImpl` to `Assoc` and `trait_or_impl` to `assoc`.
eddyb May 28, 2019
03d3290
rustc_codegen_ssa: remove obsolete codegen stats.
eddyb May 28, 2019
29b7c06
rustc_codegen_llvm: remove LLVM instruction count stats.
eddyb May 28, 2019
7fa97c0
rustc_codegen_llvm: rename away the last occurrence of `insn`.
eddyb May 28, 2019
9f8d934
Bump Sum and Product for Option<T> to 1.37
dtolnay May 28, 2019
8e3f003
Rollup merge of #58975 - jtdowney:iter_arith_traits_option, r=dtolnay
Centril May 29, 2019
85c975b
Rollup merge of #60542 - timvermeulen:step_sub_usize, r=scottmcm
Centril May 29, 2019
966e354
Rollup merge of #60555 - timvermeulen:rchunks_nth_back, r=scottmcm
Centril May 29, 2019
c98841b
Rollup merge of #60766 - vorner:weak-into-raw, r=sfackler
Centril May 29, 2019
aad084a
Rollup merge of #61048 - wizAmit:feature/nth_back_chunks, r=scottmcm
Centril May 29, 2019
cd38c8f
Rollup merge of #61191 - phansch:annotate_snippet_refactorings1, r=es…
Centril May 29, 2019
e0bb093
Rollup merge of #61291 - spastorino:avoid-unneeded-bug-call, r=estebank
Centril May 29, 2019
2af35bf
Rollup merge of #61294 - eddyb:no-trait-nor-impl, r=varkor
Centril May 29, 2019
c0142ac
Rollup merge of #61297 - eddyb:forsaken-stats, r=nagisa
Centril May 29, 2019
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
Prev Previous commit
Next Next commit
Add Step::sub_usize
  • Loading branch information
timvermeulen committed May 25, 2019
commit f1d0829e20e3ff3ff78a09136968612887544af2
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