Skip to content

Commit ea442e5

Browse files
committed
added DerefOption and DerefResult + tests to std
1 parent 165cb86 commit ea442e5

File tree

9 files changed

+224
-3
lines changed

9 files changed

+224
-3
lines changed

src/libcore/option.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
#![stable(feature = "rust1", since = "1.0.0")]
147147

148148
use iter::{FromIterator, FusedIterator, TrustedLen};
149-
use {mem, ops};
149+
use {mem, ops::{self, Deref}};
150150

151151
// Note that this is not a lang item per se, but it has a hidden dependency on
152152
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -878,6 +878,17 @@ impl<T: Default> Option<T> {
878878
}
879879
}
880880

881+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
882+
impl<T: Deref> Option<T> {
883+
/// Converts from `&Option<T>` to `Option<&T::Target>`.
884+
///
885+
/// Leaves the original Option in-place, creating a new one with a reference
886+
/// to the original one, additionally coercing the contents via `Deref`.
887+
pub fn deref(&self) -> Option<&T::Target> {
888+
self.as_ref().map(|t| t.deref())
889+
}
890+
}
891+
881892
impl<T, E> Option<Result<T, E>> {
882893
/// Transposes an `Option` of a `Result` into a `Result` of an `Option`.
883894
///
@@ -914,7 +925,6 @@ fn expect_failed(msg: &str) -> ! {
914925
panic!("{}", msg)
915926
}
916927

917-
918928
/////////////////////////////////////////////////////////////////////////////
919929
// Trait implementations
920930
/////////////////////////////////////////////////////////////////////////////

src/libcore/result.rs

+39-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242

243243
use fmt;
244244
use iter::{FromIterator, FusedIterator, TrustedLen};
245-
use ops;
245+
use ops::{self, Deref};
246246

247247
/// `Result` is a type that represents either success ([`Ok`]) or failure ([`Err`]).
248248
///
@@ -909,6 +909,44 @@ impl<T: Default, E> Result<T, E> {
909909
}
910910
}
911911

