Skip to content

Commit bc9dc0a

Browse files
author
Clar Charr
committed
Move Try to module.
1 parent f774cdd commit bc9dc0a

File tree

2 files changed

+118
-102
lines changed

2 files changed

+118
-102
lines changed

src/libcore/ops/mod.rs

Lines changed: 7 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ mod bit;
152152
mod function;
153153
mod place;
154154
mod range;
155+
mod try;
155156

156157
#[stable(feature = "rust1", since = "1.0.0")]
157158
pub use self::arith::{Add, Sub, Mul, Div, Rem, Neg};
@@ -172,6 +173,12 @@ pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
172173
#[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
173174
pub use self::range::{RangeInclusive, RangeToInclusive};
174175

176+
#[unstable(feature = "question_mark_carrier", issue = "31436")]
177+
#[cfg(stage0)]
178+
pub use self::try::Carrier;
179+
#[unstable(feature = "try_trait", issue = "42327")]
180+
pub use self::try::Try;
181+
175182
#[unstable(feature = "placement_new_protocol", issue = "27779")]
176183
pub use self::place::{Place, Placer, InPlace, Boxed, BoxPlace};
177184

@@ -593,105 +600,3 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
593600
// *const T -> *const U
594601
#[unstable(feature = "coerce_unsized", issue = "27732")]
595602
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
596-
597-
/// This trait has been superseded by the `Try` trait, but must remain
598-
/// here as `?` is still lowered to it in stage0 .
599-
#[cfg(stage0)]
600-
#[unstable(feature = "question_mark_carrier", issue = "31436")]
601-
pub trait Carrier {
602-
/// The type of the value when computation succeeds.
603-
type Success;
604-
/// The type of the value when computation errors out.
605-
type Error;
606-
607-
/// Create a `Carrier` from a success value.
608-
fn from_success(_: Self::Success) -> Self;
609-
610-
/// Create a `Carrier` from an error value.
611-
fn from_error(_: Self::Error) -> Self;
612-
613-
/// Translate this `Carrier` to another implementation of `Carrier` with the
614-
/// same associated types.
615-
fn translate<T>(self) -> T where T: Carrier<Success=Self::Success, Error=Self::Error>;
616-
}
617-
618-
#[cfg(stage0)]
619-
#[unstable(feature = "question_mark_carrier", issue = "31436")]
620-
impl<U, V> Carrier for Result<U, V> {
621-
type Success = U;
622-
type Error = V;
623-
624-
fn from_success(u: U) -> Result<U, V> {
625-
Ok(u)
626-
}
627-
628-
fn from_error(e: V) -> Result<U, V> {
629-
Err(e)
630-
}
631-
632-
fn translate<T>(self) -> T
633-
where T: Carrier<Success=U, Error=V>
634-
{
635-
match self {
636-
Ok(u) => T::from_success(u),
637-
Err(e) => T::from_error(e),
638-
}
639-
}
640-
}
641-
642-
struct _DummyErrorType;
643-
644-
impl Try for _DummyErrorType {
645-
type Ok = ();
646-
type Error = ();
647-
648-
fn into_result(self) -> Result<Self::Ok, Self::Error> {
649-
Ok(())
650-
}
651-
652-
fn from_ok(_: ()) -> _DummyErrorType {
653-
_DummyErrorType
654-
}
655-
656-
fn from_error(_: ()) -> _DummyErrorType {
657-
_DummyErrorType
658-
}
659-
}
660-
661-
/// A trait for customizing the behaviour of the `?` operator.
662-
///
663-
/// A type implementing `Try` is one that has a canonical way to view it
664-
/// in terms of a success/failure dichotomy. This trait allows both
665-
/// extracting those success or failure values from an existing instance and
666-
/// creating a new instance from a success or failure value.
667-
#[unstable(feature = "try_trait", issue = "42327")]
668-
pub trait Try {
669-
/// The type of this value when viewed as successful.
670-
#[unstable(feature = "try_trait", issue = "42327")]
671-
type Ok;
672-
/// The type of this value when viewed as failed.
673-
#[unstable(feature = "try_trait", issue = "42327")]
674-
type Error;
675-
676-
/// Applies the "?" operator. A return of `Ok(t)` means that the
677-
/// execution should continue normally, and the result of `?` is the
678-
/// value `t`. A return of `Err(e)` means that execution should branch
679-
/// to the innermost enclosing `catch`, or return from the function.
680-
///
681-
/// If an `Err(e)` result is returned, the value `e` will be "wrapped"
682-
/// in the return type of the enclosing scope (which must itself implement
683-
/// `Try`). Specifically, the value `X::from_error(From::from(e))`
684-
/// is returned, where `X` is the return type of the enclosing function.
685-
#[unstable(feature = "try_trait", issue = "42327")]
686-
fn into_result(self) -> Result<Self::Ok, Self::Error>;
687-
688-
/// Wrap an error value to construct the composite result. For example,
689-
/// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
690-
#[unstable(feature = "try_trait", issue = "42327")]
691-
fn from_error(v: Self::Error) -> Self;
692-
693-
/// Wrap an OK value to construct the composite result. For example,
694-
/// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
695-
#[unstable(feature = "try_trait", issue = "42327")]
696-
fn from_ok(v: Self::Ok) -> Self;
697-
}

