From 3591ff1a9942024ad6211ec706294b111df45803 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 13 Sep 2023 15:30:43 +0200 Subject: [PATCH] chore(ext): simplify CollectionWidget impl --- src/ext.rs | 70 ++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/src/ext.rs b/src/ext.rs index 984a0f6b..6638965c 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -20,7 +20,10 @@ impl<'a, Message: 'static> ElementExt for crate::Element<'a, Message> { } /// Additional methods for the [`Column`] and [`Row`] widgets. -pub trait CollectionWidget<'a, Message>: Widget { +pub trait CollectionWidget<'a, Message: 'a>: Widget +where + Self: Sized, +{ /// Moves all the elements of `other` into `self`, leaving `other` empty. #[must_use] fn append(self, other: &mut Vec) -> Self @@ -29,36 +32,23 @@ pub trait CollectionWidget<'a, Message>: Widget { /// Appends all elements in an iterator to the widget. #[must_use] - fn extend(self, iterator: impl Iterator) -> Self + fn extend(mut self, iterator: impl Iterator) -> Self where - E: Into>; + E: Into>, + { + for item in iterator { + self = self.push(item.into()); + } + + self + } + + /// Pushes an element into the widget. + #[must_use] + fn push(self, element: impl Into>) -> Self; /// Conditionally pushes an element to the widget. #[must_use] - fn push_maybe(self, element: Option>>) -> Self; -} - -impl<'a, Message> CollectionWidget<'a, Message> - for crate::widget::Column<'a, Message, crate::Renderer> -{ - fn append(self, other: &mut Vec) -> Self - where - E: Into>, - { - self.extend(other.drain(..)) - } - - fn extend(mut self, iterator: impl Iterator) -> Self - where - E: Into>, - { - for item in iterator { - self = self.push(item.into()); - } - - self - } - fn push_maybe(self, element: Option>>) -> Self { if let Some(element) = element { self.push(element.into()) @@ -68,9 +58,7 @@ impl<'a, Message> CollectionWidget<'a, Message> } } -impl<'a, Message> CollectionWidget<'a, Message> - for crate::widget::Row<'a, Message, crate::Renderer> -{ +impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Column<'a, Message> { fn append(self, other: &mut Vec) -> Self where E: Into>, @@ -78,22 +66,20 @@ impl<'a, Message> CollectionWidget<'a, Message> self.extend(other.drain(..)) } - fn extend(mut self, iterator: impl Iterator) -> Self + fn push(self, element: impl Into>) -> Self { + self.push(element) + } +} + +impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Row<'a, Message> { + fn append(self, other: &mut Vec) -> Self where E: Into>, { - for item in iterator { - self = self.push(item.into()); - } - - self + self.extend(other.drain(..)) } - fn push_maybe(self, element: Option>>) -> Self { - if let Some(element) = element { - self.push(element.into()) - } else { - self - } + fn push(self, element: impl Into>) -> Self { + self.push(element) } }