From 5991c607501595b32998a3367171ca42f5d8ef0b Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Sat, 20 Jul 2013 19:19:13 +0200 Subject: [PATCH 1/2] option: Title-case `Some` and `None` in docs and fail messages For accuracy, say 'get_ref None' instead of 'get_ref none', and so on. --- src/libstd/option.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index f5e5dbb3dbf7..b68c67e8903b 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -119,13 +119,13 @@ impl Option { } } - /// Returns true if the option equals `none` + /// Returns true if the option equals `None` #[inline] pub fn is_none(&self) -> bool { match *self { None => true, Some(_) => false } } - /// Returns true if the option contains some value + /// Returns true if the option contains a `Some` value #[inline] pub fn is_some(&self) -> bool { !self.is_none() } @@ -168,19 +168,19 @@ impl Option { } } - /// Maps a `some` value from one type to another by reference + /// Maps a `Some` value from one type to another by reference #[inline] pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option { match *self { Some(ref x) => Some(f(x)), None => None } } - /// Maps a `some` value from one type to another by a mutable reference + /// Maps a `Some` value from one type to another by a mutable reference #[inline] pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option { match *self { Some(ref mut x) => Some(f(x)), None => None } } - /// Maps a `some` value from one type to another by a mutable reference, + /// Maps a `Some` value from one type to another by a mutable reference, /// or returns a default value. #[inline] pub fn map_mut_default<'a, U>(&'a mut self, def: U, f: &fn(&'a mut T) -> U) -> U { @@ -261,7 +261,7 @@ impl Option { pub fn get_ref<'a>(&'a self) -> &'a T { match *self { Some(ref x) => x, - None => fail!("option::get_ref none") + None => fail!("option::get_ref None") } } @@ -283,7 +283,7 @@ impl Option { pub fn get_mut_ref<'a>(&'a mut self) -> &'a mut T { match *self { Some(ref mut x) => x, - None => fail!("option::get_mut_ref none") + None => fail!("option::get_mut_ref None") } } @@ -307,7 +307,7 @@ impl Option { */ match self { Some(x) => x, - None => fail!("option::unwrap none") + None => fail!("option::unwrap None") } } @@ -321,7 +321,7 @@ impl Option { */ #[inline] pub fn take_unwrap(&mut self) -> T { - if self.is_none() { fail!("option::take_unwrap none") } + if self.is_none() { fail!("option::take_unwrap None") } self.take().unwrap() } @@ -331,7 +331,7 @@ impl Option { * * # Failure * - * Fails if the value equals `none` + * Fails if the value equals `None` */ #[inline] pub fn expect(self, reason: &str) -> T { @@ -359,7 +359,7 @@ impl Option { pub fn get(self) -> T { match self { Some(x) => return x, - None => fail!("option::get none") + None => fail!("option::get None") } } @@ -369,7 +369,7 @@ impl Option { match self { Some(x) => x, None => def } } - /// Applies a function zero or more times until the result is none. + /// Applies a function zero or more times until the result is None. #[inline] pub fn while_some(self, blk: &fn(v: T) -> Option) { let mut opt = self; From 625ca7afe4fb50758215d623d45de84a5f4290ae Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Sat, 20 Jul 2013 19:19:13 +0200 Subject: [PATCH 2/2] option: Add .chain_mut_ref() .chain_mut_ref() is the mutable alternative to .chain_ref(). A use case example for this method is extraction of an optional value from an Option value. --- src/libstd/option.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index b68c67e8903b..f116c1127ca9 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -159,6 +159,17 @@ impl Option { } } + /// Update an optional value by optionally running its content by mut reference + /// through a function that returns an option. + #[inline] + pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option) + -> Option { + match *self { + Some(ref mut x) => f(x), + None => None + } + } + /// Filters an optional value using given function. #[inline(always)] pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option {