Skip to content

Commit 5f42f3e

Browse files
committed
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`.
2 parents b9de4ef + 61e5286 commit 5f42f3e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/libcore/result.rs

+81
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,87 @@ impl<T, E> Result<T, E> {
820820
}
821821
}
822822

823+
impl<T: Copy, E> Result<&T, E> {
824+
/// Maps a `Result<&T, E>` to a `Result<T, E>` by copying the contents of the
825+
/// `Ok` part.
826+
///
827+
/// # Examples
828+
///
829+
/// ```
830+
/// #![feature(result_copied)]
831+
/// let val = 12;
832+
/// let x: Result<&i32, i32> = Ok(&val);
833+
/// assert_eq!(x, Ok(&12));
834+
/// let copied = x.copied();
835+
/// assert_eq!(copied, Ok(12));
836+
/// ```
837+
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
838+
pub fn copied(self) -> Result<T, E> {
839+
self.map(|&t| t)
840+
}
841+
}
842+
843+
impl<T: Copy, E> Result<&mut T, E> {
844+
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by copying the contents of the
845+
/// `Ok` part.
846+
///
847+
/// # Examples
848+
///
849+
/// ```
850+
/// #![feature(result_copied)]
851+
/// let mut val = 12;
852+
/// let x: Result<&mut i32, i32> = Ok(&mut val);
853+
/// assert_eq!(x, Ok(&mut 12));
854+
/// let copied = x.copied();
855+
/// assert_eq!(copied, Ok(12));
856+
/// ```
857+
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
858+
pub fn copied(self) -> Result<T, E> {
859+
self.map(|&mut t| t)
860+
}
861+
}
862+
863+
impl<T: Clone, E> Result<&T, E> {
864+
/// Maps a `Result<&T, E>` to a `Result<T, E>` by cloning the contents of the
865+
/// `Ok` part.
866+
///
867+
/// # Examples
868+
///
869+
/// ```
870+
/// #![feature(result_cloned)]
871+
/// let val = 12;
872+
/// let x: Result<&i32, i32> = Ok(&val);
873+
/// assert_eq!(x, Ok(&12));
874+
/// let cloned = x.cloned();
875+
/// assert_eq!(cloned, Ok(12));
876+
/// ```
877+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
878+
pub fn cloned(self) -> Result<T, E> {
879+
self.map(|t| t.clone())
880+
}
881+
}
882+
883+
impl<T: Clone, E> Result<&mut T, E> {
884+
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by cloning the contents of the
885+
/// `Ok` part.
886+
///
887+
/// # Examples
888+
///
889+
/// ```
890+
/// #![feature(result_cloned)]
891+
/// let mut val = 12;
892+
/// let x: Result<&mut i32, i32> = Ok(&mut val);
893+
/// assert_eq!(x, Ok(&mut 12));
894+
/// let cloned = x.cloned();
895+
/// assert_eq!(cloned, Ok(12));
896+
/// ```
897+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
898+
pub fn cloned(self) -> Result<T, E> {
899+
self.map(|t| t.clone())
900+
}
901+
}
902+
903+
823904
impl<T, E: fmt::Debug> Result<T, E> {
824905
/// Unwraps a result, yielding the content of an [`Ok`].
825906
///

0 commit comments

Comments
 (0)