Skip to content

Commit

Permalink
Rename {copied,cloned} to {copied,cloned}_ok, and add {copied,cloned}…
Browse files Browse the repository at this point in the history
… to copy/clone both Ok and Err
  • Loading branch information
ksqsf committed Jul 31, 2019
1 parent 5a36b0d commit 6c13081
Showing 1 changed file with 140 additions and 4 deletions.
144 changes: 140 additions & 4 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ impl<T: Copy, E> Result<&T, E> {
/// assert_eq!(copied, Ok(12));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
pub fn copied_ok(self) -> Result<T, E> {
self.map(|&t| t)
}
}
Expand All @@ -855,7 +855,7 @@ impl<T: Copy, E> Result<&mut T, E> {
/// assert_eq!(copied, Ok(12));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
pub fn copied_ok(self) -> Result<T, E> {
self.map(|&mut t| t)
}
}
Expand Down Expand Up @@ -900,6 +900,74 @@ impl<T, E: Copy> Result<T, &mut E> {
}
}

impl<T: Copy, E: Copy> Result<&T, &E> {
/// Maps a `Result<&T, &E>` to a `Result<T, E>` by copying the
/// contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_copied)]
/// assert_eq!(Err(&1), Err(1));
/// assert_eq!(Ok(&42), Ok(42));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
self.copied_ok().copied_err()
}
}

impl<T: Copy, E: Copy> Result<&mut T, &E> {
/// Maps a `Result<&mut T, &E>` to a `Result<T, E>` by copying the
/// contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_copied)]
/// assert_eq!(Err(&1), Err(1));
/// assert_eq!(Ok(&mut 42), Ok(42));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
self.copied_ok().copied_err()
}
}

impl<T: Copy, E: Copy> Result<&T, &mut E> {
/// Maps a `Result<&T, &mut E>` to a `Result<T, E>` by copying the
/// contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_copied)]
/// assert_eq!(Err(&mut 1), Err(1));
/// assert_eq!(Ok(&42), Ok(42));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
self.copied_ok().copied_err()
}
}

impl<T: Copy, E: Copy> Result<&mut T, &mut E> {
/// Maps a `Result<&mut T, &mut E>` to a `Result<T, E>` by copying
/// the contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_copied)]
/// assert_eq!(Err(&mut 1), Err(1));
/// assert_eq!(Ok(&mut 42), Ok(42));
/// ```
#[unstable(feature = "result_copied", reason = "newly added", issue = "63168")]
pub fn copied(self) -> Result<T, E> {
self.copied_ok().copied_err()
}
}

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.
Expand All @@ -915,7 +983,7 @@ impl<T: Clone, E> Result<&T, E> {
/// assert_eq!(cloned, Ok(12));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
pub fn cloned_ok(self) -> Result<T, E> {
self.map(|t| t.clone())
}
}
Expand All @@ -935,7 +1003,7 @@ impl<T: Clone, E> Result<&mut T, E> {
/// assert_eq!(cloned, Ok(12));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
pub fn cloned_ok(self) -> Result<T, E> {
self.map(|t| t.clone())
}
}
Expand Down Expand Up @@ -980,6 +1048,74 @@ impl<T, E: Clone> Result<T, &mut E> {
}
}

impl<T: Clone, E: Clone> Result<&T, &E> {
/// Maps a `Result<&T, &E>` to a `Result<T, E>` by cloning the contents of the
/// result.
///
/// # Examples
///
/// ```
/// #![feature(result_cloned)]
/// assert_eq!(Err(&1).cloned(), Err(1));
/// assert_eq!(Ok(&42).cloned(), Ok(42));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
self.cloned_ok().cloned_err()
}
}

impl<T: Clone, E: Clone> Result<&mut T, &E> {
/// Maps a `Result<&mut T, &E>` to a `Result<T, E>` by cloning the
/// contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_cloned)]
/// assert_eq!(Err(&1).cloned(), Err(1));
/// assert_eq!(Ok(&mut 42).cloned(), Ok(42));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
self.cloned_ok().cloned_err()
}
}

impl<T: Clone, E: Clone> Result<&T, &mut E> {
/// Maps a `Result<&T, &mut E>` to a `Result<T, E>` by cloning the
/// contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_cloned)]
/// assert_eq!(Err(&mut 1).cloned(), Err(1));
/// assert_eq!(Ok(&42).cloned(), Ok(42));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
self.cloned_ok().cloned_err()
}
}

impl<T: Clone, E: Clone> Result<&mut T, &mut E> {
/// Maps a `Result<&mut T, &mut E>` to a `Result<T, E>` by cloning
/// the contents of the result.
///
/// # Examples
///
/// ```
/// #![feature(result_cloned)]
/// assert_eq!(Err(&mut 1).cloned(), Err(1));
/// assert_eq!(Ok(&mut 42).cloned(), Ok(42));
/// ```
#[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")]
pub fn cloned(self) -> Result<T, E> {
self.cloned_ok().cloned_err()
}
}

impl<T, E: fmt::Debug> Result<T, E> {
/// Unwraps a result, yielding the content of an [`Ok`].
///
Expand Down

0 comments on commit 6c13081

Please sign in to comment.