Rollup merge of #62421 - JohnTitor:U007D-master, r=alexcrichton
Introduce `as_deref` to Option This is re-submission for #59628. Renames `deref()` to `as_deref()` and adds `deref_mut()` impls and tests. CC #50264 r? @Kimundi (I picked you as you're the previous reviewer.)
This commit is contained in:
commit
625aa60b3a
31 changed files with 393 additions and 136 deletions
|
@ -136,7 +136,7 @@
|
|||
#![stable(feature = "rust1", since = "1.0.0")]
|
||||
|
||||
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
|
||||
use crate::{convert, fmt, hint, mem, ops::{self, Deref}};
|
||||
use crate::{convert, fmt, hint, mem, ops::{self, Deref, DerefMut}};
|
||||
use crate::pin::Pin;
|
||||
|
||||
// Note that this is not a lang item per se, but it has a hidden dependency on
|
||||
|
@ -1104,17 +1104,28 @@ impl<T: Default> Option<T> {
|
|||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: Deref> Option<T> {
|
||||
/// Converts from `&Option<T>` to `Option<&T::Target>`.
|
||||
/// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
|
||||
///
|
||||
/// Leaves the original Option in-place, creating a new one with a reference
|
||||
/// to the original one, additionally coercing the contents via [`Deref`].
|
||||
///
|
||||
/// [`Deref`]: ../../std/ops/trait.Deref.html
|
||||
pub fn deref(&self) -> Option<&T::Target> {
|
||||
pub fn as_deref(&self) -> Option<&T::Target> {
|
||||
self.as_ref().map(|t| t.deref())
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: DerefMut> Option<T> {
|
||||
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
|
||||
///
|
||||
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
|
||||
/// the inner type's `Deref::Target` type.
|
||||
pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> {
|
||||
self.as_mut().map(|t| t.deref_mut())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E> Option<Result<T, E>> {
|
||||
/// Transposes an `Option` of a [`Result`] into a [`Result`] of an `Option`.
|
||||
///
|
||||
|
|
|
@ -232,7 +232,7 @@
|
|||
|
||||
use crate::fmt;
|
||||
use crate::iter::{FromIterator, FusedIterator, TrustedLen};
|
||||
use crate::ops::{self, Deref};
|
||||
use crate::ops::{self, Deref, DerefMut};
|
||||
|
||||
/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
|
||||
///
|
||||
|
@ -981,24 +981,22 @@ impl<T: Default, E> Result<T, E> {
|
|||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T: Deref, E> Result<T, E> {
|
||||
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`.
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
|
||||
///
|
||||
/// Leaves the original Result in-place, creating a new one with a reference
|
||||
/// to the original one, additionally coercing the `Ok` arm of the Result via
|
||||
/// `Deref`.
|
||||
pub fn deref_ok(&self) -> Result<&T::Target, &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> {
|
||||
self.as_ref().map(|t| t.deref())
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T, E: Deref> Result<T, E> {
|
||||
/// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`.
|
||||
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &E::Target>`.
|
||||
///
|
||||
/// Leaves the original Result in-place, creating a new one with a reference
|
||||
/// to the original one, additionally coercing the `Err` arm of the Result via
|
||||
/// `Deref`.
|
||||
pub fn deref_err(&self) -> Result<&T, &E::Target>
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
|
||||
/// `Err` type's `Deref::Target` type.
|
||||
pub fn as_deref_err(&self) -> Result<&T, &E::Target>
|
||||
{
|
||||
self.as_ref().map_err(|e| e.deref())
|
||||
}
|
||||
|
@ -1006,17 +1004,52 @@ 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>` to `Result<&T::Target, &E::Target>`.
|
||||
/// 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 with a reference
|
||||
/// to the original one, additionally coercing both the `Ok` and `Err` arms
|
||||
/// of the Result via `Deref`.
|
||||
pub fn deref(&self) -> 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> {
|
||||
self.as_mut().map(|t| t.deref_mut())
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
|
||||
impl<T, E: DerefMut> Result<T, E> {
|
||||
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut E::Target>`.
|
||||
///
|
||||
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
|
||||
/// the `Err` type's `Deref::Target` type.
|
||||
pub fn as_deref_mut_err(&mut self) -> Result<&mut T, &mut E::Target>
|
||||
{
|
||||
self.as_mut().map_err(|e| e.deref_mut())
|
||||
}
|
||||
}
|
||||
|
||||
#[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`.
|
||||
///
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
use core::option::*;
|
||||
use core::mem;
|
||||
use core::clone::Clone;
|
||||
use core::array::FixedSizeArray;
|
||||
use core::ops::DerefMut;
|
||||
|
||||
#[test]
|
||||
fn test_get_ptr() {
|
||||
|
@ -310,20 +312,38 @@ fn test_try() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_option_deref() {
|
||||
fn test_option_as_deref() {
|
||||
// Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
|
||||
let ref_option = &Some(&42);
|
||||
assert_eq!(ref_option.deref(), Some(&42));
|
||||
assert_eq!(ref_option.as_deref(), Some(&42));
|
||||
|
||||
let ref_option = &Some(String::from("a result"));
|
||||
assert_eq!(ref_option.deref(), Some("a result"));
|
||||
assert_eq!(ref_option.as_deref(), Some("a result"));
|
||||
|
||||
let ref_option = &Some(vec![1, 2, 3, 4, 5]);
|
||||
assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..]));
|
||||
assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
|
||||
|
||||
// None: &Option<T: Deref>>::None -> None
|
||||
let ref_option: &Option<&i32> = &None;
|
||||
assert_eq!(ref_option.deref(), None);
|
||||
assert_eq!(ref_option.as_deref(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_option_as_deref_mut() {
|
||||
// Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
|
||||
let mut val = 42;
|
||||
let ref_option = &mut Some(&mut val);
|
||||
assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
|
||||
|
||||
let ref_option = &mut Some(String::from("a result"));
|
||||
assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
|
||||
|
||||
let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
|
||||
assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
|
||||
|
||||
// None: &mut Option<T: Deref>>::None -> None
|
||||
let ref_option: &mut Option<&mut i32> = &mut None;
|
||||
assert_eq!(ref_option.as_deref_mut(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use core::option::*;
|
||||
use core::array::FixedSizeArray;
|
||||
use core::ops::DerefMut;
|
||||
|
||||
fn op1() -> Result<isize, &'static str> { Ok(666) }
|
||||
fn op2() -> Result<isize, &'static str> { Err("sadface") }
|
||||
|
@ -225,94 +227,213 @@ fn test_try() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_result_deref() {
|
||||
// &Result<T: Deref, E>::Ok(T).deref_ok() ->
|
||||
fn test_result_as_deref() {
|
||||
// &Result<T: Deref, E>::Ok(T).as_deref_ok() ->
|
||||
// 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.deref_ok(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref_ok(), 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.deref_ok(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref_ok(), 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][..]);
|
||||
assert_eq!(ref_ok.deref_ok(), expected_result);
|
||||
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).deref() ->
|
||||
// &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.deref(), 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.deref(), 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][..]);
|
||||
assert_eq!(ref_ok.deref(), expected_result);
|
||||
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).deref_err() ->
|
||||
// &Result<T, 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);
|
||||
assert_eq!(ref_err.deref_err(), expected_result);
|
||||
assert_eq!(ref_err.as_deref_err(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<u32, String>(String::from("an error"));
|
||||
let expected_result = Result::Err::<&u32, &str>("an error");
|
||||
assert_eq!(ref_err.deref_err(), expected_result);
|
||||
assert_eq!(ref_err.as_deref_err(), 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][..]);
|
||||
assert_eq!(ref_err.deref_err(), expected_result);
|
||||
let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice());
|
||||
assert_eq!(ref_err.as_deref_err(), expected_result);
|
||||
|
||||
// &Result<T: Deref, E: Deref>::Err(T).deref_err() ->
|
||||
// &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);
|
||||
assert_eq!(ref_err.deref(), expected_result);
|
||||
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");
|
||||
assert_eq!(ref_err.deref(), expected_result);
|
||||
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][..]);
|
||||
assert_eq!(ref_err.deref(), expected_result);
|
||||
let expected_result = Result::Err::<&u32, &[i32]>([5, 4, 3, 2, 1].as_slice());
|
||||
assert_eq!(ref_err.as_deref(), expected_result);
|
||||
|
||||
// The following cases test calling deref_* with the wrong variant (i.e.
|
||||
// `deref_ok()` with a `Result::Err()`, or `deref_err()` with a `Result::Ok()`.
|
||||
// While unusual, these cases are supported to ensure that an `inner_deref`
|
||||
// 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()`.
|
||||
// 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).
|
||||
|
||||
// &Result<T, E: Deref>::Ok(T).deref_err() ->
|
||||
// &Result<T, E: Deref>::Ok(T).as_deref_err() ->
|
||||
// Result<&T, &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.deref_err(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref_err(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<&str, &u32>("a result");
|
||||
let expected_result = Result::Ok::<&&str, &u32>(&"a result");
|
||||
assert_eq!(ref_ok.deref_err(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref_err(), expected_result);
|
||||
|
||||
let ref_ok = &Result::Ok::<[i32; 5], &u32>([1, 2, 3, 4, 5]);
|
||||
let expected_result = Result::Ok::<&[i32; 5], &u32>(&[1, 2, 3, 4, 5]);
|
||||
assert_eq!(ref_ok.deref_err(), expected_result);
|
||||
assert_eq!(ref_ok.as_deref_err(), expected_result);
|
||||
|
||||
// &Result<T: Deref, E>::Err(E).deref_ok() ->
|
||||
// &Result<T: Deref, E>::Err(E).as_deref_ok() ->
|
||||
// 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.deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref_ok(), expected_result);
|
||||
|
||||
let ref_err = &Result::Err::<&u32, &str>("an error");
|
||||
let expected_result = Result::Err::<&u32, &&str>(&"an error");
|
||||
assert_eq!(ref_err.deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref_ok(), 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.deref_ok(), expected_result);
|
||||
assert_eq!(ref_err.as_deref_ok(), expected_result);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_result_as_deref_mut() {
|
||||
// &mut Result<T: Deref, E>::Ok(T).as_deref_mut_ok() ->
|
||||
// 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);
|
||||
|
||||
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);
|
||||
|
||||
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() ->
|
||||
// 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::<u8, &mut i32>(&mut val);
|
||||
let expected_result = Result::Err::<&mut u8, &mut i32>(&mut expected_val);
|
||||
assert_eq!(mut_err.as_deref_mut_err(), expected_result);
|
||||
|
||||
let mut expected_string = String::from("an error");
|
||||
let mut_err = &mut Result::Err::<u32, String>(expected_string.clone());
|
||||
let expected_result = Result::Err::<&mut u32, &mut str>(expected_string.deref_mut());
|
||||
assert_eq!(mut_err.as_deref_mut_err(), expected_result);
|
||||
|
||||
let mut expected_vec = vec![5, 4, 3, 2, 1];
|
||||
let mut_err = &mut Result::Err::<u32, Vec<i32>>(expected_vec.clone());
|
||||
let expected_result = Result::Err::<&mut u32, &mut [i32]>(expected_vec.as_mut_slice());
|
||||
assert_eq!(mut_err.as_deref_mut_err(), expected_result);
|
||||
|
||||
// &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);
|
||||
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());
|
||||
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());
|
||||
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()`.
|
||||
// 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).
|
||||
|
||||
// &mut Result<T, E: Deref>::Ok(T).as_deref_mut_err() ->
|
||||
// Result<&mut T, &mut E::Deref::Target>::Ok(&mut T)
|
||||
let mut expected_val = 42;
|
||||
let mut_ok = &mut Result::Ok::<i32, &mut u8>(expected_val.clone());
|
||||
let expected_result = Result::Ok::<&mut i32, &mut u8>(&mut expected_val);
|
||||
assert_eq!(mut_ok.as_deref_mut_err(), expected_result);
|
||||
|
||||
let string = String::from("a result");
|
||||
let expected_string = string.clone();
|
||||
let mut ref_str = expected_string.as_ref();
|
||||
let mut_ok = &mut Result::Ok::<&str, &mut u32>(string.as_str());
|
||||
let expected_result = Result::Ok::<&mut &str, &mut u32>(&mut ref_str);
|
||||
assert_eq!(mut_ok.as_deref_mut_err(), expected_result);
|
||||
|
||||
let mut expected_arr = [1, 2, 3, 4, 5];
|
||||
let mut_ok = &mut Result::Ok::<[i32; 5], &mut u32>(expected_arr.clone());
|
||||
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() ->
|
||||
// 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);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -941,7 +941,7 @@ impl<'a> State<'a> {
|
|||
self.maybe_print_comment(st.span.lo());
|
||||
match st.node {
|
||||
hir::StmtKind::Local(ref loc) => {
|
||||
self.print_local(loc.init.deref(), |this| this.print_local_decl(&loc));
|
||||
self.print_local(loc.init.as_deref(), |this| this.print_local_decl(&loc));
|
||||
}
|
||||
hir::StmtKind::Item(item) => {
|
||||
self.ann.nested(self, Nested::Item(item))
|
||||
|
|
|
@ -225,7 +225,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
tcx.mk_unit()
|
||||
}
|
||||
ExprKind::Break(destination, ref expr_opt) => {
|
||||
self.check_expr_break(destination, expr_opt.deref(), expr)
|
||||
self.check_expr_break(destination, expr_opt.as_deref(), expr)
|
||||
}
|
||||
ExprKind::Continue(destination) => {
|
||||
if destination.target_id.is_ok() {
|
||||
|
@ -236,7 +236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
ExprKind::Ret(ref expr_opt) => {
|
||||
self.check_expr_return(expr_opt.deref(), expr)
|
||||
self.check_expr_return(expr_opt.as_deref(), expr)
|
||||
}
|
||||
ExprKind::Assign(ref lhs, ref rhs) => {
|
||||
self.check_expr_assign(expr, expected, lhs, rhs)
|
||||
|
|
|
@ -1173,7 +1173,7 @@ themePicker.onblur = handleThemeButtonsBlur;
|
|||
title: "Index of crates",
|
||||
css_class: "mod",
|
||||
root_path: "./",
|
||||
static_root_path: cx.shared.static_root_path.deref(),
|
||||
static_root_path: cx.shared.static_root_path.as_deref(),
|
||||
description: "List of crates",
|
||||
keywords: BASIC_KEYWORDS,
|
||||
resource_suffix: &cx.shared.resource_suffix,
|
||||
|
@ -1513,7 +1513,7 @@ impl<'a> SourceCollector<'a> {
|
|||
title: &title,
|
||||
css_class: "source",
|
||||
root_path: &root_path,
|
||||
static_root_path: self.scx.static_root_path.deref(),
|
||||
static_root_path: self.scx.static_root_path.as_deref(),
|
||||
description: &desc,
|
||||
keywords: BASIC_KEYWORDS,
|
||||
resource_suffix: &self.scx.resource_suffix,
|
||||
|
@ -2110,7 +2110,7 @@ impl Context {
|
|||
title: "List of all items in this crate",
|
||||
css_class: "mod",
|
||||
root_path: "../",
|
||||
static_root_path: self.shared.static_root_path.deref(),
|
||||
static_root_path: self.shared.static_root_path.as_deref(),
|
||||
description: "List of all items in this crate",
|
||||
keywords: BASIC_KEYWORDS,
|
||||
resource_suffix: &self.shared.resource_suffix,
|
||||
|
@ -2137,7 +2137,7 @@ impl Context {
|
|||
self.shared.fs.write(&final_file, &v)?;
|
||||
|
||||
// Generating settings page.
|
||||
let settings = Settings::new(self.shared.static_root_path.deref().unwrap_or("./"),
|
||||
let settings = Settings::new(self.shared.static_root_path.as_deref().unwrap_or("./"),
|
||||
&self.shared.resource_suffix);
|
||||
page.title = "Rustdoc settings";
|
||||
page.description = "Settings of Rustdoc";
|
||||
|
@ -2195,7 +2195,7 @@ impl Context {
|
|||
let page = layout::Page {
|
||||
css_class: tyname,
|
||||
root_path: &self.root_path(),
|
||||
static_root_path: self.shared.static_root_path.deref(),
|
||||
static_root_path: self.shared.static_root_path.as_deref(),
|
||||
title: &title,
|
||||
description: &desc,
|
||||
keywords: &keywords,
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Some(42).as_deref();
|
||||
//~^ ERROR no method named `as_deref` found for type `std::option::Option<{integer}>`
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref` found for type `std::option::Option<{integer}>` in the current scope
|
||||
--> $DIR/option-as_deref.rs:4:29
|
||||
|
|
||||
LL | let _result = &Some(42).as_deref();
|
||||
| ^^^^^^^^ help: there is a method with a similar name: `as_ref`
|
||||
|
|
||||
= note: the method `as_deref` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &mut Some(42).as_deref_mut();
|
||||
//~^ ERROR no method named `as_deref_mut` found for type `std::option::Option<{integer}>`
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_mut` found for type `std::option::Option<{integer}>` in the current scope
|
||||
--> $DIR/option-as_deref_mut.rs:4:33
|
||||
|
|
||||
LL | let _result = &mut Some(42).as_deref_mut();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
= note: the method `as_deref_mut` 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 = &Some(42).deref();
|
||||
//~^ ERROR no method named `deref` found for type `std::option::Option<{integer}>`
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
error[E0599]: no method named `deref` found for type `std::option::Option<{integer}>` in the current scope
|
||||
--> $DIR/option-deref.rs:4:29
|
||||
|
|
||||
LL | let _result = &Some(42).deref();
|
||||
| ^^^^^
|
||||
|
|
||||
= note: the method `deref` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Ok(42).as_deref();
|
||||
//~^ ERROR no method named `as_deref` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref` found for type `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-as_deref.rs:4:27
|
||||
|
|
||||
LL | let _result = &Ok(42).as_deref();
|
||||
| ^^^^^^^^ help: there is a method with a similar name: `as_ref`
|
||||
|
|
||||
= note: the method `as_deref` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Err(41).as_deref_err();
|
||||
//~^ ERROR no method named `as_deref_err` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_err` found for type `std::result::Result<_, {integer}>` in the current scope
|
||||
--> $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`
|
||||
|
|
||||
= note: the method `as_deref_err` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &mut Ok(42).as_deref_mut();
|
||||
//~^ ERROR no method named `as_deref_mut` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_mut` found for type `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-as_deref_mut.rs:4:31
|
||||
|
|
||||
LL | let _result = &mut Ok(42).as_deref_mut();
|
||||
| ^^^^^^^^^^^^ help: there is a method with a similar name: `as_deref_err`
|
||||
|
|
||||
= note: the method `as_deref_mut` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &mut Err(41).as_deref_mut_err();
|
||||
//~^ ERROR no method named `as_deref_mut_err` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_mut_err` found for type `std::result::Result<_, {integer}>` in the current scope
|
||||
--> $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`
|
||||
|
|
||||
= note: the method `as_deref_mut_err` 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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &mut Ok(42).as_deref_mut_ok();
|
||||
//~^ ERROR no method named `as_deref_mut_ok` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_mut_ok` found for type `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`.
|
|
@ -0,0 +1,6 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Ok(42).as_deref_ok();
|
||||
//~^ ERROR no method named `as_deref_ok` found
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
error[E0599]: no method named `as_deref_ok` found for type `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`.
|
|
@ -1,6 +0,0 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Err(41).deref_err();
|
||||
//~^ ERROR no method named `deref_err` found
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
error[E0599]: no method named `deref_err` found for type `std::result::Result<_, {integer}>` in the current scope
|
||||
--> $DIR/result-deref-err.rs:4:28
|
||||
|
|
||||
LL | let _result = &Err(41).deref_err();
|
||||
| ^^^^^^^^^ help: there is a method with a similar name: `deref_ok`
|
||||
|
|
||||
= note: the method `deref_err` 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`.
|
|
@ -1,6 +0,0 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Ok(42).deref_ok();
|
||||
//~^ ERROR no method named `deref_ok` found
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
error[E0599]: no method named `deref_ok` found for type `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-deref-ok.rs:4:27
|
||||
|
|
||||
LL | let _result = &Ok(42).deref_ok();
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= note: the method `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`.
|
|
@ -1,6 +0,0 @@
|
|||
#![feature(inner_deref)]
|
||||
|
||||
fn main() {
|
||||
let _result = &Ok(42).deref();
|
||||
//~^ ERROR no method named `deref` found
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
error[E0599]: no method named `deref` found for type `std::result::Result<{integer}, _>` in the current scope
|
||||
--> $DIR/result-deref.rs:4:27
|
||||
|
|
||||
LL | let _result = &Ok(42).deref();
|
||||
| ^^^^^
|
||||
|
|
||||
= note: the method `deref` 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 a new issue