Skip to content

Commit

Permalink
Rollup merge of rust-lang#62421 - JohnTitor:U007D-master, r=alexcrichton
Browse files Browse the repository at this point in the history
Introduce `as_deref` to Option

This is re-submission for rust-lang#59628.
Renames `deref()` to `as_deref()` and adds `deref_mut()` impls and tests.

CC rust-lang#50264

r? @Kimundi
(I picked you as you're the previous reviewer.)
  • Loading branch information
Centril committed Jul 25, 2019
2 parents b7b2a4c + 59634bc commit ba6436f
Show file tree
Hide file tree
Showing 31 changed files with 393 additions and 136 deletions.
17 changes: 14 additions & 3 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
///
Expand Down
65 changes: 49 additions & 16 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]).
///
Expand Down Expand Up @@ -981,42 +981,75 @@ 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())
}
}

#[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`.
///
Expand Down
30 changes: 25 additions & 5 deletions src/libcore/tests/option.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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]
Expand Down
Loading

0 comments on commit ba6436f

Please sign in to comment.