@@ -18,6 +18,7 @@ use cmp::Eq;
18
18
use iterator:: IteratorUtil ;
19
19
use result:: Result ;
20
20
use result;
21
+ use str:: StrSlice ;
21
22
use vec;
22
23
use vec:: { OwnedVector , ImmutableVector } ;
23
24
@@ -121,24 +122,37 @@ pub fn is_right<T, U>(eith: &Either<T, U>) -> bool {
121
122
}
122
123
}
123
124
124
- /// Retrieves the value in the left branch. Fails if the either is Right.
125
+ /// Retrieves the value in the left branch.
126
+ /// Fails with a specified reason if the either is Right.
125
127
#[ inline]
126
- pub fn unwrap_left < T , U > ( eith : Either < T , U > ) -> T {
128
+ pub fn expect_left < T , U > ( eith : Either < T , U > , reason : & str ) -> T {
127
129
match eith {
128
130
Left ( x) => x,
129
- Right ( _) => fail ! ( "either::unwrap_left Right" )
131
+ Right ( _) => fail ! ( reason . to_owned ( ) )
130
132
}
131
133
}
132
134
133
- /// Retrieves the value in the right branch. Fails if the either is Left .
135
+ /// Retrieves the value in the left branch. Fails if the either is Right .
134
136
#[ inline]
135
- pub fn unwrap_right < T , U > ( eith : Either < T , U > ) -> U {
137
+ pub fn unwrap_left < T , U > ( eith : Either < T , U > ) -> T {
138
+ expect_left ( eith, "either::unwrap_left Right" )
139
+ }
140
+
141
+ /// Retrieves the value in the right branch.
142
+ /// Fails with a specified reason if the either is Left.
143
+ #[ inline]
144
+ pub fn expect_right < T , U > ( eith : Either < T , U > , reason : & str ) -> U {
136
145
match eith {
137
146
Right ( x) => x,
138
- Left ( _) => fail ! ( "either::unwrap_right Left" )
147
+ Left ( _) => fail ! ( reason . to_owned ( ) )
139
148
}
140
149
}
141
150
151
+ /// Retrieves the value in the right branch. Fails if the either is Left.
152
+ pub fn unwrap_right < T , U > ( eith : Either < T , U > ) -> U {
153
+ expect_right ( eith, "either::unwrap_right Left" )
154
+ }
155
+
142
156
impl < T , U > Either < T , U > {
143
157
#[ inline]
144
158
pub fn either < V > ( & self , f_left : & fn ( & T ) -> V , f_right : & fn ( & U ) -> V ) -> V {
@@ -157,9 +171,15 @@ impl<T, U> Either<T, U> {
157
171
#[ inline]
158
172
pub fn is_right ( & self ) -> bool { is_right ( self ) }
159
173
174
+ #[ inline]
175
+ pub fn expect_left ( self , reason : & str ) -> T { expect_left ( self , reason) }
176
+
160
177
#[ inline]
161
178
pub fn unwrap_left ( self ) -> T { unwrap_left ( self ) }
162
179
180
+ #[ inline]
181
+ pub fn expect_right ( self , reason : & str ) -> U { expect_right ( self , reason) }
182
+
163
183
#[ inline]
164
184
pub fn unwrap_right ( self ) -> U { unwrap_right ( self ) }
165
185
}
0 commit comments