-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Carrier trait for testing, DON'T LAND #35056
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ | |
use cmp::PartialOrd; | ||
use fmt; | ||
use marker::{Sized, Unsize}; | ||
use result::Result::{self, Ok, Err}; | ||
|
||
/// The `Drop` trait is used to run some code when a value goes out of scope. | ||
/// This is sometimes called a 'destructor'. | ||
|
@@ -2145,3 +2146,74 @@ pub trait BoxPlace<Data: ?Sized> : Place<Data> { | |
/// Creates a globally fresh place. | ||
fn make_place() -> Self; | ||
} | ||
|
||
/// A trait for types which have success and error states and are meant to work | ||
/// with the question mark operator. | ||
/// When the `?` operator is used with a value, whether the value is in the | ||
/// success or error state is determined by calling `translate`. | ||
/// | ||
/// This trait is **very** experimental, it will probably be iterated on heavily | ||
/// before it is stabilised. Implementors should expect change. Users of `?` | ||
/// should not rely on any implementations of `Carrier` other than `Result`, | ||
/// i.e., you should not expect `?` to continue to work with `Option`, etc. | ||
#[unstable(feature = "question_mark_carrier", issue = "31436")] | ||
pub trait Carrier { | ||
/// The type of the value when computation succeeds. | ||
type Success; | ||
/// The type of the value when computation errors out. | ||
type Error; | ||
|
||
/// Create a `Carrier` from a success value. | ||
fn from_success(Self::Success) -> Self; | ||
|
||
/// Create a `Carrier` from an error value. | ||
fn from_error(Self::Error) -> Self; | ||
|
||
/// Translate this `Carrier` to another implementation of `Carrier` with the | ||
/// same associated types. | ||
fn translate<T>(self) -> T where T: Carrier<Success=Self::Success, Error=Self::Error>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should discuss how we could enforce a rule that ensures that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you enforce that rule, then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, that depends on the rule I guess. =) See my extended comment below for some more precise thoughts. |
||
} | ||
|
||
#[unstable(feature = "question_mark_carrier", issue = "31436")] | ||
impl<U, V> Carrier for Result<U, V> { | ||
type Success = U; | ||
type Error = V; | ||
|
||
fn from_success(u: U) -> Result<U, V> { | ||
Ok(u) | ||
} | ||
|
||
fn from_error(e: V) -> Result<U, V> { | ||
Err(e) | ||
} | ||
|
||
fn translate<T>(self) -> T | ||
where T: Carrier<Success=U, Error=V> | ||
{ | ||
match self { | ||
Ok(u) => T::from_success(u), | ||
Err(e) => T::from_error(e), | ||
} | ||
} | ||
} | ||
|
||
struct _DummyErrorType; | ||
|
||
impl Carrier for _DummyErrorType { | ||
type Success = (); | ||
type Error = (); | ||
|
||
fn from_success(_: ()) -> _DummyErrorType { | ||
_DummyErrorType | ||
} | ||
|
||
fn from_error(_: ()) -> _DummyErrorType { | ||
_DummyErrorType | ||
} | ||
|
||
fn translate<T>(self) -> T | ||
where T: Carrier<Success=(), Error=()> | ||
{ | ||
T::from_success(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be clear, this change (updating
try!
) was something we wanted to do just to evaluate whether we could stabilize without the carrier trait. Seems clear we cannot -- and so should revert this change, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is what I meant in my last comment