diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 49ba10dfffbe7..40c6315bfe9a2 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -114,7 +114,7 @@ pub pure fn map_consume(opt: Option, * As `map`, but consumes the option and gives `f` ownership to avoid * copying. */ - if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None } + match opt { None => None, Some(v) => Some(f(v)) } } pub pure fn chain(opt: Option, @@ -264,12 +264,42 @@ 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 { map_default(self, move def, f) } + /// As `map_default`, but consumes the option and gives `f` + /// ownership to avoid copying. + #[inline(always)] + pure fn map_consume_default(self, def: U, f: fn(v: T) -> U) -> U { + match self { None => def, Some(v) => f(v) } + } + + /// Apply a function to the contained value or do nothing + fn mutate(&mut self, f: fn(T) -> T) { + if self.is_some() { + *self = Some(f(self.swap_unwrap())); + } + } + + /// Apply a function to the contained value or set it to a default + fn mutate_default(&mut self, def: T, f: fn(T) -> T) { + if self.is_some() { + *self = Some(f(self.swap_unwrap())); + } else { + *self = Some(def); + } + } + /// Performs an operation on the contained value by reference #[inline(always)] pure fn iter(&self, f: fn(x: &T)) { iter(self, f) } @@ -301,6 +331,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