Skip to content

Commit

Permalink
Remove the Option and bool impls for carrier and add a dummy impl
Browse files Browse the repository at this point in the history
The dummy impl should ensure the same type checking behaviour as having other (real) Carrier impls.
  • Loading branch information
nrc committed Aug 9, 2016
1 parent 5f891c5 commit 6471be5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 77 deletions.
67 changes: 7 additions & 60 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ use cmp::PartialOrd;
use fmt;
use marker::{Sized, Unsize};
use result::Result::{self, Ok, Err};
use option::Option::{self, Some, None};

/// The `Drop` trait is used to run some code when a value goes out of scope.
/// This is sometimes called a 'destructor'.
Expand Down Expand Up @@ -2198,75 +2197,23 @@ impl<U, V> Carrier for Result<U, V> {
}
}

#[unstable(feature = "question_mark_carrier", issue = "31436")]
impl<U> Carrier for Option<U> {
type Success = U;
type Error = ();

fn from_success(u: U) -> Option<U> {
Some(u)
}
struct _DummyErrorType;

fn from_error(_: ()) -> Option<U> {
None
}

fn translate<T>(self) -> T
where T: Carrier<Success=U, Error=()>
{
match self {
Some(u) => T::from_success(u),
None => T::from_error(()),
}
}
}

// Implementing Carrier for bools means it's easy to write short-circuiting
// functions. E.g.,
// ```
// fn foo() -> bool {
// if !(f() || g()) {
// return false;
// }
//
// some_computation();
// if h() {
// return false;
// }
//
// more_computation();
// i()
// }
// ```
// becomes
// ```
// fn foo() -> bool {
// (f() || g())?;
// some_computation();
// (!h())?;
// more_computation();
// i()
// }
// ```
#[unstable(feature = "question_mark_carrier", issue = "31436")]
impl Carrier for bool {
impl Carrier for _DummyErrorType {
type Success = ();
type Error = ();

fn from_success(_: ()) -> bool {
true
fn from_success(_: ()) -> _DummyErrorType {
_DummyErrorType
}

fn from_error(_: ()) -> bool {
false
fn from_error(_: ()) -> _DummyErrorType {
_DummyErrorType
}

fn translate<T>(self) -> T
where T: Carrier<Success=(), Error=()>
{
match self {
true => T::from_success(()),
false => T::from_error(()),
}
T::from_success(())
}
}
17 changes: 0 additions & 17 deletions src/test/run-pass/try-operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,6 @@ fn merge_error() -> Result<i32, Error> {
Ok(s.parse::<i32>()? + 1)
}

fn option() -> Option<i32> {
let x = Some(42);
let y = x?;
Some(y + 2)
}

fn bool() -> bool {
let x = true;
let y = false;
let z = true;

(x || y)?;
let a: () = z?;
x?;
true
}

fn main() {
assert_eq!(Ok(3), on_method());

Expand Down

0 comments on commit 6471be5

Please sign in to comment.