Skip to content

Commit 29a1cf2

Browse files
authored
Rollup merge of rust-lang#67930 - lzutao:result-as_deref, r=dtolnay
Rename Result::as_deref_ok to as_deref Addresses rust-lang#50264 (comment) r? @dtolnay
2 parents 7b741fb + c2c2d3b commit 29a1cf2

8 files changed

+31
-121
lines changed

src/libcore/result.rs

+2-25
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ impl<T: Deref, E> Result<T, E> {
11361136
///
11371137
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
11381138
/// `Ok` type's `Deref::Target` type.
1139-
pub fn as_deref_ok(&self) -> Result<&T::Target, &E> {
1139+
pub fn as_deref(&self) -> Result<&T::Target, &E> {
11401140
self.as_ref().map(|t| t.deref())
11411141
}
11421142
}
@@ -1152,24 +1152,13 @@ impl<T, E: Deref> Result<T, E> {
11521152
}
11531153
}
11541154

1155-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1156-
impl<T: Deref, E: Deref> Result<T, E> {
1157-
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E::Target>`.
1158-
///
1159-
/// Leaves the original `Result` in-place, creating a new one containing a reference to both
1160-
/// the `Ok` and `Err` types' `Deref::Target` types.
1161-
pub fn as_deref(&self) -> Result<&T::Target, &E::Target> {
1162-
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
1163-
}
1164-
}
1165-
11661155
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
11671156
impl<T: DerefMut, E> Result<T, E> {
11681157
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`.
11691158
///
11701159
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
11711160
/// the `Ok` type's `Deref::Target` type.
1172-
pub fn as_deref_mut_ok(&mut self) -> Result<&mut T::Target, &mut E> {
1161+
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
11731162
self.as_mut().map(|t| t.deref_mut())
11741163
}
11751164
}
@@ -1185,18 +1174,6 @@ impl<T, E: DerefMut> Result<T, E> {
11851174
}
11861175
}
11871176

1188-
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1189-
impl<T: DerefMut, E: DerefMut> Result<T, E> {
1190-
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to
1191-
/// `Result<&mut T::Target, &mut E::Target>`.
1192-
///
1193-
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
1194-
/// both the `Ok` and `Err` types' `Deref::Target` types.
1195-
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E::Target> {
1196-
self.as_mut().map(|t| t.deref_mut()).map_err(|e| e.deref_mut())
1197-
}
1198-
}
1199-
12001177
impl<T, E> Result<Option<T>, E> {
12011178
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
12021179
///

src/libcore/tests/result.rs

+27-58
Original file line numberDiff line numberDiff line change
@@ -236,31 +236,17 @@ fn test_try() {
236236

237237
#[test]
238238
fn test_result_as_deref() {
239-
// &Result<T: Deref, E>::Ok(T).as_deref_ok() ->
239+
// &Result<T: Deref, E>::Ok(T).as_deref() ->
240240
// Result<&T::Deref::Target, &E>::Ok(&*T)
241241
let ref_ok = &Result::Ok::<&i32, u8>(&42);
242242
let expected_result = Result::Ok::<&i32, &u8>(&42);
243-
assert_eq!(ref_ok.as_deref_ok(), expected_result);
244-
245-
let ref_ok = &Result::Ok::<String, u32>(String::from("a result"));
246-
let expected_result = Result::Ok::<&str, &u32>("a result");
247-
assert_eq!(ref_ok.as_deref_ok(), expected_result);
248-
249-
let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]);
250-
let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice());
251-
assert_eq!(ref_ok.as_deref_ok(), expected_result);
252-
253-
// &Result<T: Deref, E: Deref>::Ok(T).as_deref() ->
254-
// Result<&T::Deref::Target, &E::Deref::Target>::Ok(&*T)
255-
let ref_ok = &Result::Ok::<&i32, &u8>(&42);
256-
let expected_result = Result::Ok::<&i32, &u8>(&42);
257243
assert_eq!(ref_ok.as_deref(), expected_result);
258244

259-
let ref_ok = &Result::Ok::<String, &u32>(String::from("a result"));
245+
let ref_ok = &Result::Ok::<String, u32>(String::from("a result"));
260246
let expected_result = Result::Ok::<&str, &u32>("a result");
261247
assert_eq!(ref_ok.as_deref(), expected_result);
262248

263-
let ref_ok = &Result::Ok::<Vec<i32>, &u32>(vec![1, 2, 3, 4, 5]);
249+
let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]);
264250
let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice());
265251
assert_eq!(ref_ok.as_deref(), expected_result);
266252

