diff --git a/src/libcore/dvec.rs b/src/libcore/dvec.rs index 874a36eb9f0cb..b02a6f5c8b538 100644 --- a/src/libcore/dvec.rs +++ b/src/libcore/dvec.rs @@ -110,6 +110,9 @@ priv impl DVec { self.data = move data; } } + + #[inline(always)] + fn unwrap(self) -> ~[A] { unwrap(self) } } // In theory, most everything should work with any A, but in practice diff --git a/src/libcore/either.rs b/src/libcore/either.rs index 74b29f3a5f1ca..6c0254ff77902 100644 --- a/src/libcore/either.rs +++ b/src/libcore/either.rs @@ -142,6 +142,14 @@ pub pure fn unwrap_right(eith: Either) -> U { } } +impl Either { + #[inline(always)] + fn unwrap_left(self) -> T { unwrap_left(self) } + + #[inline(always)] + fn unwrap_right(self) -> U { unwrap_right(self) } +} + #[test] fn test_either_left() { let val = Left(10); diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs index ccefda41fdad5..e5090f02b41bc 100644 --- a/src/libcore/mutable.rs +++ b/src/libcore/mutable.rs @@ -73,6 +73,9 @@ impl Data { op(unsafe{transmute_immut(&mut self.value)}) } } + + #[inline(always)] + fn unwrap(self) -> T { unwrap(self) } } #[test] diff --git a/src/libcore/option.rs b/src/libcore/option.rs index f7de25bf021a5..a4f385ea12cd8 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -96,19 +96,6 @@ pub pure fn get_ref(opt: &r/Option) -> &r/T { } } -pub pure fn expect(opt: Option, 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(opt: &Option, f: fn(x: &T) -> U) -> Option { //! Maps a `some` value by reference from one type to another @@ -235,35 +222,46 @@ pub fn swap_unwrap(opt: &mut Option) -> T { unwrap(util::replace(opt, None)) } -pub pure fn unwrap_expect(opt: Option, reason: &str) -> T { +pub pure fn expect(opt: Option, 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, some should not. See below. impl Option { /// 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 &Option { /** * Update an optional value by optionally running its content by reference * through a function that returns an option. */ - pure fn chain_ref(f: fn(x: &T) -> Option) -> Option { + #[inline(always)] + pure fn chain_ref(&self, f: fn(x: &T) -> Option) -> Option { chain_ref(self, f) } + + /// Maps a `some` value from one type to another by reference + #[inline(always)] + pure fn map(&self, f: fn(x: &T) -> U) -> Option { map(self, f) } + /// Applies a function to the contained value or returns a default - pure fn map_default(def: U, f: fn(x: &T) -> U) -> U - { map_default(self, move def, f) } + #[inline(always)] + pure fn map_default(&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(f: fn(x: &T) -> U) -> Option { 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 &Option { 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 Option { @@ -296,19 +316,17 @@ impl Option { 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) { while_some(self, blk) } + #[inline(always)] + pure fn while_some(self, blk: fn(v: T) -> Option) { + while_some(self, blk) + } } #[test] diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index 914530c065309..59d59d3351896 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( p: RecvPacketBuffered) -> 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 PortSet : GenericPort { } 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 a1e7df28872a8..26064345b59f2 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -114,7 +114,7 @@ pub pure fn to_either(res: &Result) * ok(parse_bytes(buf)) * } */ -pub fn chain(res: Result, op: fn(t: T) +pub pure fn chain(res: Result, op: fn(T) -> Result) -> Result { match move res { Ok(move t) => op(move t), @@ -130,7 +130,7 @@ pub fn chain(res: Result, 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( +pub pure fn chain_err( res: Result, op: fn(t: V) -> Result) -> Result { @@ -154,7 +154,7 @@ pub fn chain_err( * print_buf(buf) * } */ -pub fn iter(res: &Result, f: fn(&T)) { +pub pure fn iter(res: &Result, f: fn(&T)) { match *res { Ok(ref t) => f(t), Err(_) => () @@ -169,7 +169,7 @@ pub fn iter(res: &Result, f: fn(&T)) { * This function can be used to pass through a successful result while * handling an error. */ -pub fn iter_err(res: &Result, f: fn(&E)) { +pub pure fn iter_err(res: &Result, f: fn(&E)) { match *res { Ok(_) => (), Err(ref e) => f(e) @@ -190,7 +190,7 @@ pub fn iter_err(res: &Result, f: fn(&E)) { * parse_bytes(buf) * } */ -pub fn map(res: &Result, op: fn(&T) -> U) +pub pure fn map(res: &Result, op: fn(&T) -> U) -> Result { match *res { Ok(ref t) => Ok(op(t)), @@ -206,7 +206,7 @@ pub fn map(res: &Result, 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(res: &Result, op: fn(&E) -> F) +pub pure fn map_err(res: &Result, op: fn(&E) -> F) -> Result { match *res { Ok(copy t) => Ok(t), @@ -215,58 +215,55 @@ pub fn map_err(res: &Result, op: fn(&E) -> F) } impl Result { + #[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(self, op: fn(T) -> Result) -> Result { + chain(self, op) } - fn iter_err(f: fn(&E)) { - match self { - Ok(_) => (), - Err(ref e) => f(e) - } + #[inline(always)] + pure fn chain_err(self, op: fn(E) -> Result) -> Result { + chain_err(self, op) } } impl Result { - pure fn get() -> T { get(&self) } + #[inline(always)] + pure fn get(&self) -> T { get(self) } - fn map_err(op: fn(&E) -> F) -> Result { - match self { - Ok(copy t) => Ok(t), - Err(ref e) => Err(op(e)) - } + #[inline(always)] + pure fn map_err(&self, op: fn(&E) -> F) -> Result { + map_err(self, op) } } impl Result { - pure fn get_err() -> E { get_err(&self) } - - fn map(op: fn(&T) -> U) -> Result { - match self { - Ok(ref t) => Ok(op(t)), - Err(copy e) => Err(e) - } - } -} - -impl Result { - fn chain(op: fn(t: T) -> Result) -> Result { - // XXX: Bad copy - chain(copy self, op) - } + #[inline(always)] + pure fn get_err(&self) -> E { get_err(self) } - fn chain_err(op: fn(t: E) -> Result) -> Result { - // XXX: Bad copy - chain_err(copy self, op) + #[inline(always)] + pure fn map(&self, op: fn(&T) -> U) -> Result { + map(self, op) } } @@ -360,7 +357,8 @@ pub fn iter_vec2(ss: &[S], ts: &[T], } /// Unwraps a result, assuming it is an `ok(T)` -pub fn unwrap(res: Result) -> T { +#[inline(always)] +pub pure fn unwrap(res: Result) -> T { match move res { Ok(move t) => move t, Err(_) => fail ~"unwrap called on an err result" @@ -368,7 +366,8 @@ pub fn unwrap(res: Result) -> T { } /// Unwraps a result, assuming it is an `err(U)` -pub fn unwrap_err(res: Result) -> U { +#[inline(always)] +pub pure fn unwrap_err(res: Result) -> U { match move res { Err(move u) => move u, Ok(_) => fail ~"unwrap called on an ok result" diff --git a/src/libstd/par.rs b/src/libstd/par.rs index 55f88d4427cef..10cbcaa0c3b3a 100644 --- a/src/libstd/par.rs +++ b/src/libstd/par.rs @@ -85,7 +85,7 @@ fn map_slices( /// A parallel version of map. pub fn map( - xs: &[A], f: fn~((&A)) -> B) -> ~[B] { + xs: &[A], f: fn~(&A) -> B) -> ~[B] { vec::concat(map_slices(xs, || { fn~(_base: uint, slice : &[A], copy f) -> ~[B] { vec::map(slice, |x| f(x)) @@ -95,7 +95,7 @@ pub fn map( /// A parallel version of mapi. pub fn mapi(xs: &[A], - f: fn~(uint, (&A)) -> B) -> ~[B] { + f: fn~(uint, &A) -> B) -> ~[B] { let slices = map_slices(xs, || { fn~(base: uint, slice : &[A], copy f) -> ~[B] { vec::mapi(slice, |i, x| { @@ -132,7 +132,7 @@ pub fn mapi_factory( } /// Returns true if the function holds for all elements in the vector. -pub fn alli(xs: &[A], f: fn~(uint, (&A)) -> bool) -> bool { +pub fn alli(xs: &[A], f: fn~(uint, &A) -> bool) -> bool { do vec::all(map_slices(xs, || { fn~(base: uint, slice : &[A], copy f) -> bool { vec::alli(slice, |i, x| { @@ -143,7 +143,7 @@ pub fn alli(xs: &[A], f: fn~(uint, (&A)) -> bool) -> bool { } /// Returns true if the function holds for any elements in the vector. -pub fn any(xs: &[A], f: fn~(&(A)) -> bool) -> bool { +pub fn any(xs: &[A], f: fn~(&A) -> bool) -> bool { do vec::any(map_slices(xs, || { fn~(_base : uint, slice: &[A], copy f) -> bool { vec::any(slice, |x| f(x))