mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 15:01:51 +00:00
Rollup merge of #67930 - lzutao:result-as_deref, r=dtolnay
Rename Result::as_deref_ok to as_deref Addresses https://github.com/rust-lang/rust/issues/50264#issuecomment-533952247 r? @dtolnay
This commit is contained in:
commit
29a1cf2876
@ -1136,7 +1136,7 @@ impl<T: Deref, E> Result<T, E> {
|
||||
///
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
|
||||
/// `Ok` type's `Deref::Target` type.
|
||||
pub fn as_deref_ok(&self) -> Result<&T::Target, &E> {
|
||||
pub fn as_deref(&self) -> Result<&T::Target, &E> {
|
||||
self.as_ref().map(|t| t.deref())
|
||||
}
|
||||
}
|
||||
@ -1152,24 +1152,13 @@ impl<T, E: Deref> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: Deref, E: Deref> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E::Target>`.
|
||||
///
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a reference to both
|
||||
/// the `Ok` and `Err` types' `Deref::Target` types.
|
||||
pub fn as_deref(&self) -> Result<&T::Target, &E::Target> {
|
||||
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: DerefMut, E> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`.
|
||||
///
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
|
||||
/// the `Ok` type's `Deref::Target` type.
|
||||
pub fn as_deref_mut_ok(&mut self) -> Result<&mut T::Target, &mut E> {
|
||||
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
|
||||
self.as_mut().map(|t| t.deref_mut())
|
||||
}
|
||||
}
|
||||
@ -1185,18 +1174,6 @@ impl<T, E: DerefMut> Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: DerefMut, E: DerefMut> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to
|
||||
/// `Result<&mut T::Target, &mut E::Target>`.
|
||||
///
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
|
||||
/// both the `Ok` and `Err` types' `Deref::Target` types.
|
||||
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E::Target> {
|
||||
self.as_mut().map(|t| t.deref_mut()).map_err(|e| e.deref_mut())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> Result<Option<T>, E> {
|
||||
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
|
||||
///
|
||||
|
@ -236,32 +236,18 @@ fn test_try() {
|
||||
|
||||
#[test]
|
||||
fn test_result_as_deref() {
|
||||
// &Result<T: Deref, E>::Ok(T).as_deref_ok() ->
|
||||
// &Result<T: Deref, E>::Ok(T).as_deref() ->
|
||||
// Result<&T::Deref::Target, &E>::Ok(&*T)
|
||||
let ref_ok = &Result::Ok::<&i32, u8>(&42);
|
||||
let expected_result = Result::Ok::<&i32, &u8>(&42);
|
||||
assert_eq!(ref_ok.as_deref_ok(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<String, u32>(String::from("a result"));
|
||||
let expected_result = Result::Ok::<&str, &u32>("a result");
|
||||
assert_eq!(ref_ok.as_deref_ok(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]);
|
||||
let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice());
|
||||
assert_eq!(ref_ok.as_deref_ok(), expected_result);
|
||||
|
||||
// &Result<T: Deref, E: Deref>::Ok(T).as_deref() ->
|
||||
// Result<&T::Deref::Target, &E::Deref::Target>::Ok(&*T)
|
||||
let ref_ok = &Result::Ok::<&i32, &u8>(&42);
|
||||
let expected_result = Result::Ok::<&i32, &u8>(&42);
|
||||
assert_eq!(ref_ok.as_deref(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<String, &u32>(String::from("a result"));
|
||||
let expected_result = Result::Ok::<&str, &u32>("a result");
|
||||
assert_eq!(ref_ok.as_deref(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<Vec<i32>, &u32>(vec![1, 2, 3, 4, 5]);
|
||||
let expected_result = Result::Ok::<&[i32], &u32>([1, 2, 3, 4, 5].as_slice());
|
||||
assert_eq!(ref_ok.as_deref(), expected_result);
|
||||
|
||||
// &Result<T, E: Deref>::Err(T).as_deref_err() ->
|
||||
@ -281,19 +267,21 @@ fn test_result_as_deref() {
|
||||
// &Result<T: Deref, E: Deref>::Err(T).as_deref_err() ->
|
||||
// Result<&T, &E::Deref::Target>::Err(&*E)
|
||||
let ref_err = &Result::Err::<&u8, &i32>(&41);
|
||||
let expected_result = Result::Err::<&u8, &i32>(&41);
|
||||
let expected_result = Result::Err::<&u8, &&i32>(&&41);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<&u32, String>(String::from("an error"));
|
||||
let expected_result = Result::Err::<&u32, &str>("an error");
|
||||
let s = String::from("an error");
|
||||
let ref_err = &Result::Err::<&u32, String>(s.clone());
|
||||
let expected_result = Result::Err::<&u32, &String>(&s);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<&u32, Vec<i32>>(vec![5, 4, 3, 2, 1]);
|
||||
let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice());
|
||||
let v = vec![5, 4, 3, 2, 1];
|
||||
let ref_err = &Result::Err::<&u32, Vec<i32>>(v.clone());
|
||||
let expected_result = Result::Err::<&u32, &Vec<i32>>(&v);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
// The following cases test calling `as_deref_*` with the wrong variant (i.e.
|
||||
// `as_deref_ok()` with a `Result::Err()`, or `as_deref_err()` with a `Result::Ok()`.
|
||||
// `as_deref()` with a `Result::Err()`, or `as_deref_err()` with a `Result::Ok()`.
|
||||
// While uncommon, these cases are supported to ensure that an `as_deref_*`
|
||||
// call can still be made even when one of the Result types does not implement
|
||||
// `Deref` (for example, std::io::Error).
|
||||
@ -312,57 +300,39 @@ fn test_result_as_deref() {
|
||||
let expected_result = Result::Ok::<&[i32; 5], &u32>(&[1, 2, 3, 4, 5]);
|
||||
assert_eq!(ref_ok.as_deref_err(), expected_result);
|
||||
|
||||
// &Result<T: Deref, E>::Err(E).as_deref_ok() ->
|
||||
// &Result<T: Deref, E>::Err(E).as_deref() ->
|
||||
// Result<&T::Deref::Target, &E>::Err(&E)
|
||||
let ref_err = &Result::Err::<&u8, i32>(41);
|
||||
let expected_result = Result::Err::<&u8, &i32>(&41);
|
||||
assert_eq!(ref_err.as_deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<&u32, &str>("an error");
|
||||
let expected_result = Result::Err::<&u32, &&str>(&"an error");
|
||||
assert_eq!(ref_err.as_deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<&u32, [i32; 5]>([5, 4, 3, 2, 1]);
|
||||
let expected_result = Result::Err::<&u32, &[i32; 5]>(&[5, 4, 3, 2, 1]);
|
||||
assert_eq!(ref_err.as_deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_result_as_deref_mut() {
|
||||
// &mut Result<T: Deref, E>::Ok(T).as_deref_mut_ok() ->
|
||||
// &mut Result<T: Deref, E>::Ok(T).as_deref_mut() ->
|
||||
// Result<&mut T::Deref::Target, &mut E>::Ok(&mut *T)
|
||||
let mut val = 42;
|
||||
let mut expected_val = 42;
|
||||
let mut_ok = &mut Result::Ok::<&mut i32, u8>(&mut val);
|
||||
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
|
||||
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
|
||||
assert_eq!(mut_ok.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_string = String::from("a result");
|
||||
let mut_ok = &mut Result::Ok::<String, u32>(expected_string.clone());
|
||||
let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut());
|
||||
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
|
||||
assert_eq!(mut_ok.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_vec = vec![1, 2, 3, 4, 5];
|
||||
let mut_ok = &mut Result::Ok::<Vec<i32>, u32>(expected_vec.clone());
|
||||
let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice());
|
||||
assert_eq!(mut_ok.as_deref_mut_ok(), expected_result);
|
||||
|
||||
// &mut Result<T: Deref, E: Deref>::Ok(T).as_deref_mut() ->
|
||||
// Result<&mut T::Deref::Target, &mut E::Deref::Target>::Ok(&mut *T)
|
||||
let mut val = 42;
|
||||
let mut expected_val = 42;
|
||||
let mut_ok = &mut Result::Ok::<&mut i32, &mut u8>(&mut val);
|
||||
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
|
||||
assert_eq!(mut_ok.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_string = String::from("a result");
|
||||
let mut_ok = &mut Result::Ok::<String, &mut u32>(expected_string.clone());
|
||||
let expected_result = Result::Ok::<&mut str, &mut u32>(expected_string.deref_mut());
|
||||
assert_eq!(mut_ok.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_vec = vec![1, 2, 3, 4, 5];
|
||||
let mut_ok = &mut Result::Ok::<Vec<i32>, &mut u32>(expected_vec.clone());
|
||||
let expected_result = Result::Ok::<&mut [i32], &mut u32>(expected_vec.as_mut_slice());
|
||||
assert_eq!(mut_ok.as_deref_mut(), expected_result);
|
||||
|
||||
// &mut Result<T, E: Deref>::Err(T).as_deref_mut_err() ->
|
||||
@ -386,23 +356,22 @@ fn test_result_as_deref_mut() {
|
||||
// &mut Result<T: Deref, E: Deref>::Err(T).as_deref_mut_err() ->
|
||||
// Result<&mut T, &mut E::Deref::Target>::Err(&mut *E)
|
||||
let mut val = 41;
|
||||
let mut expected_val = 41;
|
||||
let mut_err = &mut Result::Err::<&mut u8, &mut i32>(&mut val);
|
||||
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val);
|
||||
let mut_err = &mut Result::Err::<&mut u8, i32>(val);
|
||||
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut val);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_string = String::from("an error");
|
||||
let mut_err = &mut Result::Err::<&mut u32, String>(expected_string.clone());
|
||||
let expected_result = Result::Err::<&mut u32, &mut str>(expected_string.as_mut_str());
|
||||
let expected_result = Result::Err::<&mut u32, &mut String>(&mut expected_string);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_vec = vec![5, 4, 3, 2, 1];
|
||||
let mut_err = &mut Result::Err::<&mut u32, Vec<i32>>(expected_vec.clone());
|
||||
let expected_result = Result::Err::<&mut u32, &mut [i32]>(expected_vec.as_mut_slice());
|
||||
let expected_result = Result::Err::<&mut u32, &mut Vec<i32>>(&mut expected_vec);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
|
||||
// The following cases test calling `as_deref_mut_*` with the wrong variant (i.e.
|
||||
// `as_deref_mut_ok()` with a `Result::Err()`, or `as_deref_mut_err()` with a `Result::Ok()`.
|
||||
// `as_deref_mut()` with a `Result::Err()`, or `as_deref_mut_err()` with a `Result::Ok()`.
|
||||
// While uncommon, these cases are supported to ensure that an `as_deref_mut_*`
|
||||
// call can still be made even when one of the Result types does not implement
|
||||
// `Deref` (for example, std::io::Error).
|
||||
@ -426,22 +395,22 @@ fn test_result_as_deref_mut() {
|
||||
let expected_result = Result::Ok::<&mut [i32; 5], &mut u32>(&mut expected_arr);
|
||||
assert_eq!(mut_ok.as_deref_mut_err(), expected_result);
|
||||
|
||||
// &mut Result<T: Deref, E>::Err(E).as_deref_mut_ok() ->
|
||||
// &mut Result<T: Deref, E>::Err(E).as_deref_mut() ->
|
||||
// Result<&mut T::Deref::Target, &mut E>::Err(&mut E)
|
||||
let mut expected_val = 41;
|
||||
let mut_err = &mut Result::Err::<&mut u8, i32>(expected_val.clone());
|
||||
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val);
|
||||
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
|
||||
let string = String::from("an error");
|
||||
let expected_string = string.clone();
|
||||
let mut ref_str = expected_string.as_ref();
|
||||
let mut_err = &mut Result::Err::<&mut u32, &str>(string.as_str());
|
||||
let expected_result = Result::Err::<&mut u32, &mut &str>(&mut ref_str);
|
||||
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
|
||||
let mut expected_arr = [5, 4, 3, 2, 1];
|
||||
let mut_err = &mut Result::Err::<&mut u32, [i32; 5]>(expected_arr.clone());
|
||||
let expected_result = Result::Err::<&mut u32, &mut [i32; 5]>(&mut expected_arr);
|
||||
assert_eq!(mut_err.as_deref_mut_ok(), expected_result);
|
||||
assert_eq!(mut_err.as_deref_mut(), expected_result);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ error[E0599]: no method named `as_deref_err` found for enum `std::result::Result
|
||||
--> $DIR/result-as_deref_err.rs:4:28
|
||||
|
|
||||
LL | let _result = &Err(41).as_deref_err();
|
||||
| ^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_ok`
|
||||
| ^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut`
|
||||
|
|
||||
= note: the method `as_deref_err` exists but the following trait bounds were not satisfied:
|
||||
`{integer} : std::ops::Deref`
|
||||
|
@ -2,7 +2,7 @@ error[E0599]: no method named `as_deref_mut_err` found for enum `std::result::Re
|
||||
--> $DIR/result-as_deref_mut_err.rs:4:32
|
||||
|
|
||||
LL | let _result = &mut Err(41).as_deref_mut_err();
|
||||
| ^^^^^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut_ok`
|
||||
| ^^^^^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut`
|
||||
|
|
||||
= note: the method `as_deref_mut_err` exists but the following trait bounds were not satisfied:
|
||||
`{integer} : std::ops::DerefMut`
|
||||
|
@ -1,6 +0,0 @@
|
||||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &mut Ok(42).as_deref_mut_ok();
|
||||
//~^ ERROR no method named `as_deref_mut_ok` found
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
error[E0599]: no method named `as_deref_mut_ok` found for enum `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-as_deref_mut_ok.rs:4:31
|
||||
|
|
||||
LL | let _result = &mut Ok(42).as_deref_mut_ok();
|
||||
| ^^^^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_mut_err`
|
||||
|
|
||||
= note: the method `as_deref_mut_ok` exists but the following trait bounds were not satisfied:
|
||||
`{integer} : std::ops::DerefMut`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0599`.
|
@ -1,6 +0,0 @@
|
||||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Ok(42).as_deref_ok();
|
||||
//~^ ERROR no method named `as_deref_ok` found
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
error[E0599]: no method named `as_deref_ok` found for enum `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-as_deref_ok.rs:4:27
|
||||
|
|
||||
LL | let _result = &Ok(42).as_deref_ok();
|
||||
| ^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_err`
|
||||
|
|
||||
= note: the method `as_deref_ok` exists but the following trait bounds were not satisfied:
|
||||
`{integer} : std::ops::Deref`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0599`.
|
Loading…
Reference in New Issue
Block a user