diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-02-07 18:30:34 -0500 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-02-07 22:04:38 -0500 |
| commit | e01824477751561fa0c1c13db13d0fbda341008e (patch) | |
| tree | 883dda4c4ac8c5c4e4a8c48c89cfbf90e5306a65 | |
| parent | f6e0df6563830a930188760bbefdc080d9c0902f (diff) | |
| download | rust-e01824477751561fa0c1c13db13d0fbda341008e.tar.gz rust-e01824477751561fa0c1c13db13d0fbda341008e.zip | |
make Option's map and map_default use a lifetime
| -rw-r--r-- | src/libcore/option.rs | 10 | ||||
| -rw-r--r-- | src/libstd/treemap.rs | 14 |
2 files changed, 9 insertions, 15 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index e5d703eec4a..cfc2cba9226 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -102,7 +102,7 @@ pub pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T { } #[inline(always)] -pub pure fn map<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> { +pub pure fn map<T, U>(opt: &r/Option<T>, f: fn(x: &r/T) -> U) -> Option<U> { //! Maps a `some` value by reference from one type to another match *opt { Some(ref x) => Some(f(x)), None => None } @@ -193,8 +193,8 @@ pub pure fn get_or_default<T: Copy>(opt: Option<T>, def: T) -> T { } #[inline(always)] -pub pure fn map_default<T, U>(opt: &Option<T>, def: U, - f: fn(x: &T) -> U) -> U { +pub pure fn map_default<T, U>(opt: &r/Option<T>, def: U, + f: fn(&r/T) -> U) -> U { //! Applies a function to the contained value or returns a default match *opt { None => move def, Some(ref t) => f(t) } @@ -273,7 +273,7 @@ impl<T> Option<T> { /// Maps a `some` value from one type to another by reference #[inline(always)] - pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) } + pure fn map<U>(&self, f: fn(&self/T) -> U) -> Option<U> { map(self, f) } /// As `map`, but consumes the option and gives `f` ownership to avoid /// copying. @@ -284,7 +284,7 @@ impl<T> Option<T> { /// Applies a function to the contained value or returns a default #[inline(always)] - pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U { + pure fn map_default<U>(&self, def: U, f: fn(&self/T) -> U) -> U { map_default(self, move def, f) } diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index e79025a7955..da83c7b789b 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -556,24 +556,18 @@ impl <K: Ord, V> TreeNode<K, V> { pure fn each<K: Ord, V>(node: &r/Option<~TreeNode<K, V>>, f: fn(&(&r/K, &r/V)) -> bool) { - match *node { - Some(ref x) => { + do node.map |x| { each(&x.left, f); if f(&(&x.key, &x.value)) { each(&x.right, f) } - } - None => () - } + }; } pure fn each_reverse<K: Ord, V>(node: &r/Option<~TreeNode<K, V>>, f: fn(&(&r/K, &r/V)) -> bool) { - match *node { - Some(ref x) => { + do node.map |x| { each_reverse(&x.right, f); if f(&(&x.key, &x.value)) { each_reverse(&x.left, f) } - } - None => () - } + }; } // Remove left horizontal link by rotating right |
