about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2012-07-17 18:24:51 -0400
committerBen Blum <bblum@andrew.cmu.edu>2012-07-17 20:03:14 -0400
commite57745b48c6a032da10d497e2bee6c2b2a11637b (patch)
tree869c910af80662bfcb58f62723e55797fe12c60d /src
parent06c42b77d39ff0fa2e04b46aa7bc81bef0c1cfac (diff)
downloadrust-e57745b48c6a032da10d497e2bee6c2b2a11637b.tar.gz
rust-e57745b48c6a032da10d497e2bee6c2b2a11637b.zip
option: remove map's copy restriction and add map_consume
Diffstat (limited to 'src')
-rw-r--r--src/libcore/option.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index 2b9794987ee..0ad0cb6045b 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -37,12 +37,20 @@ pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
     alt opt { some(x) { x } none { fail reason; } }
 }
 
-pure fn map<T, U: copy>(opt: option<T>, f: fn(T) -> U) -> option<U> {
+pure fn map<T, U>(opt: option<T>, f: fn(T) -> U) -> option<U> {
     //! Maps a `some` value from one type to another
 
     alt opt { some(x) { some(f(x)) } none { none } }
 }
 
+pure fn map_consume<T, U>(-opt: option<T>, f: fn(-T) -> U) -> option<U> {
+    /*!
+     * As `map`, but consumes the option and gives `f` ownership to avoid
+     * copying.
+     */
+    if opt.is_some() { some(f(option::unwrap(opt))) } else { none }
+}
+
 pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U> {
     /*!
      * Update an optional value by optionally running its content through a
@@ -128,7 +136,7 @@ impl extensions<T> for option<T> {
     /// Returns true if the option contains some value
     pure fn is_some() -> bool { is_some(self) }
     /// Maps a `some` value from one type to another
-    pure fn map<U:copy>(f: fn(T) -> U) -> option<U> { map(self, f) }
+    pure fn map<U>(f: fn(T) -> U) -> option<U> { map(self, f) }
 }
 
 impl extensions<T: copy> for option<T> {