@@ -1145,45 +1145,69 @@ impl<T, E: Into<!>> Result<T, E> {
1145
1145
}
1146
1146
}
1147
1147
1148
- #[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
1148
+ #[ unstable( feature = "inner_deref" , issue = "50264" ) ]
1149
1149
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>`.
1151
1151
///
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
+ /// ```
1154
1166
pub fn as_deref ( & self ) -> Result < & T :: Target , & E > {
1155
1167
self . as_ref ( ) . map ( |t| t. deref ( ) )
1156
1168
}
1157
1169
}
1158
1170
1159
- #[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
1171
+ #[ unstable( feature = "inner_deref" , issue = "50264" ) ]
1160
1172
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>`.
1162
1174
///
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`] .
1165
1177
pub fn as_deref_err ( & self ) -> Result < & T , & E :: Target > {
1166
1178
self . as_ref ( ) . map_err ( |e| e. deref ( ) )
1167
1179
}
1168
1180
}
1169
1181
1170
- #[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
1182
+ #[ unstable( feature = "inner_deref" , issue = "50264" ) ]
1171
1183
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>`.
1173
1185
///
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
+ /// ```
1176
1200
pub fn as_deref_mut ( & mut self ) -> Result < & mut T :: Target , & mut E > {
1177
1201
self . as_mut ( ) . map ( |t| t. deref_mut ( ) )
1178
1202
}
1179
1203
}
1180
1204
1181
- #[ unstable( feature = "inner_deref" , reason = "newly added" , issue = "50264" ) ]
1205
+ #[ unstable( feature = "inner_deref" , issue = "50264" ) ]
1182
1206
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>`.
1184
1208
///
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`] .
1187
1211
pub fn as_deref_mut_err ( & mut self ) -> Result < & mut T , & mut E :: Target > {
1188
1212
self . as_mut ( ) . map_err ( |e| e. deref_mut ( ) )
1189
1213
}
0 commit comments