Skip to content

Commit 06aa86a

Browse files
authored
Rollup merge of rust-lang#59628 - U007D:master, r=Kimundi
`as_deref()` and `as_deref_mut()` impls addresses rust-lang#50264 renamed `deref()` -> `as_deref()` added `deref_mut()` impls + tests fixed breaking changes
2 parents 5fb8949 + e71e71b commit 06aa86a

29 files changed

+391
-134
lines changed

src/libcore/option.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
#![stable(feature = "rust1", since = "1.0.0")]
137137

138138
use iter::{FromIterator, FusedIterator, TrustedLen};
139-
use {hint, mem, ops::{self, Deref}};
139+
use {hint, mem, ops::{self, Deref, DerefMut}};
140140
use pin::Pin;
141141

142142
// Note that this is not a lang item per se, but it has a hidden dependency on
@@ -991,15 +991,26 @@ impl<T: Default> Option<T> {
991991

992992
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
993993
impl<T: Deref> Option<T> {
994-
/// Converts from `&Option<T>` to `Option<&T::Target>`.
994+
/// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
995995
///
996-
/// Leaves the original Option in-place, creating a new one with a reference
997-
/// to the original one, additionally coercing the contents via `Deref`.
998-
pub fn deref(&self) -> Option<&T::Target> {
996+
/// Leaves the original `Option` in-place, creating a new one containing a reference to the
997+
/// inner type's `Deref::Target` type.
998+
pub fn as_deref(&self) -> Option<&T::Target> {
999999
self.as_ref().map(|t| t.deref())
10001000
}
10011001
}
10021002

1003+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
1004+
impl<T: DerefMut> Option<T> {
1005+
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
1006+
///
1007+
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
1008+
/// the inner type's `Deref::Target` type.
1009+
pub fn as_deref_mut(&mut self) -> Option<&mut T::Target> {
1010+
self.as_mut().map(|t| t.deref_mut())
1011+
}
1012+
}
1013+
10031014
impl<T, E> Option<Result<T, E>> {
10041015
/// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
10051016
///

src/libcore/result.rs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232

233233
use fmt;
234234
use iter::{FromIterator, FusedIterator, TrustedLen};
235-
use ops::{self, Deref};
235+
use ops::{self, Deref, DerefMut};
236236

237237
/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
238238
///
@@ -927,42 +927,75 @@ impl<T: Default, E> Result<T, E> {
927927

928928
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
929929
impl<T: Deref, E> Result<T, E> {
930-
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`.
930+
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E>`.
931931
///
932-
/// Leaves the original Result in-place, creating a new one with a reference
933-
/// to the original one, additionally coercing the `Ok` arm of the Result via
934-
/// `Deref`.
935-
pub fn deref_ok(&self) -> Result<&T::Target, &E> {
932+
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
933+
/// `Ok` type's `Deref::Target` type.
934+
pub fn as_deref_ok(&self) -> Result<&T::Target, &E> {
936935
self.as_ref().map(|t| t.deref())
937936
}
938937
}
939938

940939
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
941940
impl<T, E: Deref> Result<T, E> {
942-
/// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`.
941+
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T, &E::Target>`.
943942
///
944-
/// Leaves the original Result in-place, creating a new one with a reference
945-
/// to the original one, additionally coercing the `Err` arm of the Result via
946-
/// `Deref`.
947-
pub fn deref_err(&self) -> Result<&T, &E::Target>
943+
/// Leaves the original `Result` in-place, creating a new one containing a reference to the
944+
/// `Err` type's `Deref::Target` type.
945+
pub fn as_deref_err(&self) -> Result<&T, &E::Target>
948946
{
949947
self.as_ref().map_err(|e| e.deref())
950948
}
951949
}
952950

953951
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
954952
impl<T: Deref, E: Deref> Result<T, E> {
955-
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`.
953+
/// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&T::Target, &E::Target>`.
956954
///
957-
/// Leaves the original Result in-place, creating a new one with a reference
958-
/// to the original one, additionally coercing both the `Ok` and `Err` arms
959-
/// of the Result via `Deref`.
960-
pub fn deref(&self) -> Result<&T::Target, &E::Target>
955+
/// Leaves the original `Result` in-place, creating a new one containing a reference to both
956+
/// the `Ok` and `Err` types' `Deref::Target` types.
957+
pub fn as_deref(&self) -> Result<&T::Target, &E::Target>
961958
{
962959
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
963960
}
964961
}
965962

963+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
964+
impl<T: DerefMut, E> Result<T, E> {
965+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T::Target, &mut E>`.
966+
///
967+
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
968+
/// the `Ok` type's `Deref::Target` type.
969+
pub fn as_deref_mut_ok(&mut self) -> Result<&mut T::Target, &mut E> {
970+
self.as_mut().map(|t| t.deref_mut())
971+
}
972+
}
973+
974+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
975+
impl<T, E: DerefMut> Result<T, E> {
976+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut T, &mut E::Target>`.
977+
///
978+
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
979+
/// the `Err` type's `Deref::Target` type.
980+
pub fn as_deref_mut_err(&mut self) -> Result<&mut T, &mut E::Target>
981+
{
982+
self.as_mut().map_err(|e| e.deref_mut())
983+
}
984+
}
985+
986+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
987+
impl<T: DerefMut, E: DerefMut> Result<T, E> {
988+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to
989+
/// `Result<&mut T::Target, &mut E::Target>`.
990+
///
991+
/// Leaves the original `Result` in-place, creating a new one containing a mutable reference to
992+
/// both the `Ok` and `Err` types' `Deref::Target` types.
993+
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E::Target>
994+
{
995+
self.as_mut().map(|t| t.deref_mut()).map_err(|e| e.deref_mut())
996+
}
997+
}
998+
966999
impl<T, E> Result<Option<T>, E> {
9671000
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
9681001
///

src/libcore/tests/option.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use core::option::*;
22
use core::mem;
33
use core::clone::Clone;
4+
use core::array::FixedSizeArray;
5+
use core::ops::DerefMut;
46

57
#[test]
68
fn test_get_ptr() {
@@ -310,20 +312,38 @@ fn test_try() {
310312
}
311313

312314
#[test]
313-
fn test_option_deref() {
315+
fn test_option_as_deref() {
314316
// Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
315317
let ref_option = &Some(&42);
316-
assert_eq!(ref_option.deref(), Some(&42));
318+
assert_eq!(ref_option.as_deref(), Some(&42));
317319

318320
let ref_option = &Some(String::from("a result"));
319-
assert_eq!(ref_option.deref(), Some("a result"));
321+
assert_eq!(ref_option.as_deref(), Some("a result"));
320322

321323
let ref_option = &Some(vec![1, 2, 3, 4, 5]);
322-
assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..]));
324+
assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
323325

324326
// None: &Option<T: Deref>>::None -> None
325327
let ref_option: &Option<&i32> = &None;
326-
assert_eq!(ref_option.deref(), None);
328+
assert_eq!(ref_option.as_deref(), None);
329+
}
330+
331+
#[test]
332+
fn test_option_as_deref_mut() {
333+
// Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
334+
let mut val = 42;
335+
let ref_option = &mut Some(&mut val);
336+
assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
337+
338+
let ref_option = &mut Some(String::from("a result"));
339+
assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
340+
341+
let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
342+
assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
343+
344+
// None: &mut Option<T: Deref>>::None -> None
345+
let ref_option: &mut Option<&mut i32> = &mut None;
346+
assert_eq!(ref_option.as_deref_mut(), None);
327347
}
328348

329349
#[test]

0 commit comments

Comments
 (0)