Skip to content

Replace core::iter::AlwaysOk<T> by Result<T, !> #50941

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
Jun 13, 2018
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
4 changes: 2 additions & 2 deletions src/libcore/iter/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use cmp::Ordering;
use ops::Try;

use super::{AlwaysOk, LoopState};
use super::LoopState;
use super::{Chain, Cycle, Cloned, Enumerate, Filter, FilterMap, Fuse};
use super::{Flatten, FlatMap, flatten_compat};
use super::{Inspect, Map, Peekable, Scan, Skip, SkipWhile, StepBy, Take, TakeWhile, Rev};
Expand Down Expand Up @@ -1614,7 +1614,7 @@ pub trait Iterator {
fn fold<B, F>(mut self, init: B, mut f: F) -> B where
Self: Sized, F: FnMut(B, Self::Item) -> B,
{
self.try_fold(init, move |acc, x| AlwaysOk(f(acc, x))).0
self.try_fold(init, move |acc, x| Ok::<B, !>(f(acc, x))).unwrap()
}

/// Tests if every element of the iterator matches a predicate.
Expand Down
15 changes: 0 additions & 15 deletions src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,21 +354,6 @@ mod range;
mod sources;
mod traits;

/// Transparent newtype used to implement foo methods in terms of try_foo.
/// Important until #43278 is fixed; might be better as `Result<T, !>` later.
struct AlwaysOk<T>(pub T);

impl<T> Try for AlwaysOk<T> {
type Ok = T;
type Error = !;
#[inline]
fn into_result(self) -> Result<Self::Ok, Self::Error> { Ok(self.0) }
#[inline]
fn from_error(v: Self::Error) -> Self { v }
#[inline]
fn from_ok(v: Self::Ok) -> Self { AlwaysOk(v) }
}

/// Used to make try_fold closures more like normal loops
#[derive(PartialEq)]
enum LoopState<C, B> {
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/iter/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use ops::{Mul, Add, Try};
use num::Wrapping;

use super::{AlwaysOk, LoopState};
use super::LoopState;

/// Conversion from an `Iterator`.
///
Expand Down Expand Up @@ -524,7 +524,7 @@ pub trait DoubleEndedIterator: Iterator {
fn rfold<B, F>(mut self, accum: B, mut f: F) -> B where
Self: Sized, F: FnMut(B, Self::Item) -> B,
{
self.try_rfold(accum, move |acc, x| AlwaysOk(f(acc, x))).0
self.try_rfold(accum, move |acc, x| Ok::<B, !>(f(acc, x))).unwrap()
}

/// Searches for an element of an iterator from the back that satisfies a predicate.
Expand Down