From a33966b2f2a93b5108d0fd4464b18912da270e15 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 13 Jan 2013 00:47:00 -0500 Subject: [PATCH 1/4] 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 From 65a0125f7f2e016a066bb3e8944b6ec31d2d46c7 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 13 Jan 2013 00:56:50 -0500 Subject: [PATCH 2/4] add Option map_consume_default method --- src/libcore/option.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 21581297894..6a5e90c58ad 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -277,6 +277,13 @@ impl Option { 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) } + } + /// Performs an operation on the contained value by reference #[inline(always)] pure fn iter(&self, f: fn(x: &T)) { iter(self, f) } From 3c6da7761bf74c54c1d56ba85f4ef55004abcb0a Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 13 Jan 2013 01:38:20 -0500 Subject: [PATCH 3/4] avoid unwrap in Option::map_consume implementation --- src/libcore/option.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 6a5e90c58ad..3fb2acd3480 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -114,7 +114,7 @@ pub enum 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, From ac85bf356f3674ed0e0e79668b384141d38e0efe Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sun, 13 Jan 2013 11:09:09 -0500 Subject: [PATCH 4/4] add Option methods for in-place mutation --- src/libcore/option.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 3fb2acd3480..40c6315bfe9 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -284,6 +284,22 @@ impl Option { 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) }