912+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
913+
impl<T: Deref, E> Result<T, E> {
914+
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E>`.
915+
///
916+
/// Leaves the original Result in-place, creating a new one with a reference
917+
/// to the original one, additionally coercing the `Ok` arm of the Result via
918+
/// `Deref`.
919+
pub fn deref_ok(&self) -> Result<&T::Target, &E> {
920+
self.as_ref().map(|t| t.deref())
921+
}
922+
}
923+
924+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
925+
impl<T, E: Deref> Result<T, E> {
926+
/// Converts from `&Result<T, E>` to `Result<&T, &E::Target>`.
927+
///
928+
/// Leaves the original Result in-place, creating a new one with a reference
929+
/// to the original one, additionally coercing the `Err` arm of the Result via
930+
/// `Deref`.
931+
pub fn deref_err(&self) -> Result<&T, &E::Target>
932+
{
933+
self.as_ref().map_err(|e| e.deref())
934+
}
935+
}
936+
937+
#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")]
938+
impl<T: Deref, E: Deref> Result<T, E> {
939+
/// Converts from `&Result<T, E>` to `Result<&T::Target, &E::Target>`.
940+
///
941+
/// Leaves the original Result in-place, creating a new one with a reference
942+
/// to the original one, additionally coercing both the `Ok` and `Err` arms
943+
/// of the Result via `Deref`.
944+
pub fn deref(&self) -> Result<&T::Target, &E::Target>
945+
{
946+
self.as_ref().map(|t| t.deref()).map_err(|e| e.deref())
947+
}
948+
}
949+
912950
impl<T, E> Result<Option<T>, E> {
913951
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
914952
///

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#![feature(reverse_bits)]
4747
#![feature(inclusive_range_fields)]
4848
#![feature(iterator_find_map)]
49+
#![feature(inner_deref)]
4950

5051
extern crate core;
5152
extern crate test;

src/libcore/tests/option.rs

+19
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,22 @@ fn test_try() {
297297
}
298298
assert_eq!(try_option_err(), Err(NoneError));
299299
}
300+
301+
#[test]
302+
fn test_option_deref() {
303+
// Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
304+
let ref_option = &Some(&42);
305+
assert_eq!(ref_option.deref(), Some(&42));
306+
307+
let ref_option = &Some(String::from("the answer"));
308+
assert_eq!(ref_option.deref(), Some("the answer"));
309+
310+
let ref_option = &Some(vec![1, 2, 3, 4, 5]);
311+
assert_eq!(ref_option.deref(), Some(&[1, 2, 3, 4, 5][..]));
312+
313+
// None: &Option<T: Deref>>::None -> None
314+
let ref_option: &Option<&i32> = &None;
315+
assert_eq!(ref_option.deref(), None);
316+
317+
318+
}

src/libcore/tests/result.rs

+89
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,92 @@ fn test_try() {
231231
}
232232
assert_eq!(try_result_err(), Err(1));
233233
}
234+
235+
#[test]
236+
fn test_result_deref() {
237+
// &Result<T: Deref, E>::Ok(T).deref_ok() ->
238+
// Result<&T::Deref::Target, &E>::Ok(&*T)
239+
let ref_ok = &Result::Ok::<&i32, u8>(&42);
240+
let expected_result = Result::Ok::<&i32, &u8>(&42);
241+
assert_eq!(ref_ok.deref_ok(), expected_result);
242+
243+
let ref_ok = &Result::Ok::<String, u32>(String::from("the answer"));
244+
let expected_result = Result::Ok::<&str, &u32>("the answer");
245+
assert_eq!(ref_ok.deref_ok(), expected_result);
246+
247+
let ref_ok = &Result::Ok::<Vec<i32>, u32>(vec![1, 2, 3, 4, 5]);
248+
let expected_result = Result::Ok::<&[i32], &u32>(&[1, 2, 3, 4, 5][..]);
249+
assert_eq!(ref_ok.deref_ok(), expected_result);
250+
251+
// &Result<T: Deref, E: Deref>::Ok(T).deref() ->
252+
// Result<&T::Deref::Target, &E::Deref::Target>::Ok(&*T)
253+
let ref_ok = &Result::Ok::<&i32, &u8>(&42);
254+
let expected_result = Result::Ok::<&i32, &u8>(&42);
255+
assert_eq!(ref_ok.deref(), expected_result);
256+
257+
let ref_ok = &Result::Ok::<String, &u32>(String::from("a result"));
258+
let expected_result = Result::Ok::<&str, &u32>("a result");
259+
assert_eq!(ref_ok.deref(), expected_result);
260+
261+
let ref_ok = &Result::Ok::<Vec<i32>, &u32>(vec![1, 2, 3, 4, 5]);
262+
let expected_result = Result::Ok::<&[i32], &u32>(&[1, 2, 3, 4, 5][..]);
263+
assert_eq!(ref_ok.deref(), expected_result);
264+
265+
// &Result<T, E: Deref>::Err(T).deref_err() ->
266+
// Result<&T, &E::Deref::Target>::Err(&*E)
267+
let ref_err = &Result::Err::<u8, &i32>(&41);
268+
let expected_result = Result::Err::<&u8, &i32>(&41);
269+
assert_eq!(ref_err.deref_err(), expected_result);
270+
271+
let ref_err = &Result::Err::<u32, String>(String::from("an error"));
272+
let expected_result = Result::Err::<&u32, &str>("an error");
273+
assert_eq!(ref_err.deref_err(), expected_result);
274+
275+
let ref_err = &Result::Err::<u32, Vec<i32>>(vec![5, 4, 3, 2, 1]);
276+
let expected_result = Result::Err::<&u32, &[i32]>(&[5, 4, 3, 2, 1][..]);
277+
assert_eq!(ref_err.deref_err(), expected_result);
278+
279+
// &Result<T: Deref, E: Deref>::Err(T).deref_err() ->
280+
// Result<&T, &E::Deref::Target>::Err(&*E)
281+
let ref_err = &Result::Err::<&u8, &i32>(&41);
282+
let expected_result = Result::Err::<&u8, &i32>(&41);
283+
assert_eq!(ref_err.deref(), expected_result);
284+
285+
let ref_err = &Result::Err::<&u32, String>(String::from("an error"));
286+
let expected_result = Result::Err::<&u32, &str>("an error");
287+
assert_eq!(ref_err.deref(), expected_result);
288+
289+
let ref_err = &Result::Err::<&u32, Vec<i32>>(vec![5, 4, 3, 2, 1]);
290+
let expected_result = Result::Err::<&u32, &[i32]>(&[5, 4, 3, 2, 1][..]);
291+
assert_eq!(ref_err.deref(), expected_result);
292+
293+
// *Odd corner cases (tested for completeness)*
294+
295+
// &Result<T, E: Deref>::Ok(T).deref_err() ->
296+
// Result<&T, &E::Deref::Target>::Ok(&T)
297+
let ref_ok = &Result::Ok::<i32, &u8>(42);
298+
let expected_result = Result::Ok::<&i32, &u8>(&42);
299+
assert_eq!(ref_ok.deref_err(), expected_result);
300+
301+
let ref_ok = &Result::Ok::<&str, &u32>("a result");
302+
let expected_result = Result::Ok::<&&str, &u32>(&"a result");
303+
assert_eq!(ref_ok.deref_err(), expected_result);
304+
305+
let ref_ok = &Result::Ok::<[i32; 5], &u32>([1, 2, 3, 4, 5]);
306+
let expected_result = Result::Ok::<&[i32; 5], &u32>(&[1, 2, 3, 4, 5]);
307+
assert_eq!(ref_ok.deref_err(), expected_result);
308+
309+
// &Result<T: Deref, E>::Err(E).deref_ok() ->
310+
// Result<&T::Deref::Target, &E>::Err(&E)
311+
let ref_err = &Result::Err::<&u8, i32>(41);
312+
let expected_result = Result::Err::<&u8, &i32>(&41);
313+
assert_eq!(ref_err.deref_ok(), expected_result);
314+
315+
let ref_err = &Result::Err::<&u32, &str>("an error");
316+
let expected_result = Result::Err::<&u32, &&str>(&"an error");
317+
assert_eq!(ref_err.deref_ok(), expected_result);
318+
319+
let ref_err = &Result::Err::<&u32, [i32; 5]>([5, 4, 3, 2, 1]);
320+
let expected_result = Result::Err::<&u32, &[i32; 5]>(&[5, 4, 3, 2, 1]);
321+
assert_eq!(ref_err.deref_ok(), expected_result);
322+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(inner_deref)]
12+
13+
fn main() {
14+
let _result = &Some(42).deref();
15+
//~^ ERROR no method named `deref` found for type `std::option::Option<{integer}>`
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(inner_deref)]
12+
13+
fn main() {
14+
let _result = &Ok(42).deref();
15+
//~^ ERROR no method named `deref` found
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(inner_deref)]
12+
13+
fn main() {
14+
let _result = &Err(41).deref_err();
15+
//~^ ERROR no method named `deref_err` found
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(inner_deref)]
12+
13+
fn main() {
14+
let _result = &Ok(42).deref_ok();
15+
//~^ ERROR no method named `deref_ok` found
16+
}

0 commit comments

Comments
 (0)