From a33966b2f2a93b5108d0fd4464b18912da270e15 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 13 Jan 2013 00:47:00 -0500 Subject: [PATCH] add Option methods for swap_unwrap and map_consume --- src/libcore/option.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 49ba10dfffb..21581297894 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -264,6 +264,13 @@ impl Option { #[inline(always)] pure fn map(&self, f: fn(x: &T) -> U) -> Option { map(self, f) } + /// As `map`, but consumes the option and gives `f` ownership to avoid + /// copying. + #[inline(always)] + pure fn map_consume(self, f: fn(v: T) -> U) -> Option { + map_consume(self, f) + } + /// Applies a function to the contained value or returns a default #[inline(always)] pure fn map_default(&self, def: U, f: fn(x: &T) -> U) -> U { @@ -301,6 +308,17 @@ impl Option { #[inline(always)] pure fn unwrap(self) -> T { unwrap(self) } + /** + * The option dance. Moves a value out of an option type and returns it, + * replacing the original with `None`. + * + * # Failure + * + * Fails if the value equals `None`. + */ + #[inline(always)] + fn swap_unwrap(&mut self) -> T { swap_unwrap(self) } + /** * Gets the value out of an option, printing a specified message on * failure