Skip to content

Commit e57745b

Browse files
committed
option: remove map's copy restriction and add map_consume
1 parent 06c42b7 commit e57745b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/libcore/option.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,20 @@ pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
3737
alt opt { some(x) { x } none { fail reason; } }
3838
}
3939

40-
pure fn map<T, U: copy>(opt: option<T>, f: fn(T) -> U) -> option<U> {
40+
pure fn map<T, U>(opt: option<T>, f: fn(T) -> U) -> option<U> {
4141
//! Maps a `some` value from one type to another
4242
4343
alt opt { some(x) { some(f(x)) } none { none } }
4444
}
4545

46+
pure fn map_consume<T, U>(-opt: option<T>, f: fn(-T) -> U) -> option<U> {
47+
/*!
48+
* As `map`, but consumes the option and gives `f` ownership to avoid
49+
* copying.
50+
*/
51+
if opt.is_some() { some(f(option::unwrap(opt))) } else { none }
52+
}
53+
4654
pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U> {
4755
/*!
4856
* Update an optional value by optionally running its content through a
@@ -128,7 +136,7 @@ impl extensions<T> for option<T> {
128136
/// Returns true if the option contains some value
129137
pure fn is_some() -> bool { is_some(self) }
130138
/// Maps a `some` value from one type to another
131-
pure fn map<U:copy>(f: fn(T) -> U) -> option<U> { map(self, f) }
139+
pure fn map<U>(f: fn(T) -> U) -> option<U> { map(self, f) }
132140
}
133141

134142
impl extensions<T: copy> for option<T> {

0 commit comments

Comments
 (0)