Skip to content

Commit e96f154

Browse files
authored
Rollup merge of #73447 - lzutao:stabilize-result_as_deref, r=dtolnay
Improve document for `Result::as_deref(_mut)` methods cc #50264
2 parents b3a7b40 + 4e77214 commit e96f154

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

src/libcore/result.rs

+40-16
Original file line numberDiff line numberDiff line change
@@ -1145,45 +1145,69 @@ impl<T, E: Into<!>> Result<T, E> {
11451145
}
11461146
}
11471147

1148-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1148+
#[unstable(feature = "inner_deref", issue = "50264")]
11491149
impl<T: Deref, E> Result<T, E> {
1150-
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
1150+
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&<T as Deref>::Target, &E>`.
11511151
///
1152-
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
1153-
/// `Ok` type's `Deref::Target` type.
1152+
/// Coerces the [`Ok`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
1153+
/// and returns the new [`Result`].
1154+
///
1155+
/// # Examples
1156+
///
1157+
/// ```
1158+
/// let x: Result<String, u32> = Ok("hello".to_string());
1159+
/// let y: Result<&str, &u32> = Ok("hello");
1160+
/// assert_eq!(x.as_deref(), y);
1161+
///
1162+
/// let x: Result<String, u32> = Err(42);
1163+
/// let y: Result<&str, &u32> = Err(&42);
1164+
/// assert_eq!(x.as_deref(), y);
1165+
/// ```
11541166
pub fn as_deref(&self) -> Result<&T::Target, &E> {
11551167
self.as_ref().map(|t| t.deref())
11561168
}
11571169
}
11581170

1159-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1171+
#[unstable(feature = "inner_deref", issue = "50264")]
11601172
impl<T, E: Deref> Result<T, E> {
1161-
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &E::Target>`.
1173+
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &<E as Deref>::Target>`.
11621174
///
1163-
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
1164-
/// `Err` type's `Deref::Target` type.
1175+
/// Coerces the [`Err`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
1176+
/// and returns the new [`Result`].
11651177
pub fn as_deref_err(&self) -> Result<&T, &E::Target> {
11661178
self.as_ref().map_err(|e| e.deref())
11671179
}
11681180
}
11691181

1170-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1182+
#[unstable(feature = "inner_deref", issue = "50264")]
11711183
impl<T: DerefMut, E> Result<T, E> {
1172-
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`.
1184+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
11731185
///
1174-
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
1175-
/// the `Ok` type's `Deref::Target` type.
1186+
/// Coerces the [`Ok`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
1187+
/// and returns the new [`Result`].
1188+
///
1189+
/// # Examples
1190+
///
1191+
/// ```
1192+
/// let mut x: Result<String, u32> = Ok("hello".to_string());
1193+
/// let y: Result<&mut str, &mut u32> = Ok("HELLO");
1194+
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
1195+
///
1196+
/// let mut x: Result<String, u32> = Err(42);
1197+
/// let y: Result<&mut str, &mut u32> = Err(&42);
1198+
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
1199+
/// ```
11761200
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
11771201
self.as_mut().map(|t| t.deref_mut())
11781202
}
11791203
}
11801204

1181-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1205+
#[unstable(feature = "inner_deref", issue = "50264")]
11821206
impl<T, E: DerefMut> Result<T, E> {
1183-
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut E::Target>`.
1207+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut <E as DerefMut>::Target>`.
11841208
///
1185-
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
1186-
/// the `Err` type's `Deref::Target` type.
1209+
/// Coerces the [`Err`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
1210+
/// and returns the new [`Result`].
11871211
pub fn as_deref_mut_err(&mut self) -> Result<&mut T, &mut E::Target> {
11881212
self.as_mut().map_err(|e| e.deref_mut())
11891213
}

0 commit comments

Comments
 (0)