Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dd481d5
fix nested impl trait lifetimes
cramertj Jan 18, 2018
4d92fe2
Fix span bug.
jseyfried Feb 9, 2018
a003cb7
Add test.
jseyfried Feb 9, 2018
0bb818c
Add Iterator::try_for_each
scottmcm Feb 12, 2018
bd10ef7
rustc_typeck/check/closure: rustfmt
nikomatsakis Feb 10, 2018
cc05561
detect wrong number of args when type-checking a closure
nikomatsakis Feb 12, 2018
1f0e1a0
Convert compile-fail/lint-ctypes.rs to ui test
Feb 14, 2018
7ac5e96
[improper_ctypes] Use a 'help:' line for possible fixes
Feb 11, 2018
ae92dfa
[improper_ctypes] Stop complaining about repr(usize) and repr(isize) …
Feb 11, 2018
9b5f47e
[improper_ctypes] Overhaul primary label
Feb 12, 2018
22a1716
[improper_ctypes] Suggest repr(transparent) for structs
Feb 14, 2018
9d493c8
[improper_ctypes] Point at definition of non-FFI-safe type if possible
Feb 14, 2018
051ea5c
[improper_ctypes] Don't suggest raw pointers when encountering trait …
Feb 14, 2018
7041ef3
lookup exported symbols only when needed.
andjo403 Feb 14, 2018
9d3719b
Do not run the default panic hook inside procedural macros. Fixes #47812
Zoxc Feb 1, 2018
4d8b251
update tracking issue for nll
Feb 22, 2018
e88fe1d
Small grammar fix to docs for String::new().
adeschamps Feb 22, 2018
311fbc9
[docs] Minor wording changes to drain_filter docs
mbrubeck Feb 22, 2018
e5d79c4
Move word type and word size usage to constants & make it of 128 bits
spastorino Feb 14, 2018
ff9eb56
Use Sparse bitsets instead of dense ones for NLL results
spastorino Feb 15, 2018
aa3409c
Fix typo otherwies -> otherwise
spastorino Feb 15, 2018
6a74615
Run rustfmt over bitvec.rs and region_infer/values.rs
spastorino Feb 22, 2018
9e6d2d7
Update Clippy
Manishearth Feb 23, 2018
abf4d70
Rollup merge of #47933 - Zoxc:plugin-panics, r=nikomatsakis
Manishearth Feb 23, 2018
4941cb4
Rollup merge of #48072 - cramertj:impl-trait-lifetime-res, r=nikomats…
Manishearth Feb 23, 2018
a1acb15
Rollup merge of #48083 - jseyfried:improve_tuple_struct_field_access_…
Manishearth Feb 23, 2018
1e67c13
Rollup merge of #48123 - nikomatsakis:issue-47244-expected-num-args, …
Manishearth Feb 23, 2018
5fd8d18
Rollup merge of #48157 - scottmcm:try-for-each, r=dtolnay
Manishearth Feb 23, 2018
5d30cba
Rollup merge of #48219 - andjo403:export_symbol, r=michaelwoerister
Manishearth Feb 23, 2018
9f36a35
Rollup merge of #48221 - rkruppe:improve-ctypes-lint, r=estebank
Manishearth Feb 23, 2018
74f6794
Rollup merge of #48245 - spastorino:sparse_bitsets, r=nikomatsakis
Manishearth Feb 23, 2018
f2da2fa
Rollup merge of #48429 - toidiu:patch-1, r=nikomatsakis
Manishearth Feb 23, 2018
cf3623f
Rollup merge of #48436 - adeschamps:string-doc-fix, r=estebank
Manishearth Feb 23, 2018
f2cbb76
Rollup merge of #48438 - mbrubeck:docs, r=TimNN
Manishearth Feb 23, 2018
b26442a
Rollup merge of #48472 - Manishearth:clippyup, r=oli-obk
Manishearth Feb 23, 2018
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 Iterator::try_for_each
The fallible version of for_each and the stateless version of try_fold.
  • Loading branch information
scottmcm committed Feb 12, 2018
commit 0bb818cc0b2885b01ce670ce192aac1dbc6db16a
48 changes: 42 additions & 6 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,9 @@ pub trait Iterator {
///
/// In particular, try to have this call `try_fold()` on the internal parts
/// from which this iterator is composed. If multiple calls are needed,
/// the `?` operator be convenient for chaining the accumulator value along,
/// but beware any invariants that need to be upheld before those early
/// returns. This is a `&mut self` method, so iteration needs to be
/// the `?` operator may be convenient for chaining the accumulator value
/// along, but beware any invariants that need to be upheld before those
/// early returns. This is a `&mut self` method, so iteration needs to be
/// resumable after hitting an error here.
///
/// # Examples
Expand Down Expand Up @@ -1414,6 +1414,42 @@ pub trait Iterator {
Try::from_ok(accum)
}

/// An iterator method that applies a fallible function to each item in the
/// iterator, stopping at the first error and returning that error.
///
/// This can also be thought of as the fallible form of [`for_each()`]
/// or as the stateless version of [`try_fold()`].
///
/// [`for_each()`]: #method.for_each
/// [`try_fold()`]: #method.try_fold
///
/// # Examples
///
/// ```
/// #![feature(iterator_try_fold)]
/// use std::fs::rename;
/// use std::io::{stdout, Write};
/// use std::path::Path;
///
/// let data = ["no_tea.txt", "stale_bread.json", "torrential_rain.png"];
///
/// let res = data.iter().try_for_each(|x| writeln!(stdout(), "{}", x));
/// assert!(res.is_ok());
///
/// let mut it = data.iter().cloned();
/// let res = it.try_for_each(|x| rename(x, Path::new(x).with_extension("old")));
/// assert!(res.is_err());
/// // It short-circuited, so the remaining items are still in the iterator:
/// assert_eq!(it.next(), Some("stale_bread.json"));
/// ```
#[inline]
#[unstable(feature = "iterator_try_fold", issue = "45594")]
fn try_for_each<F, R>(&mut self, mut f: F) -> R where
Self: Sized, F: FnMut(Self::Item) -> R, R: Try<Ok=()>
{
self.try_fold((), move |(), x| f(x))
}

/// An iterator method that applies a function, producing a single, final value.
///
/// `fold()` takes two arguments: an initial value, and a closure with two
Expand Down Expand Up @@ -1528,7 +1564,7 @@ pub trait Iterator {
fn all<F>(&mut self, mut f: F) -> bool where
Self: Sized, F: FnMut(Self::Item) -> bool
{
self.try_fold((), move |(), x| {
self.try_for_each(move |x| {
if f(x) { LoopState::Continue(()) }
else { LoopState::Break(()) }
}) == LoopState::Continue(())
Expand Down Expand Up @@ -1577,7 +1613,7 @@ pub trait Iterator {
Self: Sized,
F: FnMut(Self::Item) -> bool
{
self.try_fold((), move |(), x| {
self.try_for_each(move |x| {
if f(x) { LoopState::Break(()) }
else { LoopState::Continue(()) }
}) == LoopState::Break(())
Expand Down Expand Up @@ -1631,7 +1667,7 @@ pub trait Iterator {
Self: Sized,
P: FnMut(&Self::Item) -> bool,
{
self.try_fold((), move |(), x| {
self.try_for_each(move |x| {
if predicate(&x) { LoopState::Break(x) }
else { LoopState::Continue(()) }
}).break_value()
Expand Down