Skip to content

Commit a0ab5a3

Browse files
committed
Add Result::cloned{,_err} and Result::copied{,_err}
1 parent 9152fe4 commit a0ab5a3

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

src/libcore/result.rs

+160
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,166 @@ 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 = 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 = "XXXXX")]
838+
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 val = 12;
852+
/// let x = 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 = "XXXXX")]
858+
fn copied(self) -> Result<T, E> {
859+
self.map(|&mut t| t)
860+
}
861+
}
862+
863+
impl<T, E: Copy> Result<T, &E> {
864+
/// Maps a `Result<T, &E>` to a `Result<T, E>` by copying the contents of the
865+
/// `Err` part.
866+
///
867+
/// # Examples
868+
///
869+
/// ```
870+
/// #![feature(result_copied)]
871+
/// let val = 12;
872+
/// let x = Err(&val);
873+
/// assert_eq!(x, Err(&12));
874+
/// let copied = x.copied_err();
875+
/// assert_eq!(copied, Err(12));
876+
/// ```
877+
#[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")]
878+
fn copied_err(self) -> Result<T, E> {
879+
self.map_err(|&e| e)
880+
}
881+
}
882+
883+
impl<T, E: Copy> Result<T, &mut E> {
884+
/// Maps a `Result<T, &mut E>` to a `Result<T, E>` by copying the contents of the
885+
/// `Err` part.
886+
///
887+
/// # Examples
888+
///
889+
/// ```
890+
/// #![feature(result_copied)]
891+
/// let val = 12;
892+
/// let x = Err(&mut val);
893+
/// assert_eq!(x, Err(&mut 12));
894+
/// let copied = x.copied();
895+
/// assert_eq!(cloned, Err(12));
896+
/// ```
897+
#[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")]
898+
fn copied_err(self) -> Result<T, E> {
899+
self.map_err(|&mut e| e)
900+
}
901+
}
902+
903+
impl<T: Clone, E> Result<&T, E> {
904+
/// Maps a `Result<&T, E>` to a `Result<T, E>` by cloning the contents of the
905+
/// `Ok` part.
906+
///
907+
/// # Examples
908+
///
909+
/// ```
910+
/// #![feature(result_cloned)]
911+
/// let val = 12;
912+
/// let x = Ok(&val);
913+
/// assert_eq!(x, Ok(&12));
914+
/// let cloned = x.cloned();
915+
/// assert_eq!(cloned, Ok(12));
916+
/// ```
917+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")]
918+
fn cloned(self) -> Result<T, E> {
919+
self.map(|t| t.clone())
920+
}
921+
}
922+
923+
impl<T: Clone, E> Result<&mut T, E> {
924+
/// Maps a `Result<&mut T, E>` to a `Result<T, E>` by cloning the contents of the
925+
/// `Ok` part.
926+
///
927+
/// # Examples
928+
///
929+
/// ```
930+
/// #![feature(result_cloned)]
931+
/// let val = 12;
932+
/// let x = Ok(&mut val);
933+
/// assert_eq!(x, Ok(&mut 12));
934+
/// let cloned = x.cloned();
935+
/// assert_eq!(cloned, Ok(12));
936+
/// ```
937+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")]
938+
fn cloned(self) -> Result<T, E> {
939+
self.map(|t| t.clone())
940+
}
941+
}
942+
943+
impl<T, E: Clone> Result<T, &mut E> {
944+
/// Maps a `Result<T, &E>` to a `Result<T, E>` by cloning the contents of the
945+
/// `Err` part.
946+
///
947+
/// # Examples
948+
///
949+
/// ```
950+
/// #![feature(result_cloned)]
951+
/// let val = 12;
952+
/// let x = Err(&mut val);
953+
/// assert_eq!(x, Err(&mut 12));
954+
/// let cloned = x.cloned();
955+
/// assert_eq!(cloned, Err(12));
956+
/// ```
957+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")]
958+
fn cloned_err(self) -> Result<T, E> {
959+
self.map_err(|e| e.clone())
960+
}
961+
}
962+
963+
impl<T, E: Clone> Result<T, &mut E> {
964+
/// Maps a `Result<T, &mut E>` to a `Result<T, E>` by cloning the contents of the
965+
/// `Err` part.
966+
///
967+
/// # Examples
968+
///
969+
/// ```
970+
/// #![feature(result_cloned)]
971+
/// let val = 12;
972+
/// let x = Err(&mut val);
973+
/// assert_eq!(x, Err(&mut 12));
974+
/// let cloned = x.cloned();
975+
/// assert_eq!(cloned, Err(12));
976+
/// ```
977+
#[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")]
978+
fn cloned_err(self) -> Result<T, E> {
979+
self.map_err(|e| e.clone())
980+
}
981+
}
982+
823983
impl<T, E: fmt::Debug> Result<T, E> {
824984
/// Unwraps a result, yielding the content of an [`Ok`].
825985
///

0 commit comments

Comments
 (0)