diff options
Diffstat (limited to 'src/libstd/option.rs')
| -rw-r--r-- | src/libstd/option.rs | 83 |
1 files changed, 24 insertions, 59 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 5c7ae63d391..cdff32a46dc 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -96,6 +96,30 @@ impl<T: ToStr> ToStr for Option<T> { } impl<T> Option<T> { + /// Convert from `Option<T>` to `Option<&T>` + #[inline] + pub fn as_ref<'r>(&'r self) -> Option<&'r T> { + match *self { Some(ref x) => Some(x), None => None } + } + + /// Convert from `Option<T>` to `Option<&mut T>` + #[inline] + pub fn as_mut<'r>(&'r mut self) -> Option<&'r mut T> { + match *self { Some(ref mut x) => Some(x), None => None } + } + + /// Maps an `Option<T>` to `Option<U>` by applying a function to a contained value. + #[inline] + pub fn map<U>(self, f: &fn(T) -> U) -> Option<U> { + match self { Some(x) => Some(f(x)), None => None } + } + + /// Applies a function to the contained value or returns a default. + #[inline] + pub fn map_default<U>(self, def: U, f: &fn(T) -> U) -> U { + match self { None => def, Some(t) => f(t) } + } + /// Return an iterator over the possibly contained value #[inline] pub fn iter<'r>(&'r self) -> OptionIterator<&'r T> { @@ -149,26 +173,6 @@ impl<T> Option<T> { } } - /// Returns `None` if the option is `None`, otherwise calls `f` with a - /// reference to the wrapped value and returns the result. - #[inline] - pub fn and_then_ref<'a, U>(&'a self, f: &fn(&'a T) -> Option<U>) -> Option<U> { - match *self { - Some(ref x) => f(x), - None => None - } - } - - /// Returns `None` if the option is `None`, otherwise calls `f` with a - /// mutable reference to the wrapped value and returns the result. - #[inline] - pub fn and_then_mut_ref<'a, U>(&'a mut self, f: &fn(&'a mut T) -> Option<U>) -> Option<U> { - match *self { - Some(ref mut x) => f(x), - None => None - } - } - /// Returns the option if it contains a value, otherwise returns `optb`. #[inline] pub fn or(self, optb: Option<T>) -> Option<T> { @@ -197,45 +201,6 @@ impl<T> Option<T> { } } - /// 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<U> { - match *self { Some(ref x) => Some(f(x)), None => None } - } - - /// 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<U> { - match *self { Some(ref mut x) => Some(f(x)), None => None } - } - - /// Applies a function to the contained value or returns a default - #[inline] - pub fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U { - match *self { None => def, Some(ref t) => f(t) } - } - - /// 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 { - match *self { Some(ref mut x) => f(x), None => def } - } - - /// As `map`, but consumes the option and gives `f` ownership to avoid - /// copying. - #[inline] - pub fn map_move<U>(self, f: &fn(T) -> U) -> Option<U> { - match self { Some(x) => Some(f(x)), None => None } - } - - /// As `map_default`, but consumes the option and gives `f` - /// ownership to avoid copying. - #[inline] - pub fn map_move_default<U>(self, def: U, f: &fn(T) -> U) -> U { - match self { None => def, Some(t) => f(t) } - } - /// Take the value out of the option, leaving a `None` in its place. #[inline] pub fn take(&mut self) -> Option<T> { |
