Auto merge of #63166 - ksqsf:master, r=alexcrichton
Add Result::cloned{,_err} and Result::copied{,_err} This is a little nice addition to `Result`. 1. I'm not sure how useful are `cloned_err` and `copied_err`, but for the sake of completeness they are here. 2. Naming is similar to `map`/`map_err`. I thought about naming `cloned` as `cloned_ok` and add another method called `cloned` that clones both Ok and Err, but `cloned_ok` should be more prevalent than `cloned_both`.
This commit is contained in:
commit
5f42f3e108
1 changed files with 81 additions and 0 deletions
|
@ -820,6 +820,87 @@ impl<T, E> Result<T, E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, E> Result<&T, E> {
|
||||
/// Maps a `Result<&T, E>` to a `Result<T, E>` by copying the contents of the
|
||||
/// `Ok` part.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(result_copied)]
|
||||
/// let val = 12;
|
||||
/// let x: Result<&i32, i32> = Ok(&val);
|
||||
/// assert_eq!(x, Ok(&12));
|
||||
/// let copied = x.copied();
|
||||
/// assert_eq!(copied, Ok(12));
|
||||
/// ```
|
||||
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
|
||||
pub fn copied(self) -> Result<T, E> {
|
||||
self.map(|&t| t)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy, E> Result<&mut T, E> {
|
||||
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by copying the contents of the
|
||||
/// `Ok` part.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(result_copied)]
|
||||
/// let mut val = 12;
|
||||
/// let x: Result<&mut i32, i32> = Ok(&mut val);
|
||||
/// assert_eq!(x, Ok(&mut 12));
|
||||
/// let copied = x.copied();
|
||||
/// assert_eq!(copied, Ok(12));
|
||||
/// ```
|
||||
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
|
||||
pub fn copied(self) -> Result<T, E> {
|
||||
self.map(|&mut t| t)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone, E> Result<&T, E> {
|
||||
/// Maps a `Result<&T, E>` to a `Result<T, E>` by cloning the contents of the
|
||||
/// `Ok` part.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(result_cloned)]
|
||||
/// let val = 12;
|
||||
/// let x: Result<&i32, i32> = Ok(&val);
|
||||
/// assert_eq!(x, Ok(&12));
|
||||
/// let cloned = x.cloned();
|
||||
/// assert_eq!(cloned, Ok(12));
|
||||
/// ```
|
||||
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
|
||||
pub fn cloned(self) -> Result<T, E> {
|
||||
self.map(|t| t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone, E> Result<&mut T, E> {
|
||||
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by cloning the contents of the
|
||||
/// `Ok` part.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// #![feature(result_cloned)]
|
||||
/// let mut val = 12;
|
||||
/// let x: Result<&mut i32, i32> = Ok(&mut val);
|
||||
/// assert_eq!(x, Ok(&mut 12));
|
||||
/// let cloned = x.cloned();
|
||||
/// assert_eq!(cloned, Ok(12));
|
||||
/// ```
|
||||
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
|
||||
pub fn cloned(self) -> Result<T, E> {
|
||||
self.map(|t| t.clone())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<T, E: fmt::Debug> Result<T, E> {
|
||||
/// Unwraps a result, yielding the content of an [`Ok`].
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue