Move Result::as_deref
This commit is contained in:
parent
f8d4ee7c7a
commit
eda61d8d8a
1 changed files with 24 additions and 23 deletions
|
@ -901,6 +901,30 @@ impl<T, E> Result<T, E> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&<T as Deref>::Target, &E>`.
|
||||
///
|
||||
/// Coerces the [`Ok`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
|
||||
/// and returns the new [`Result`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let x: Result<String, u32> = Ok("hello".to_string());
|
||||
/// let y: Result<&str, &u32> = Ok("hello");
|
||||
/// assert_eq!(x.as_deref(), y);
|
||||
///
|
||||
/// let x: Result<String, u32> = Err(42);
|
||||
/// let y: Result<&str, &u32> = Err(&42);
|
||||
/// assert_eq!(x.as_deref(), y);
|
||||
/// ```
|
||||
#[stable(feature = "inner_deref", since = "1.47.0")]
|
||||
pub fn as_deref(&self) -> Result<&T::Target, &E>
|
||||
where
|
||||
T: Deref,
|
||||
{
|
||||
self.as_ref().map(|t| t.deref())
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// Iterator constructors
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
|
@ -1508,29 +1532,6 @@ impl<T: Into<!>, E> Result<T, E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Deref, E> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&<T as Deref>::Target, &E>`.
|
||||
///
|
||||
/// Coerces the [`Ok`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
|
||||
/// and returns the new [`Result`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// let x: Result<String, u32> = Ok("hello".to_string());
|
||||
/// let y: Result<&str, &u32> = Ok("hello");
|
||||
/// assert_eq!(x.as_deref(), y);
|
||||
///
|
||||
/// let x: Result<String, u32> = Err(42);
|
||||
/// let y: Result<&str, &u32> = Err(&42);
|
||||
/// assert_eq!(x.as_deref(), y);
|
||||
/// ```
|
||||
#[stable(feature = "inner_deref", since = "1.47.0")]
|
||||
pub fn as_deref(&self) -> Result<&T::Target, &E> {
|
||||
self.as_ref().map(|t| t.deref())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: DerefMut, E> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
|
||||
///
|
||||
|
|
Loading…
Add table
Reference in a new issue