Skip to content

Commit a33966b

Browse files
committed
add Option methods for swap_unwrap and map_consume
1 parent 802d475 commit a33966b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/libcore/option.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ impl<T> Option<T> {
264264
#[inline(always)]
265265
pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
266266

267+
/// As `map`, but consumes the option and gives `f` ownership to avoid
268+
/// copying.
269+
#[inline(always)]
270+
pure fn map_consume<U>(self, f: fn(v: T) -> U) -> Option<U> {
271+
map_consume(self, f)
272+
}
273+
267274
/// Applies a function to the contained value or returns a default
268275
#[inline(always)]
269276
pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U {
@@ -301,6 +308,17 @@ impl<T> Option<T> {
301308
#[inline(always)]
302309
pure fn unwrap(self) -> T { unwrap(self) }
303310

311+
/**
312+
* The option dance. Moves a value out of an option type and returns it,
313+
* replacing the original with `None`.
314+
*
315+
* # Failure
316+
*
317+
* Fails if the value equals `None`.
318+
*/
319+
#[inline(always)]
320+
fn swap_unwrap(&mut self) -> T { swap_unwrap(self) }
321+
304322
/**
305323
* Gets the value out of an option, printing a specified message on
306324
* failure

0 commit comments

Comments
 (0)