Consolidate impl Result<&T, E>

This commit is contained in:
David Tolnay 2021-12-30 10:30:28 -08:00
parent b2df61fa9f
commit e63e2680da
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -1493,7 +1493,7 @@ impl<T, E> Result<T, E> {
}
}
impl<T: Copy, E> Result<&T, E> {
impl<T, E> Result<&T, E> {
/// Maps a `Result<&T, E>` to a `Result<T, E>` by copying the contents of the
/// `Ok` part.
///
@ -1508,9 +1508,33 @@ 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(self) -> Result<T, E>
where
T: Copy,
{
self.map(|&t| t)
}
/// 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>
where
T: Clone,
{
self.map(|t| t.clone())
}
}
impl<T: Copy, E> Result<&mut T, E> {
@ -1533,26 +1557,6 @@ impl<T: Copy, E> Result<&mut T, E> {
}
}
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.