diff options
| author | Erick Tryzelaar <etryzelaar@iqt.org> | 2012-12-18 18:55:19 -0800 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2012-12-18 20:54:13 -0800 |
| commit | a0ef334179714e0c3f1a3c7276543a0305db2c95 (patch) | |
| tree | 36f12b1ed6b54ec9a7c3e4a2c39e21fd7d0a5b16 /src/libcore | |
| parent | 938058b0040e3c482e10b78eeef7afb941b2b64e (diff) | |
| download | rust-a0ef334179714e0c3f1a3c7276543a0305db2c95.tar.gz rust-a0ef334179714e0c3f1a3c7276543a0305db2c95.zip | |
core: use movable self to clean up option/result.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/option.rs | 98 | ||||
| -rw-r--r-- | src/libcore/pipes.rs | 4 | ||||
| -rw-r--r-- | src/libcore/result.rs | 87 |
3 files changed, 103 insertions, 86 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index f7de25bf021..a4f385ea12c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -96,19 +96,6 @@ pub pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T { } } -pub pure fn expect<T>(opt: Option<T>, reason: ~str) -> T { - /*! - * Gets the value out of an option without copying, printing a - * specified message on failure. - * - * # Failure - * - * Fails if the value equals `none` - */ - if opt.is_some() { move option::unwrap(move opt) } - else { fail reason } -} - pub pure fn map<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> { //! Maps a `some` value by reference from one type to another @@ -235,35 +222,46 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T { unwrap(util::replace(opt, None)) } -pub pure fn unwrap_expect<T>(opt: Option<T>, reason: &str) -> T { +pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T { //! As unwrap, but with a specified failure message. - if opt.is_none() { fail reason.to_owned(); } - unwrap(move opt) + match move opt { + Some(move val) => val, + None => fail reason.to_owned(), + } } -// Some of these should change to be &Option<T>, some should not. See below. impl<T> Option<T> { /// Returns true if the option equals `none` - pure fn is_none() -> bool { is_none(&self) } + #[inline(always)] + pure fn is_none(&self) -> bool { is_none(self) } + /// Returns true if the option contains some value - pure fn is_some() -> bool { is_some(&self) } -} + #[inline(always)] + pure fn is_some(&self) -> bool { is_some(self) } -impl<T> &Option<T> { /** * Update an optional value by optionally running its content by reference * through a function that returns an option. */ - pure fn chain_ref<U>(f: fn(x: &T) -> Option<U>) -> Option<U> { + #[inline(always)] + pure fn chain_ref<U>(&self, f: fn(x: &T) -> Option<U>) -> Option<U> { chain_ref(self, f) } + + /// Maps a `some` value from one type to another by reference + #[inline(always)] + pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) } + /// Applies a function to the contained value or returns a default - pure fn map_default<U>(def: U, f: fn(x: &T) -> U) -> U - { map_default(self, move def, f) } + #[inline(always)] + pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U { + map_default(self, move def, f) + } + /// Performs an operation on the contained value by reference - pure fn iter(f: fn(x: &T)) { iter(self, f) } - /// Maps a `some` value from one type to another by reference - pure fn map<U>(f: fn(x: &T) -> U) -> Option<U> { map(self, f) } + #[inline(always)] + pure fn iter(&self, f: fn(x: &T)) { iter(self, f) } + /** Gets an immutable reference to the value inside an option. @@ -278,7 +276,29 @@ impl<T> &Option<T> { Instead, prefer to use pattern matching and handle the `None` case explicitly. */ - pure fn get_ref() -> &self/T { get_ref(self) } + #[inline(always)] + pure fn get_ref(&self) -> &self/T { get_ref(self) } + + /** + * Gets the value out of an option without copying. + * + * # Failure + * + * Fails if the value equals `none` + */ + #[inline(always)] + pure fn unwrap(self) -> T { unwrap(self) } + + /** + * Gets the value out of an option, printing a specified message on + * failure + * + * # Failure + * + * Fails if the value equals `none` + */ + #[inline(always)] + pure fn expect(self, reason: &str) -> T { expect(self, reason) } } impl<T: Copy> Option<T> { @@ -296,19 +316,17 @@ impl<T: Copy> Option<T> { Instead, prefer to use pattern matching and handle the `None` case explicitly. */ - pure fn get() -> T { get(self) } - pure fn get_default(def: T) -> T { get_default(self, def) } - /** - * Gets the value out of an option, printing a specified message on - * failure - * - * # Failure - * - * Fails if the value equals `none` - */ - pure fn expect(reason: ~str) -> T { expect(self, move reason) } + #[inline(always)] + pure fn get(self) -> T { get(self) } + + #[inline(always)] + pure fn get_default(self, def: T) -> T { get_default(self, def) } + /// Applies a function zero or more times until the result is none. - pure fn while_some(blk: fn(v: T) -> Option<T>) { while_some(self, blk) } + #[inline(always)] + pure fn while_some(self, blk: fn(v: T) -> Option<T>) { + while_some(self, blk) + } } #[test] diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index 914530c0653..59d59d33518 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -412,7 +412,7 @@ Fails if the sender closes the connection. */ pub fn recv<T: Owned, Tbuffer: Owned>( p: RecvPacketBuffered<T, Tbuffer>) -> T { - option::unwrap_expect(try_recv(move p), "connection closed") + try_recv(move p).expect("connection closed") } /** Attempts to receive a message from a pipe. @@ -1102,7 +1102,7 @@ impl<T: Owned> PortSet<T> : GenericPort<T> { } fn recv() -> T { - option::unwrap_expect(self.try_recv(), "port_set: endpoints closed") + self.try_recv().expect("port_set: endpoints closed") } } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index a1e7df28872..26064345b59 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -114,7 +114,7 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>) * ok(parse_bytes(buf)) * } */ -pub fn chain<T, U, V>(res: Result<T, V>, op: fn(t: T) +pub pure fn chain<T, U, V>(res: Result<T, V>, op: fn(T) -> Result<U, V>) -> Result<U, V> { match move res { Ok(move t) => op(move t), @@ -130,7 +130,7 @@ pub fn chain<T, U, V>(res: Result<T, V>, op: fn(t: T) * immediately returned. This function can be used to pass through a * successful result while handling an error. */ -pub fn chain_err<T, U, V>( +pub pure fn chain_err<T, U, V>( res: Result<T, V>, op: fn(t: V) -> Result<T, U>) -> Result<T, U> { @@ -154,7 +154,7 @@ pub fn chain_err<T, U, V>( * print_buf(buf) * } */ -pub fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) { +pub pure fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) { match *res { Ok(ref t) => f(t), Err(_) => () @@ -169,7 +169,7 @@ pub fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) { * This function can be used to pass through a successful result while * handling an error. */ -pub fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) { +pub pure fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) { match *res { Ok(_) => (), Err(ref e) => f(e) @@ -190,7 +190,7 @@ pub fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) { * parse_bytes(buf) * } */ -pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U) +pub pure fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U) -> Result<U, E> { match *res { Ok(ref t) => Ok(op(t)), @@ -206,7 +206,7 @@ pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U) * is immediately returned. This function can be used to pass through a * successful result while handling an error. */ -pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F) +pub pure fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F) -> Result<T, F> { match *res { Ok(copy t) => Ok(t), @@ -215,58 +215,55 @@ pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F) } impl<T, E> Result<T, E> { + #[inline(always)] pure fn get_ref(&self) -> &self/T { get_ref(self) } - pure fn is_ok() -> bool { is_ok(&self) } + #[inline(always)] + pure fn is_ok(&self) -> bool { is_ok(self) } - pure fn is_err() -> bool { is_err(&self) } + #[inline(always)] + pure fn is_err(&self) -> bool { is_err(self) } - pure fn iter(f: fn(&T)) { - match self { - Ok(ref t) => f(t), - Err(_) => () - } + #[inline(always)] + pure fn iter(&self, f: fn(&T)) { iter(self, f) } + + #[inline(always)] + pure fn iter_err(&self, f: fn(&E)) { iter_err(self, f) } + + #[inline(always)] + pure fn unwrap(self) -> T { unwrap(self) } + + #[inline(always)] + pure fn unwrap_err(self) -> T { unwrap(self) } + + #[inline(always)] + pure fn chain<U>(self, op: fn(T) -> Result<U,E>) -> Result<U,E> { + chain(self, op) } - fn iter_err(f: fn(&E)) { - match self { - Ok(_) => (), - Err(ref e) => f(e) - } + #[inline(always)] + pure fn chain_err<F>(self, op: fn(E) -> Result<T,F>) -> Result<T,F> { + chain_err(self, op) } } impl<T: Copy, E> Result<T, E> { - pure fn get() -> T { get(&self) } + #[inline(always)] + pure fn get(&self) -> T { get(self) } - fn map_err<F:Copy>(op: fn(&E) -> F) -> Result<T,F> { - match self { - Ok(copy t) => Ok(t), - Err(ref e) => Err(op(e)) - } + #[inline(always)] + pure fn map_err<F:Copy>(&self, op: fn(&E) -> F) -> Result<T,F> { + map_err(self, op) } } impl<T, E: Copy> Result<T, E> { - pure fn get_err() -> E { get_err(&self) } - - fn map<U:Copy>(op: fn(&T) -> U) -> Result<U,E> { - match self { - Ok(ref t) => Ok(op(t)), - Err(copy e) => Err(e) - } - } -} - -impl<T: Copy, E: Copy> Result<T, E> { - fn chain<U:Copy>(op: fn(t: T) -> Result<U,E>) -> Result<U,E> { - // XXX: Bad copy - chain(copy self, op) - } + #[inline(always)] + pure fn get_err(&self) -> E { get_err(self) } - fn chain_err<F:Copy>(op: fn(t: E) -> Result<T,F>) -> Result<T,F> { - // XXX: Bad copy - chain_err(copy self, op) + #[inline(always)] + pure fn map<U:Copy>(&self, op: fn(&T) -> U) -> Result<U,E> { + map(self, op) } } @@ -360,7 +357,8 @@ pub fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T], } /// Unwraps a result, assuming it is an `ok(T)` -pub fn unwrap<T, U>(res: Result<T, U>) -> T { +#[inline(always)] +pub pure fn unwrap<T, U>(res: Result<T, U>) -> T { match move res { Ok(move t) => move t, Err(_) => fail ~"unwrap called on an err result" @@ -368,7 +366,8 @@ pub fn unwrap<T, U>(res: Result<T, U>) -> T { } /// Unwraps a result, assuming it is an `err(U)` -pub fn unwrap_err<T, U>(res: Result<T, U>) -> U { +#[inline(always)] +pub pure fn unwrap_err<T, U>(res: Result<T, U>) -> U { match move res { Err(move u) => move u, Ok(_) => fail ~"unwrap called on an ok result" |
