Skip to content

Commit 9dc1de4

Browse files
MaikKleinerickt
authored andcommitted
cleanup .get and .get_err
1 parent f6bcf5d commit 9dc1de4

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

src/libstd/result.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,6 @@ pub enum Result<T, U> {
3131
Err(U)
3232
}
3333

34-
/**
35-
* Get the value out of a successful result
36-
*
37-
* # Failure
38-
*
39-
* If the result is an error
40-
*/
41-
#[inline]
42-
pub fn get<T:Clone,U>(res: &Result<T, U>) -> T {
43-
match *res {
44-
Ok(ref t) => (*t).clone(),
45-
Err(ref the_err) =>
46-
fail!("get called on error result: %?", *the_err)
47-
}
48-
}
49-
50-
/**
51-
* Get the value out of an error result
52-
*
53-
* # Failure
54-
*
55-
* If the result is not an error
56-
*/
57-
#[inline]
58-
pub fn get_err<T, U: Clone>(res: &Result<T, U>) -> U {
59-
match *res {
60-
Err(ref u) => (*u).clone(),
61-
Ok(_) => fail!("get_err called on ok result")
62-
}
63-
}
64-
6534
/**
6635
* Convert to the `either` type
6736
*
@@ -245,8 +214,20 @@ impl<T, E> Result<T, E> {
245214
}
246215

247216
impl<T:Clone,E> Result<T, E> {
217+
/**
218+
* Get the value out of a successful result
219+
*
220+
* # Failure
221+
*
222+
* If the result is an error
223+
*/
248224
#[inline]
249-
pub fn get(&self) -> T { get(self) }
225+
pub fn get(&self) -> T {
226+
match *self {
227+
Ok(ref t) => (*t).clone(),
228+
Err(ref e) => fail!("get called on error result: %?", *e),
229+
}
230+
}
250231

251232
#[inline]
252233
pub fn map_err<F:Clone>(&self, op: &fn(&E) -> F) -> Result<T,F> {
@@ -255,8 +236,20 @@ impl<T:Clone,E> Result<T, E> {
255236
}
256237

257238
impl<T, E:Clone> Result<T, E> {
239+
/**
240+
* Get the value out of an error result
241+
*
242+
* # Failure
243+
*
244+
* If the result is not an error
245+
*/
258246
#[inline]
259-
pub fn get_err(&self) -> E { get_err(self) }
247+
pub fn get_err(&self) -> E {
248+
match *self {
249+
Err(ref u) => (*u).clone(),
250+
Ok(_) => fail!("get_err called on ok result"),
251+
}
252+
}
260253

261254
#[inline]
262255
pub fn map<U:Clone>(&self, op: &fn(&T) -> U) -> Result<U,E> {
@@ -363,7 +356,7 @@ pub fn iter_vec2<S,T,U>(ss: &[S], ts: &[T],
363356

364357
#[cfg(test)]
365358
mod tests {
366-
use result::{Err, Ok, Result, get, get_err};
359+
use result::{Err, Ok, Result};
367360
use result;
368361

369362
pub fn op1() -> result::Result<int, ~str> { result::Ok(666) }
@@ -376,12 +369,12 @@ mod tests {
376369

377370
#[test]
378371
pub fn chain_success() {
379-
assert_eq!(get(&(op1().chain(op2))), 667u);
372+
assert_eq!(op1().chain(op2).get(), 667u);
380373
}
381374

382375
#[test]
383376
pub fn chain_failure() {
384-
assert_eq!(get_err(&op3().chain( op2)), ~"sadface");
377+
assert_eq!(op3().chain( op2).get_err(), ~"sadface");
385378
}
386379
387380
#[test]

0 commit comments

Comments
 (0)