src/libcore/ops/try.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
/// This trait has been superseded by the `Try` trait, but must remain
12+
/// here as `?` is still lowered to it in stage0 .
13+
#[cfg(stage0)]
14+
#[unstable(feature = "question_mark_carrier", issue = "31436")]
15+
pub trait Carrier {
16+
/// The type of the value when computation succeeds.
17+
type Success;
18+
/// The type of the value when computation errors out.
19+
type Error;
20+
21+
/// Create a `Carrier` from a success value.
22+
fn from_success(_: Self::Success) -> Self;
23+
24+
/// Create a `Carrier` from an error value.
25+
fn from_error(_: Self::Error) -> Self;
26+
27+
/// Translate this `Carrier` to another implementation of `Carrier` with the
28+
/// same associated types.
29+
fn translate<T>(self) -> T where T: Carrier<Success=Self::Success, Error=Self::Error>;
30+
}
31+
32+
#[cfg(stage0)]
33+
#[unstable(feature = "question_mark_carrier", issue = "31436")]
34+
impl<U, V> Carrier for Result<U, V> {
35+
type Success = U;
36+
type Error = V;
37+
38+
fn from_success(u: U) -> Result<U, V> {
39+
Ok(u)
40+
}
41+
42+
fn from_error(e: V) -> Result<U, V> {
43+
Err(e)
44+
}
45+
46+
fn translate<T>(self) -> T
47+
where T: Carrier<Success=U, Error=V>
48+
{
49+
match self {
50+
Ok(u) => T::from_success(u),
51+
Err(e) => T::from_error(e),
52+
}
53+
}
54+
}
55+
56+
struct _DummyErrorType;
57+
58+
impl Try for _DummyErrorType {
59+
type Ok = ();
60+
type Error = ();
61+
62+
fn into_result(self) -> Result<Self::Ok, Self::Error> {
63+
Ok(())
64+
}
65+
66+
fn from_ok(_: ()) -> _DummyErrorType {
67+
_DummyErrorType
68+
}
69+
70+
fn from_error(_: ()) -> _DummyErrorType {
71+
_DummyErrorType
72+
}
73+
}
74+
75+
/// A trait for customizing the behaviour of the `?` operator.
76+
///
77+
/// A type implementing `Try` is one that has a canonical way to view it
78+
/// in terms of a success/failure dichotomy. This trait allows both
79+
/// extracting those success or failure values from an existing instance and
80+
/// creating a new instance from a success or failure value.
81+
#[unstable(feature = "try_trait", issue = "42327")]
82+
pub trait Try {
83+
/// The type of this value when viewed as successful.
84+
#[unstable(feature = "try_trait", issue = "42327")]
85+
type Ok;
86+
/// The type of this value when viewed as failed.
87+
#[unstable(feature = "try_trait", issue = "42327")]
88+
type Error;
89+
90+
/// Applies the "?" operator. A return of `Ok(t)` means that the
91+
/// execution should continue normally, and the result of `?` is the
92+
/// value `t`. A return of `Err(e)` means that execution should branch
93+
/// to the innermost enclosing `catch`, or return from the function.
94+
///
95+
/// If an `Err(e)` result is returned, the value `e` will be "wrapped"
96+
/// in the return type of the enclosing scope (which must itself implement
97+
/// `Try`). Specifically, the value `X::from_error(From::from(e))`
98+
/// is returned, where `X` is the return type of the enclosing function.
99+
#[unstable(feature = "try_trait", issue = "42327")]
100+
fn into_result(self) -> Result<Self::Ok, Self::Error>;
101+
102+
/// Wrap an error value to construct the composite result. For example,
103+
/// `Result::Err(x)` and `Result::from_error(x)` are equivalent.
104+
#[unstable(feature = "try_trait", issue = "42327")]
105+
fn from_error(v: Self::Error) -> Self;
106+
107+
/// Wrap an OK value to construct the composite result. For example,
108+
/// `Result::Ok(x)` and `Result::from_ok(x)` are equivalent.
109+
#[unstable(feature = "try_trait", issue = "42327")]
110+
fn from_ok(v: Self::Ok) -> Self;
111+
}

0 commit comments

Comments
 (0)