From 436dadb683cc82b8d2310a3a015c66bf08398432 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:02:54 -0400 Subject: [PATCH] refactor: switch to associated type for SortsRow (#828) --- .../process_table/proc_widget_column.rs | 4 +- src/components/data_table/sortable.rs | 37 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/app/widgets/process_table/proc_widget_column.rs b/src/app/widgets/process_table/proc_widget_column.rs index 5578598d..4830bc65 100644 --- a/src/app/widgets/process_table/proc_widget_column.rs +++ b/src/app/widgets/process_table/proc_widget_column.rs @@ -64,7 +64,9 @@ impl ColumnHeader for ProcColumn { } } -impl SortsRow for ProcColumn { +impl SortsRow for ProcColumn { + type DataType = ProcWidgetData; + fn sort_data(&self, data: &mut [ProcWidgetData], descending: bool) { match self { ProcColumn::CpuPercent => { diff --git a/src/components/data_table/sortable.rs b/src/components/data_table/sortable.rs index 4614f08c..f661937e 100644 --- a/src/components/data_table/sortable.rs +++ b/src/components/data_table/sortable.rs @@ -100,13 +100,15 @@ impl SortType for Sortable { } } -pub trait SortsRow { +pub trait SortsRow { + type DataType; + /// Sorts data. - fn sort_data(&self, data: &mut [DataType], descending: bool); + fn sort_data(&self, data: &mut [Self::DataType], descending: bool); } #[derive(Debug, Clone)] -pub struct SortColumn { +pub struct SortColumn { /// The inner column header. inner: T, @@ -118,13 +120,11 @@ pub struct SortColumn { /// Marks that this column is currently "hidden", and should *always* be skipped. pub is_hidden: bool, - - _pd: PhantomData, } -impl DataTableColumn for SortColumn +impl DataTableColumn for SortColumn where - T: ColumnHeader + SortsRow, + T: ColumnHeader + SortsRow, { #[inline] fn inner(&self) -> &T { @@ -165,9 +165,9 @@ where } } -impl SortColumn +impl SortColumn where - T: ColumnHeader + SortsRow, + T: ColumnHeader + SortsRow, { /// Creates a new [`SortColumn`] with a width that follows the header width, which has no shortcut and sorts by /// default in ascending order ([`SortOrder::Ascending`]). @@ -177,7 +177,6 @@ where bounds: ColumnWidthBounds::FollowHeader, is_hidden: false, default_order: SortOrder::default(), - _pd: Default::default(), } } @@ -189,7 +188,6 @@ where bounds: ColumnWidthBounds::Hard(width), is_hidden: false, default_order: SortOrder::default(), - _pd: Default::default(), } } @@ -204,7 +202,6 @@ where }, is_hidden: false, default_order: SortOrder::default(), - _pd: Default::default(), } } @@ -221,7 +218,7 @@ where } /// Given a [`SortColumn`] and the sort order, sort a mutable slice of associated data. - pub fn sort_by(&self, data: &mut [DataType], order: SortOrder) { + pub fn sort_by(&self, data: &mut [D], order: SortOrder) { let descending = matches!(order, SortOrder::Descending); self.inner.sort_data(data, descending); } @@ -234,14 +231,14 @@ pub struct SortDataTableProps { } /// A type alias for a sortable [`DataTable`]. -pub type SortDataTable = DataTable>; +pub type SortDataTable = DataTable>; -impl SortDataTable +impl SortDataTable where - DataType: DataToCell, - H: ColumnHeader + SortsRow, + D: DataToCell, + H: ColumnHeader + SortsRow, { - pub fn new_sortable>>>( + pub fn new_sortable>>>( columns: C, props: SortDataTableProps, styling: DataTableStyling, ) -> Self { Self { @@ -377,7 +374,9 @@ mod test { } } - impl SortsRow for ColumnType { + impl SortsRow for ColumnType { + type DataType = TestType; + fn sort_data(&self, data: &mut [TestType], descending: bool) { match self { ColumnType::Index => data.sort_by_key(|t| t.index),