@@ -281,19 +267,21 @@ fn test_result_as_deref() {
281267
// &Result<T: Deref, E: Deref>::Err(T).as_deref_err() ->
282268
// Result<&T, &E::Deref::Target>::Err(&*E)
283269
let ref_err = &Result::Err::<&u8, &i32>(&41);
284-
let expected_result = Result::Err::<&u8, &i32>(&41);
270+
let expected_result = Result::Err::<&u8, &&i32>(&&41);
285271
assert_eq!(ref_err.as_deref(), expected_result);
286272

287-
let ref_err = &Result::Err::<&u32, String>(String::from("an error"));
288-
let expected_result = Result::Err::<&u32, &str>("an error");
273+
let s = String::from("an error");
274+
let ref_err = &Result::Err::<&u32, String>(s.clone());
275+
let expected_result = Result::Err::<&u32, &String>(&s);
289276
assert_eq!(ref_err.as_deref(), expected_result);
290277

291-
let ref_err = &Result::Err::<&u32, Vec<i32>>(vec![5, 4, 3, 2, 1]);
292-
let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice());
278+
let v = vec![5, 4, 3, 2, 1];
279+
let ref_err = &Result::Err::<&u32, Vec<i32>>(v.clone());
280+
let expected_result = Result::Err::<&u32, &Vec<i32>>(&v);
293281
assert_eq!(ref_err.as_deref(), expected_result);
294282

295283
// The following cases test calling `as_deref_*` with the wrong variant (i.e.
296-
// `as_deref_ok()` with a `Result::Err()`, or `as_deref_err()` with a `Result::Ok()`.
284+
// `as_deref()` with a `Result::Err()`, or `as_deref_err()` with a `Result::Ok()`.
297285
// While uncommon, these cases are supported to ensure that an `as_deref_*`
298286
// call can still be made even when one of the Result types does not implement
299287
// `Deref` (for example, std::io::Error).
@@ -312,56 +300,38 @@ fn test_result_as_deref() {
312300
let expected_result = Result::Ok::<&[i32; 5], &u32>(&[1, 2, 3, 4, 5]);
313301
assert_eq!(ref_ok.as_deref_err(), expected_result);
314302

315-
// &Result<T: Deref, E>::Err(E).as_deref_ok() ->
303+
// &Result<T: Deref, E>::Err(E).as_deref() ->
316304
// Result<&T::Deref::Target, &E>::Err(&E)
317305
let ref_err = &Result::Err::<&u8, i32>(41);
318306
let expected_result = Result::Err::<&u8, &i32>(&41);
319-
assert_eq!(ref_err.as_deref_ok(), expected_result);
307+
assert_eq!(ref_err.as_deref(), expected_result);
320308

321309
let ref_err = &Result::Err::<&u32, &str>("an error");
322310
let expected_result = Result::Err::<&u32, &&str>(&"an error");
323-
assert_eq!(ref_err.as_deref_ok(), expected_result);
311+
assert_eq!(ref_err.as_deref(), expected_result);
324312

325313
let ref_err = &Result::Err::<&u32, [i32; 5]>([5, 4, 3, 2, 1]);
326314
let expected_result = Result::Err::<&u32, &[i32; 5]>(&[5, 4, 3, 2, 1]);
327-
assert_eq!(ref_err.as_deref_ok(), expected_result);
315+
assert_eq!(ref_err.as_deref(), expected_result);
328316
}
329317

330318
#[test]
331319
fn test_result_as_deref_mut() {
332-
// &mut Result<T: Deref, E>::Ok(T).as_deref_mut_ok() ->
320+
// &mut Result<T: Deref, E>::Ok(T).as_deref_mut() ->
333321
// Result<&mut T::Deref::Target, &mut E>::Ok(&mut *T)
334322
let mut val = 42;
335323
let mut expected_val = 42;
336324
let mut_ok = &mut Result::Ok::<&mut i32, u8>(&mut val);
337325
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
338-
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
339-
340-
let mut expected_string = String::from("a result");
341-
let mut_ok = &mut Result::Ok::<String, u32>(expected_string.clone());
342-
let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut());
343-
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
344-
345-
let mut expected_vec = vec![1, 2, 3, 4, 5];
346-
let mut_ok = &mut Result::Ok::<Vec<i32>, u32>(expected_vec.clone());
347-
let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice());
348-
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
349-
350-
// &mut Result<T: Deref, E: Deref>::Ok(T).as_deref_mut() ->
351-
// Result<&mut T::Deref::Target, &mut E::Deref::Target>::Ok(&mut *T)
352-
let mut val = 42;
353-
let mut expected_val = 42;
354-
let mut_ok = &mut Result::Ok::<&mut i32, &mut u8>(&mut val);
355-
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
356326
assert_eq!(mut_ok.as_deref_mut(), expected_result);
357327

358328
let mut expected_string = String::from("a result");
359-
let mut_ok = &mut Result::Ok::<String, &mut u32>(expected_string.clone());
329+
let mut_ok = &mut Result::Ok::<String, u32>(expected_string.clone());
360330
let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut());
361331
assert_eq!(mut_ok.as_deref_mut(), expected_result);
362332

363333
let mut expected_vec = vec![1, 2, 3, 4, 5];
364-
let mut_ok = &mut Result::Ok::<Vec<i32>, &mut u32>(expected_vec.clone());
334+
let mut_ok = &mut Result::Ok::<Vec<i32>, u32>(expected_vec.clone());
365335
let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice());
366336
assert_eq!(mut_ok.as_deref_mut(), expected_result);
367337

