From c025fdebbada2757deccdd97219ff0313631f2ed Mon Sep 17 00:00:00 2001 From: Brad Gibson Date: Mon, 30 Apr 2018 12:51:43 -0700 Subject: [PATCH] fixed some and added more tests --- src/libcore/option.rs | 2 +- src/libcore/tests/option.rs | 8 ++ src/libcore/tests/result.rs | 96 ++++++++++++++++--- .../option_deref.rs | 16 ++++ .../result_deref.rs | 16 ++++ .../result_deref_err.rs | 16 ++++ .../result_deref_ok.rs | 16 ++++ 7 files changed, 155 insertions(+), 15 deletions(-) create mode 100644 src/test/compile-fail/issue-50264-inner-deref-trait/option_deref.rs create mode 100644 src/test/compile-fail/issue-50264-inner-deref-trait/result_deref.rs create mode 100644 src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_err.rs create mode 100644 src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_ok.rs diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 506e54ba5e4..deb0380171c 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -878,7 +878,7 @@ impl Option { } } -# [unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] +#[unstable(feature = "inner_deref", reason = "newly added", issue = "50264")] impl Option { /// Converts from `&Option` to `Option<&T::Target>`. /// diff --git a/src/libcore/tests/option.rs b/src/libcore/tests/option.rs index e7e48b2daa2..ced7b03636a 100644 --- a/src/libcore/tests/option.rs +++ b/src/libcore/tests/option.rs @@ -304,7 +304,15 @@ fn test_option_deref() { let ref_option = &Some(&42); assert_eq!(ref_option.deref(), Some(&42)); + let ref_option = &Some(String::from("a result")); + assert_eq!(ref_option.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][..])); + // None: &Option>::None -> None let ref_option: &Option<&i32> = &None; assert_eq!(ref_option.deref(), None); + + } diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index dd23467322d..fd0dd21401b 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -234,21 +234,89 @@ fn test_try() { #[test] fn test_result_deref() { - // Ok(T).deref_ok() -> Result<&T, &E::Deref::Target>::Ok(&T) - let ref_ok: &Result<&i32, &u8> = &Ok(&42); - assert_eq!(ref_ok.deref_ok(), Ok(&42)); - assert_eq!(ref_ok.deref_ok(), Ok(&42)); - assert_eq!(ref_ok.deref(), Ok(&42)); + // &Result::Ok(T).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); - // Err(E) -> Result<&T, &E::Deref::Target>::Err(&*E) - let ref_err: &Result<&i32, &u8> = &Err(&41); - assert_eq!(ref_err.deref_err(), Err(&41)); - assert_eq!(ref_err.deref_err(), Err(&41)); - assert_eq!(ref_err.deref(), Err(&41)); + let ref_ok = &Result::Ok::(String::from("a result")); + let expected_result = Result::Ok::<&str, &u32>("a result"); + assert_eq!(ref_ok.deref_ok(), expected_result); - // &Ok(T).deref_err() -> Result<&T, &E::Deref::Target>::Ok(&T) - assert_eq!(ref_ok.deref_err(), Ok(&&42)); + let ref_ok = &Result::Ok::, 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); - // &Err(E) -> Result<&T::Deref::Target, &E>::Err(&E) - assert_eq!(ref_err.deref_ok(), Err(&&41)); + // &Result::Ok(T).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); + + let ref_ok = &Result::Ok::(String::from("a result")); + let expected_result = Result::Ok::<&str, &u32>("a result"); + assert_eq!(ref_ok.deref(), expected_result); + + let ref_ok = &Result::Ok::, &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); + + // &Result::Err(T).deref_err() -> + // Result<&T, &E::Deref::Target>::Err(&*E) + let ref_err = &Result::Err::(&41); + let expected_result = Result::Err::<&u8, &i32>(&41); + assert_eq!(ref_err.deref_err(), expected_result); + + let ref_err = &Result::Err::(String::from("an error")); + let expected_result = Result::Err::<&u32, &str>("an error"); + assert_eq!(ref_err.deref_err(), expected_result); + + let ref_err = &Result::Err::>(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); + + // &Result::Err(T).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); + + 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); + + let ref_err = &Result::Err::<&u32, Vec>(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); + + // *Odd corner cases (tested for completeness)* + + // &Result::Ok(T).deref_err() -> + // Result<&T, &E::Deref::Target>::Ok(&T) + let ref_ok = &Result::Ok::(42); + let expected_result = Result::Ok::<&i32, &u8>(&42); + assert_eq!(ref_ok.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); + + 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); + + // &Result::Err(E).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); + + 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); + + 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); } diff --git a/src/test/compile-fail/issue-50264-inner-deref-trait/option_deref.rs b/src/test/compile-fail/issue-50264-inner-deref-trait/option_deref.rs new file mode 100644 index 00000000000..4c67fb3bef1 --- /dev/null +++ b/src/test/compile-fail/issue-50264-inner-deref-trait/option_deref.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(inner_deref)] + +fn main() { + let _result = &Some(42).deref(); +//~^ ERROR no method named `deref` found for type `std::option::Option<{integer}>` +} diff --git a/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref.rs b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref.rs new file mode 100644 index 00000000000..73bdf0b9209 --- /dev/null +++ b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(inner_deref)] + +fn main() { + let _result = &Ok(42).deref(); +//~^ ERROR no method named `deref` found +} diff --git a/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_err.rs b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_err.rs new file mode 100644 index 00000000000..5d1e7472d8f --- /dev/null +++ b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_err.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(inner_deref)] + +fn main() { + let _result = &Err(41).deref_err(); +//~^ ERROR no method named `deref_err` found +} diff --git a/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_ok.rs b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_ok.rs new file mode 100644 index 00000000000..bee8e0c062b --- /dev/null +++ b/src/test/compile-fail/issue-50264-inner-deref-trait/result_deref_ok.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(inner_deref)] + +fn main() { + let _result = &Ok(42).deref_ok(); +//~^ ERROR no method named `deref_ok` found +}