diff options
Diffstat (limited to 'src/libstd/option.rs')
| -rw-r--r-- | src/libstd/option.rs | 212 |
1 files changed, 104 insertions, 108 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 417251d3740..3a4a9220ee1 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -47,7 +47,8 @@ use ops::Add; use util; use num::Zero; use iterator::Iterator; -use str::StrSlice; +use str::{StrSlice, OwnedStr}; +use to_str::ToStr; use clone::DeepClone; /// The option type @@ -85,18 +86,33 @@ impl<T:Ord> Ord for Option<T> { } } -impl<T:Clone+Add<T,T>> Add<Option<T>, Option<T>> for Option<T> { +impl<T: Add<T, T>> Add<Option<T>, Option<T>> for Option<T> { #[inline] fn add(&self, other: &Option<T>) -> Option<T> { match (&*self, &*other) { (&None, &None) => None, - (_, &None) => (*self).clone(), - (&None, _) => (*other).clone(), + (_, &None) => None, + (&None, _) => None, (&Some(ref lhs), &Some(ref rhs)) => Some(*lhs + *rhs) } } } +// FIXME: #8242 implementing manually because deriving doesn't work for some reason +impl<T: ToStr> ToStr for Option<T> { + fn to_str(&self) -> ~str { + match *self { + Some(ref x) => { + let mut s = ~"Some("; + s.push_str(x.to_str()); + s.push_str(")"); + s + } + None => ~"None" + } + } +} + impl<T> Option<T> { /// Return an iterator over the possibly contained value #[inline] @@ -148,8 +164,7 @@ impl<T> Option<T> { /// Update an optional value by optionally running its content by reference /// through a function that returns an option. #[inline] - pub fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>) - -> Option<U> { + pub fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>) -> Option<U> { match *self { Some(ref x) => f(x), None => None @@ -159,8 +174,7 @@ impl<T> Option<T> { /// 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<U>) - -> Option<U> { + pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>) -> Option<U> { match *self { Some(ref mut x) => f(x), None => None @@ -256,132 +270,105 @@ impl<T> Option<T> { } } - /** - Gets an immutable reference to the value inside an option. - - # Failure - - Fails if the value equals `None` - - # Safety note - - In general, because this function may fail, its use is discouraged - (calling `get` on `None` is akin to dereferencing a null pointer). - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ + /// Gets an immutable reference to the value inside an option. + /// + /// # Failure + /// + /// Fails if the value equals `None` + /// + /// # Safety note + /// + /// In general, because this function may fail, its use is discouraged + /// (calling `get` on `None` is akin to dereferencing a null pointer). + /// Instead, prefer to use pattern matching and handle the `None` + /// case explicitly. #[inline] pub fn get_ref<'a>(&'a self) -> &'a T { match *self { - Some(ref x) => x, - None => fail!("option::get_ref `None`"), + Some(ref x) => x, + None => fail!("called `Option::get_ref()` on a `None` value"), } } - /** - Gets a mutable reference to the value inside an option. - - # Failure - - Fails if the value equals `None` - - # Safety note - - In general, because this function may fail, its use is discouraged - (calling `get` on `None` is akin to dereferencing a null pointer). - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ + /// Gets a mutable reference to the value inside an option. + /// + /// # Failure + /// + /// Fails if the value equals `None` + /// + /// # Safety note + /// + /// In general, because this function may fail, its use is discouraged + /// (calling `get` on `None` is akin to dereferencing a null pointer). + /// Instead, prefer to use pattern matching and handle the `None` + /// case explicitly. #[inline] 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`"), + Some(ref mut x) => x, + None => fail!("called `Option::get_mut_ref()` on a `None` value"), } } + /// Moves a value out of an option type and returns it. + /// + /// Useful primarily for getting strings, vectors and unique pointers out + /// of option types without copying them. + /// + /// # Failure + /// + /// Fails if the value equals `None`. + /// + /// # Safety note + /// + /// In general, because this function may fail, its use is discouraged. + /// Instead, prefer to use pattern matching and handle the `None` + /// case explicitly. #[inline] pub fn unwrap(self) -> T { - /*! - Moves a value out of an option type and returns it. - - Useful primarily for getting strings, vectors and unique pointers out - of option types without copying them. - - # Failure - - Fails if the value equals `None`. - - # Safety note - - In general, because this function may fail, its use is discouraged. - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ match self { - Some(x) => x, - None => fail!("option::unwrap `None`"), + Some(x) => x, + None => fail!("called `Option::unwrap()` on a `None` value"), } } - /** - * 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`. - */ + /// 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] pub fn take_unwrap(&mut self) -> T { - if self.is_none() { fail!("option::take_unwrap `None`") } + if self.is_none() { + fail!("called `Option::take_unwrap()` on a `None` value") + } self.take().unwrap() } - /** - * Gets the value out of an option, printing a specified message on - * failure - * - * # Failure - * - * Fails if the value equals `None` - */ + /// Gets the value out of an option, printing a specified message on + /// failure + /// + /// # Failure + /// + /// Fails if the value equals `None` #[inline] pub fn expect(self, reason: &str) -> T { match self { - Some(val) => val, - None => fail!(reason.to_owned()), + Some(val) => val, + None => fail!(reason.to_owned()), } } - /** - Gets the value out of an option - - # Failure - - Fails if the value equals `None` - - # Safety note - - In general, because this function may fail, its use is discouraged - (calling `get` on `None` is akin to dereferencing a null pointer). - Instead, prefer to use pattern matching and handle the `None` - case explicitly. - */ + /// Returns the contained value or a default #[inline] - pub fn get(self) -> T { + pub fn unwrap_or_default(self, def: T) -> T { match self { - Some(x) => return x, - None => fail!("option::get `None`") + Some(x) => x, + None => def } } - /// Returns the contained value or a default - #[inline] - pub fn get_or_default(self, def: T) -> T { - match self { Some(x) => x, None => def } - } - /// Applies a function zero or more times until the result is `None`. #[inline] pub fn while_some(self, blk: &fn(v: T) -> Option<T>) { @@ -395,12 +382,21 @@ impl<T> Option<T> { impl<T:Zero> Option<T> { /// Returns the contained value or zero (for this type) #[inline] - pub fn get_or_zero(self) -> T { + pub fn unwrap_or_zero(self) -> T { match self { Some(x) => x, None => Zero::zero() } } + + /// Returns self or `Some(zero)` (for this type) + #[inline] + pub fn or_zero(self) -> Option<T> { + match self { + None => Some(Zero::zero()), + x => x + } + } } impl<T> Zero for Option<T> { @@ -450,7 +446,7 @@ mod tests { use util; #[test] - fn test_unwrap_ptr() { + fn test_get_ptr() { unsafe { let x = ~0; let addr_x: *int = ::cast::transmute(&*x); @@ -462,7 +458,7 @@ mod tests { } #[test] - fn test_unwrap_str() { + fn test_get_str() { let x = ~"test"; let addr_x = x.as_imm_buf(|buf, _len| buf); let opt = Some(x); @@ -472,7 +468,7 @@ mod tests { } #[test] - fn test_unwrap_resource() { + fn test_get_resource() { struct R { i: @mut int, } @@ -530,18 +526,18 @@ mod tests { } #[test] - fn test_get_or_zero() { + fn test_unwrap_or_zero() { let some_stuff = Some(42); - assert_eq!(some_stuff.get_or_zero(), 42); + assert_eq!(some_stuff.unwrap_or_zero(), 42); let no_stuff: Option<int> = None; - assert_eq!(no_stuff.get_or_zero(), 0); + assert_eq!(no_stuff.unwrap_or_zero(), 0); } #[test] fn test_filtered() { let some_stuff = Some(42); let modified_stuff = some_stuff.filtered(|&x| {x < 10}); - assert_eq!(some_stuff.get(), 42); + assert_eq!(some_stuff.unwrap(), 42); assert!(modified_stuff.is_none()); } |
