From 3a129b2f55ac9e53ca780974603cfdbf5340dcff Mon Sep 17 00:00:00 2001 From: Gymore Date: Sun, 24 Jan 2021 06:31:26 +0100 Subject: [PATCH 1/3] Implementation of `Result::swap` and tests --- library/core/src/result.rs | 22 ++++++++++++++++++++++ library/core/tests/result.rs | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/library/core/src/result.rs b/library/core/src/result.rs index d6d1762572928..33ec0c8f33f73 100644 --- a/library/core/src/result.rs +++ b/library/core/src/result.rs @@ -592,6 +592,28 @@ impl Result { } } + /// Maps a `Result` to a `Result` by swapping the success/failure + /// semantics of the stored variant. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// #![feature(result_swap)] + /// + /// assert_eq!(Ok("Error :(").swap(), Err("Error :(")); + /// assert_eq!(Err("Success!").swap(), Ok("Success!")); + /// ``` + #[inline] + #[unstable(feature = "result_swap", issue = "none")] + pub fn swap(self) -> Result { + match self { + Ok(t) => Err(t), + Err(e) => Ok(e), + } + } + ///////////////////////////////////////////////////////////////////////// // Iterator constructors ///////////////////////////////////////////////////////////////////////// diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index 81660870e95e4..c257368574504 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -358,3 +358,10 @@ fn result_opt_conversions() { assert_eq!(res, Err(BadNumErr)) } + +#[test] +fn test_result_swap() { + assert_eq!(Ok("err").swap(), Err("err")); + assert_eq!(Err("ok").swap(), Ok("ok")); + assert_eq!(Ok("ok").swap().swap(), Ok("ok")); +} From 9c800abed78d95e75d4549a53268e431b9ca5b64 Mon Sep 17 00:00:00 2001 From: Gymore Date: Sun, 24 Jan 2021 07:13:14 +0100 Subject: [PATCH 2/3] Add feature attribute `result_swap` for tests --- library/core/tests/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 98f5982fbb2c1..572db1c74a19b 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -76,6 +76,7 @@ #![feature(integer_atomics)] #![feature(slice_group_by)] #![feature(trusted_random_access)] +#![feature(result_swap)] #![deny(unsafe_op_in_unsafe_fn)] extern crate test; From 5381c26bb2e62c0a07c632e055a2b0617b7966ee Mon Sep 17 00:00:00 2001 From: Gymore Date: Sun, 24 Jan 2021 07:52:32 +0100 Subject: [PATCH 3/3] Specify type parameters in the `test_result_swap` test function --- library/core/tests/result.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/tests/result.rs b/library/core/tests/result.rs index c257368574504..4dc3a0c7f0fcb 100644 --- a/library/core/tests/result.rs +++ b/library/core/tests/result.rs @@ -361,7 +361,7 @@ fn result_opt_conversions() { #[test] fn test_result_swap() { - assert_eq!(Ok("err").swap(), Err("err")); - assert_eq!(Err("ok").swap(), Ok("ok")); - assert_eq!(Ok("ok").swap().swap(), Ok("ok")); + assert_eq!(Ok::<_, u8>("err").swap(), Err("err")); + assert_eq!(Err::("ok").swap(), Ok("ok")); + assert_eq!(Ok::<_, u8>("ok").swap().swap(), Ok("ok")); }