@@ -386,23 +356,22 @@ fn test_result_as_deref_mut() {
386356
// &mut Result<T: Deref, E: Deref>::Err(T).as_deref_mut_err() ->
387357
// Result<&mut T, &mut E::Deref::Target>::Err(&mut *E)
388358
let mut val = 41;
389-
let mut expected_val = 41;
390-
let mut_err = &mut Result::Err::<&mut u8, &mut i32>(&mut val);
391-
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val);
359+
let mut_err = &mut Result::Err::<&mut u8, i32>(val);
360+
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut val);
392361
assert_eq!(mut_err.as_deref_mut(), expected_result);
393362

394363
let mut expected_string = String::from("an error");
395364
let mut_err = &mut Result::Err::<&mut u32, String>(expected_string.clone());
396-
let expected_result = Result::Err::<&mut u32, &mut str>(expected_string.as_mut_str());
365+
let expected_result = Result::Err::<&mut u32, &mut String>(&mut expected_string);
397366
assert_eq!(mut_err.as_deref_mut(), expected_result);
398367

399368
let mut expected_vec = vec![5, 4, 3, 2, 1];
400369
let mut_err = &mut Result::Err::<&mut u32, Vec<i32>>(expected_vec.clone());
401-
let expected_result = Result::Err::<&mut u32, &mut [i32]>(expected_vec.as_mut_slice());
370+
let expected_result = Result::Err::<&mut u32, &mut Vec<i32>>(&mut expected_vec);
402371
assert_eq!(mut_err.as_deref_mut(), expected_result);
403372

404373
// The following cases test calling `as_deref_mut_*` with the wrong variant (i.e.
405-
// `as_deref_mut_ok()` with a `Result::Err()`, or `as_deref_mut_err()` with a `Result::Ok()`.
374+
// `as_deref_mut()` with a `Result::Err()`, or `as_deref_mut_err()` with a `Result::Ok()`.
406375
// While uncommon, these cases are supported to ensure that an `as_deref_mut_*`
407376
// call can still be made even when one of the Result types does not implement
408377
// `Deref` (for example, std::io::Error).
@@ -426,22 +395,22 @@ fn test_result_as_deref_mut() {
426395
let expected_result = Result::Ok::<&mut [i32; 5], &mut u32>(&mut expected_arr);
427396
assert_eq!(mut_ok.as_deref_mut_err(), expected_result);
428397

429-
// &mut Result<T: Deref, E>::Err(E).as_deref_mut_ok() ->
398+
// &mut Result<T: Deref, E>::Err(E).as_deref_mut() ->
430399
// Result<&mut T::Deref::Target, &mut E>::Err(&mut E)
431400
let mut expected_val = 41;
432401
let mut_err = &mut Result::Err::<&mut u8, i32>(expected_val.clone());
433402
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val);
434-
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
403+
assert_eq!(mut_err.as_deref_mut(), expected_result);
435404

436405
let string = String::from("an error");
437406
let expected_string = string.clone();
438407
let mut ref_str = expected_string.as_ref();
439408
let mut_err = &mut Result::Err::<&mut u32, &str>(string.as_str());
440409
let expected_result = Result::Err::<&mut u32, &mut &str>(&mut ref_str);
441-
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
410+
assert_eq!(mut_err.as_deref_mut(), expected_result);
442411

443412
let mut expected_arr = [5, 4, 3, 2, 1];
444413
let mut_err = &mut Result::Err::<&mut u32, [i32; 5]>(expected_arr.clone());
445414
let expected_result = Result::Err::<&mut u32, &mut [i32; 5]>(&mut expected_arr);
446-
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
415+
assert_eq!(mut_err.as_deref_mut(), expected_result);
447416
}

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_err.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `as_deref_err` found for enum `std::result::Result
22
--> $DIR/result-as_deref_err.rs:4:28
33
|
44
LL | let _result = &Err(41).as_deref_err();
5-
| ^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_ok`
5+
| ^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut`
66
|
77
= note: the method `as_deref_err` exists but the following trait bounds were not satisfied:
88
`{integer} : std::ops::Deref`

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_mut_err.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `as_deref_mut_err` found for enum `std::result::Re
22
--> $DIR/result-as_deref_mut_err.rs:4:32
33
|
44
LL | let _result = &mut Err(41).as_deref_mut_err();
5-
| ^^^^^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut_ok`
5+
| ^^^^^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut`
66
|
77
= note: the method `as_deref_mut_err` exists but the following trait bounds were not satisfied:
88
`{integer} : std::ops::DerefMut`

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_mut_ok.rs

-6
This file was deleted.

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_mut_ok.stderr

-12
This file was deleted.

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_ok.rs

-6
This file was deleted.

src/test/ui/issues/issue-50264-inner-deref-trait/result-as_deref_ok.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)