From 83f45a310e01846db0775ae80c9cba703bf430d0 Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Thu, 13 Jul 2023 11:21:54 +0200 Subject: [PATCH 1/6] Add conversion into (Option,Option) to EitherOrBoth Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index cf65fe788..ba2428eda 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -464,7 +464,7 @@ impl EitherOrBoth { impl EitherOrBoth { /// Return either value of left, right, or apply a function `f` to both values if both are present. /// The input function has to return the same type as both Right and Left carry. - /// + /// /// # Examples /// ``` /// # use itertools::EitherOrBoth; @@ -493,3 +493,13 @@ impl Into>> for EitherOrBoth { } } } + +impl Into<(Option, Option)> for EitherOrBoth { + fn into(self) -> (Option, Option) { + match self.map_any(Some, Some) { + EitherOrBoth::Left(l) => (l, None), + EitherOrBoth::Right(r) => (None, r), + EitherOrBoth::Both(l, r) => (l, r), + } + } +} From ca23089e0baa4f1701d33d897c25ac5112a2d699 Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Thu, 13 Jul 2023 11:31:48 +0200 Subject: [PATCH 2/6] Change to use From instead of Into Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index ba2428eda..b6bda9da0 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -494,9 +494,9 @@ impl Into>> for EitherOrBoth { } } -impl Into<(Option, Option)> for EitherOrBoth { - fn into(self) -> (Option, Option) { - match self.map_any(Some, Some) { +impl From> for (Option, Option) { + fn from(val: EitherOrBoth) -> Self { + match val.map_any(Some, Some) { EitherOrBoth::Left(l) => (l, None), EitherOrBoth::Right(r) => (None, r), EitherOrBoth::Both(l, r) => (l, r), From d171ceea2e5544126ab9e5308df69c82b65a79ac Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Sat, 15 Jul 2023 13:48:36 +0200 Subject: [PATCH 3/6] Add left_or_right() method to EitherOrBoth, delegate From impl to it. Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index b6bda9da0..481f1cd1e 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -65,6 +65,18 @@ impl EitherOrBoth { } } + /// Return tupel of options corresponding to the left and right value respectively + /// + /// If `Left` return `(Some(..), None)`, if `Right` return `(None,Some(..))`, else return + /// `(Some(..),Some(..))` + pub fn left_and_right(self) -> (Option, Option) { + match self.map_any(Some, Some) { + EitherOrBoth::Left(l) => (l, None), + EitherOrBoth::Right(r) => (None, r), + EitherOrBoth::Both(l, r) => (l, r), + } + } + /// If `Left`, return `Some` with the left value. If `Right` or `Both`, return `None`. /// /// # Examples @@ -496,10 +508,6 @@ impl Into>> for EitherOrBoth { impl From> for (Option, Option) { fn from(val: EitherOrBoth) -> Self { - match val.map_any(Some, Some) { - EitherOrBoth::Left(l) => (l, None), - EitherOrBoth::Right(r) => (None, r), - EitherOrBoth::Both(l, r) => (l, r), - } + val.left_and_right() } } From 44f5f7b0324e5abf5d785e6b093681a071ba6fd4 Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Sat, 15 Jul 2023 13:50:53 +0200 Subject: [PATCH 4/6] Correct spelling of tuple Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index 481f1cd1e..410f0563f 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -65,7 +65,7 @@ impl EitherOrBoth { } } - /// Return tupel of options corresponding to the left and right value respectively + /// Return tuple of options corresponding to the left and right value respectively /// /// If `Left` return `(Some(..), None)`, if `Right` return `(None,Some(..))`, else return /// `(Some(..),Some(..))` From 5fe48e5037701c7dc3fcd55b4d6e1d62233d508c Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Sat, 15 Jul 2023 13:52:06 +0200 Subject: [PATCH 5/6] Add inline(always) Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index 410f0563f..43a24545c 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -507,6 +507,7 @@ impl Into>> for EitherOrBoth { } impl From> for (Option, Option) { + #[inline(always)] fn from(val: EitherOrBoth) -> Self { val.left_and_right() } From d7a539f8e35ae8df58ec3d414b13e559ac0b58a8 Mon Sep 17 00:00:00 2001 From: Justus Fluegel Date: Thu, 27 Jul 2023 17:51:45 +0200 Subject: [PATCH 6/6] Remove From impl, change left_and_right impl to use .or_default() Signed-off-by: Justus Fluegel --- src/either_or_both.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index 43a24545c..64e1c871c 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -70,11 +70,7 @@ impl EitherOrBoth { /// If `Left` return `(Some(..), None)`, if `Right` return `(None,Some(..))`, else return /// `(Some(..),Some(..))` pub fn left_and_right(self) -> (Option, Option) { - match self.map_any(Some, Some) { - EitherOrBoth::Left(l) => (l, None), - EitherOrBoth::Right(r) => (None, r), - EitherOrBoth::Both(l, r) => (l, r), - } + self.map_any(Some, Some).or_default() } /// If `Left`, return `Some` with the left value. If `Right` or `Both`, return `None`. @@ -505,10 +501,3 @@ impl Into>> for EitherOrBoth { } } } - -impl From> for (Option, Option) { - #[inline(always)] - fn from(val: EitherOrBoth) -> Self { - val.left_and_right() - } -}