From f8f1fca0e97c0b39fb88942e8975ef1362d70bd2 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 13:47:22 +0700 Subject: [PATCH 01/13] refactor: replace macro args with traits --- src/app.rs | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/app.rs b/src/app.rs index ac9846e5..87fc2052 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,11 +4,12 @@ pub use sub::Sub; use crate::{ args::{Args, Quantity, Threads}, - get_size::GetApparentSize, + bytes_format::BytesFormat, + get_size::{GetApparentSize, GetSize}, json_data::{JsonData, UnitAndTree}, reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport}, runtime_error::RuntimeError, - size::{self, Bytes}, + size, visualizer::{BarAlignment, Direction, Visualizer}, }; use clap::Parser; @@ -18,10 +19,7 @@ use std::{io::stdin, time::Duration}; use sysinfo::Disks; #[cfg(unix)] -use crate::{ - get_size::{GetBlockCount, GetBlockSize}, - size::Blocks, -}; +use crate::get_size::{GetBlockCount, GetBlockSize}; /// The main application. pub struct App { @@ -141,11 +139,36 @@ impl App { ) } + trait GetSizeUtils: GetSize { + type FormatSizeOutput; + fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput; + } + + impl GetSizeUtils for GetApparentSize { + type FormatSizeOutput = BytesFormat; + fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput { + bytes_format + } + } + + #[cfg(unix)] + impl GetSizeUtils for GetBlockSize { + type FormatSizeOutput = BytesFormat; + fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput { + bytes_format + } + } + + #[cfg(unix)] + impl GetSizeUtils for GetBlockCount { + type FormatSizeOutput = (); + fn format_size(_: BytesFormat) -> Self::FormatSizeOutput {} + } + macro_rules! run { ($( $(#[$variant_attrs:meta])* { - $size:ty => $format:expr; $quantity:ident => $size_getter:ident; $progress:literal => $create_reporter:ident; } @@ -167,8 +190,8 @@ impl App { direction: Direction::from_top_down(top_down), bar_alignment: BarAlignment::from_align_right(align_right), size_getter: $size_getter, - reporter: $create_reporter::<$size>(report_error), - bytes_format: $format(bytes_format), + reporter: $create_reporter::<<$size_getter as GetSize>::Size>(report_error), + bytes_format: <$size_getter as GetSizeUtils>::format_size(bytes_format), files, json_output, column_width_distribution, @@ -182,41 +205,35 @@ impl App { run! { { - Bytes => |x| x; ApparentSize => GetApparentSize; false => error_only_reporter; } { - Bytes => |x| x; ApparentSize => GetApparentSize; true => progress_and_error_reporter; } #[cfg(unix)] { - Bytes => |x| x; BlockSize => GetBlockSize; false => error_only_reporter; } #[cfg(unix)] { - Bytes => |x| x; BlockSize => GetBlockSize; true => progress_and_error_reporter; } #[cfg(unix)] { - Blocks => |_| (); BlockCount => GetBlockCount; false => error_only_reporter; } #[cfg(unix)] { - Blocks => |_| (); BlockCount => GetBlockCount; true => progress_and_error_reporter; } From 452786e6bdc003f98f4fc340a3472570c3c24318 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 14:09:27 +0700 Subject: [PATCH 02/13] refactor: shorten the macro calls --- src/app.rs | 45 ++++++++------------------------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/src/app.rs b/src/app.rs index 87fc2052..7528c203 100644 --- a/src/app.rs +++ b/src/app.rs @@ -168,10 +168,8 @@ impl App { macro_rules! run { ($( $(#[$variant_attrs:meta])* - { - $quantity:ident => $size_getter:ident; - $progress:literal => $create_reporter:ident; - } + $quantity:ident, $size_getter:ident, + $progress:literal, $create_reporter:ident; )*) => { match self.args {$( $(#[$variant_attrs])* Args { @@ -204,39 +202,12 @@ impl App { } run! { - { - ApparentSize => GetApparentSize; - false => error_only_reporter; - } - - { - ApparentSize => GetApparentSize; - true => progress_and_error_reporter; - } - - #[cfg(unix)] - { - BlockSize => GetBlockSize; - false => error_only_reporter; - } - - #[cfg(unix)] - { - BlockSize => GetBlockSize; - true => progress_and_error_reporter; - } - - #[cfg(unix)] - { - BlockCount => GetBlockCount; - false => error_only_reporter; - } - - #[cfg(unix)] - { - BlockCount => GetBlockCount; - true => progress_and_error_reporter; - } + ApparentSize, GetApparentSize, false, error_only_reporter; + ApparentSize, GetApparentSize, true, progress_and_error_reporter; + #[cfg(unix)] BlockSize, GetBlockSize, false, error_only_reporter; + #[cfg(unix)] BlockSize, GetBlockSize, true, progress_and_error_reporter; + #[cfg(unix)] BlockCount, GetBlockCount, false, error_only_reporter; + #[cfg(unix)] BlockCount, GetBlockCount, true, progress_and_error_reporter; } } } From d8902c1902130d73940406f122df039d17141e1c Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 14:31:57 +0700 Subject: [PATCH 03/13] refactor: infer reporter via utility traits --- src/app.rs | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7528c203..54887599 100644 --- a/src/app.rs +++ b/src/app.rs @@ -117,26 +117,35 @@ impl App { ErrorReport::TEXT }; - #[allow(clippy::extra_unused_type_parameters)] - fn error_only_reporter( - report_error: fn(ErrorReport), - ) -> ErrorOnlyReporter { - ErrorOnlyReporter::new(report_error) + trait CreateReporter { + type Reporter; + fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter; } - fn progress_and_error_reporter( - report_error: fn(ErrorReport), - ) -> ProgressAndErrorReporter + impl CreateReporter for () + where + Size: size::Size, + { + type Reporter = ErrorOnlyReporter; + fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter { + ErrorOnlyReporter::new(report_error) + } + } + + impl CreateReporter for () where Size: size::Size + Into + Send + Sync, ProgressReport: Default + 'static, u64: Into, { - ProgressAndErrorReporter::new( - ProgressReport::TEXT, - Duration::from_millis(100), - report_error, - ) + type Reporter = ProgressAndErrorReporter; + fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter { + ProgressAndErrorReporter::new( + ProgressReport::TEXT, + Duration::from_millis(100), + report_error, + ) + } } trait GetSizeUtils: GetSize { @@ -168,8 +177,7 @@ impl App { macro_rules! run { ($( $(#[$variant_attrs:meta])* - $quantity:ident, $size_getter:ident, - $progress:literal, $create_reporter:ident; + $quantity:ident, $size_getter:ident, $progress:literal; )*) => { match self.args {$( $(#[$variant_attrs])* Args { @@ -188,7 +196,7 @@ impl App { direction: Direction::from_top_down(top_down), bar_alignment: BarAlignment::from_align_right(align_right), size_getter: $size_getter, - reporter: $create_reporter::<<$size_getter as GetSize>::Size>(report_error), + reporter: <() as CreateReporter<$progress, <$size_getter as GetSize>::Size>>::create_reporter(report_error), bytes_format: <$size_getter as GetSizeUtils>::format_size(bytes_format), files, json_output, @@ -202,12 +210,12 @@ impl App { } run! { - ApparentSize, GetApparentSize, false, error_only_reporter; - ApparentSize, GetApparentSize, true, progress_and_error_reporter; - #[cfg(unix)] BlockSize, GetBlockSize, false, error_only_reporter; - #[cfg(unix)] BlockSize, GetBlockSize, true, progress_and_error_reporter; - #[cfg(unix)] BlockCount, GetBlockCount, false, error_only_reporter; - #[cfg(unix)] BlockCount, GetBlockCount, true, progress_and_error_reporter; + ApparentSize, GetApparentSize, false; + ApparentSize, GetApparentSize, true; + #[cfg(unix)] BlockSize, GetBlockSize, false; + #[cfg(unix)] BlockSize, GetBlockSize, true; + #[cfg(unix)] BlockCount, GetBlockCount, false; + #[cfg(unix)] BlockCount, GetBlockCount, true; } } } From 5c91d8a97185fecf3ab25990d66629a4dcce048b Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 14:33:18 +0700 Subject: [PATCH 04/13] refactor: remove unnecessary trait bound --- src/app.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.rs b/src/app.rs index 54887599..953779a9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -148,7 +148,7 @@ impl App { } } - trait GetSizeUtils: GetSize { + trait GetSizeUtils { type FormatSizeOutput; fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput; } From 4c1fde295770187711350636916f8b6cb3461243 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 14:43:07 +0700 Subject: [PATCH 05/13] refactor: remove `FormatSizeOutput` --- src/app.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index 953779a9..93163086 100644 --- a/src/app.rs +++ b/src/app.rs @@ -148,30 +148,26 @@ impl App { } } - trait GetSizeUtils { - type FormatSizeOutput; - fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput; + trait GetSizeUtils: GetSize { + fn format_size(bytes_format: BytesFormat) -> ::DisplayFormat; } impl GetSizeUtils for GetApparentSize { - type FormatSizeOutput = BytesFormat; - fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput { + fn format_size(bytes_format: BytesFormat) -> BytesFormat { bytes_format } } #[cfg(unix)] impl GetSizeUtils for GetBlockSize { - type FormatSizeOutput = BytesFormat; - fn format_size(bytes_format: BytesFormat) -> Self::FormatSizeOutput { + fn format_size(bytes_format: BytesFormat) -> BytesFormat { bytes_format } } #[cfg(unix)] impl GetSizeUtils for GetBlockCount { - type FormatSizeOutput = (); - fn format_size(_: BytesFormat) -> Self::FormatSizeOutput {} + fn format_size(_: BytesFormat) {} } macro_rules! run { From 408111837ccae27cb6f87e54609a0b610b91cb82 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 14:48:00 +0700 Subject: [PATCH 06/13] refactor: use better function name --- src/app.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/app.rs b/src/app.rs index 93163086..65d1c37e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -149,25 +149,25 @@ impl App { } trait GetSizeUtils: GetSize { - fn format_size(bytes_format: BytesFormat) -> ::DisplayFormat; + fn formatter(bytes_format: BytesFormat) -> ::DisplayFormat; } impl GetSizeUtils for GetApparentSize { - fn format_size(bytes_format: BytesFormat) -> BytesFormat { + fn formatter(bytes_format: BytesFormat) -> BytesFormat { bytes_format } } #[cfg(unix)] impl GetSizeUtils for GetBlockSize { - fn format_size(bytes_format: BytesFormat) -> BytesFormat { + fn formatter(bytes_format: BytesFormat) -> BytesFormat { bytes_format } } #[cfg(unix)] impl GetSizeUtils for GetBlockCount { - fn format_size(_: BytesFormat) {} + fn formatter(_: BytesFormat) {} } macro_rules! run { @@ -193,7 +193,7 @@ impl App { bar_alignment: BarAlignment::from_align_right(align_right), size_getter: $size_getter, reporter: <() as CreateReporter<$progress, <$size_getter as GetSize>::Size>>::create_reporter(report_error), - bytes_format: <$size_getter as GetSizeUtils>::format_size(bytes_format), + bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format), files, json_output, column_width_distribution, From 06597a8841c64a575c18e898eee382dbdaa9d865 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:14:40 +0700 Subject: [PATCH 07/13] refactor: remove `$size_getter` --- src/app.rs | 55 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/src/app.rs b/src/app.rs index 65d1c37e..b4e29a89 100644 --- a/src/app.rs +++ b/src/app.rs @@ -117,6 +117,39 @@ impl App { ErrorReport::TEXT }; + // we can't use `Quantity` directly as `const` parameter so we have to use numbers. + mod quantity_index { + pub const APPARENT_SIZE: u8 = 0; + #[cfg(unix)] + pub const BLOCK_SIZE: u8 = 1; + #[cfg(unix)] + pub const BLOCK_COUNT: u8 = 2; + } + + trait IndexToQuantity { + const QUANTITY: Quantity; + type SizeGetter; + const SIZE_GETTER: Self::SizeGetter; + } + + impl IndexToQuantity<{ quantity_index::APPARENT_SIZE }> for () { + const QUANTITY: Quantity = Quantity::ApparentSize; + type SizeGetter = GetApparentSize; + const SIZE_GETTER: Self::SizeGetter = GetApparentSize; + } + + impl IndexToQuantity<{ quantity_index::BLOCK_SIZE }> for () { + const QUANTITY: Quantity = Quantity::BlockSize; + type SizeGetter = GetBlockSize; + const SIZE_GETTER: Self::SizeGetter = GetBlockSize; + } + + impl IndexToQuantity<{ quantity_index::BLOCK_COUNT }> for () { + const QUANTITY: Quantity = Quantity::BlockCount; + type SizeGetter = GetBlockCount; + const SIZE_GETTER: Self::SizeGetter = GetBlockCount; + } + trait CreateReporter { type Reporter; fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter; @@ -173,11 +206,11 @@ impl App { macro_rules! run { ($( $(#[$variant_attrs:meta])* - $quantity:ident, $size_getter:ident, $progress:literal; + $quantity_index:ident, $progress:literal; )*) => { match self.args {$( $(#[$variant_attrs])* Args { - quantity: Quantity::$quantity, + quantity: <() as IndexToQuantity<{ quantity_index::$quantity_index }>>::QUANTITY, progress: $progress, files, json_output, @@ -191,9 +224,9 @@ impl App { } => Sub { direction: Direction::from_top_down(top_down), bar_alignment: BarAlignment::from_align_right(align_right), - size_getter: $size_getter, - reporter: <() as CreateReporter<$progress, <$size_getter as GetSize>::Size>>::create_reporter(report_error), - bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format), + size_getter: <() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SIZE_GETTER, + reporter: <() as CreateReporter<$progress, <<() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SizeGetter as GetSize>::Size>>::create_reporter(report_error), + bytes_format: <<() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SizeGetter as GetSizeUtils>::formatter(bytes_format), files, json_output, column_width_distribution, @@ -206,12 +239,12 @@ impl App { } run! { - ApparentSize, GetApparentSize, false; - ApparentSize, GetApparentSize, true; - #[cfg(unix)] BlockSize, GetBlockSize, false; - #[cfg(unix)] BlockSize, GetBlockSize, true; - #[cfg(unix)] BlockCount, GetBlockCount, false; - #[cfg(unix)] BlockCount, GetBlockCount, true; + APPARENT_SIZE, false; + APPARENT_SIZE, true; + #[cfg(unix)] BLOCK_SIZE, false; + #[cfg(unix)] BLOCK_SIZE, true; + #[cfg(unix)] BLOCK_COUNT, false; + #[cfg(unix)] BLOCK_COUNT, true; } } } From a0c02d5738c0c6f639c24d2d1986a11ac19b7a67 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:19:30 +0700 Subject: [PATCH 08/13] refactor: rename a trait --- src/app.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/app.rs b/src/app.rs index b4e29a89..f235df29 100644 --- a/src/app.rs +++ b/src/app.rs @@ -126,25 +126,25 @@ impl App { pub const BLOCK_COUNT: u8 = 2; } - trait IndexToQuantity { + trait QuantityUtils { const QUANTITY: Quantity; type SizeGetter; const SIZE_GETTER: Self::SizeGetter; } - impl IndexToQuantity<{ quantity_index::APPARENT_SIZE }> for () { + impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () { const QUANTITY: Quantity = Quantity::ApparentSize; type SizeGetter = GetApparentSize; const SIZE_GETTER: Self::SizeGetter = GetApparentSize; } - impl IndexToQuantity<{ quantity_index::BLOCK_SIZE }> for () { + impl QuantityUtils<{ quantity_index::BLOCK_SIZE }> for () { const QUANTITY: Quantity = Quantity::BlockSize; type SizeGetter = GetBlockSize; const SIZE_GETTER: Self::SizeGetter = GetBlockSize; } - impl IndexToQuantity<{ quantity_index::BLOCK_COUNT }> for () { + impl QuantityUtils<{ quantity_index::BLOCK_COUNT }> for () { const QUANTITY: Quantity = Quantity::BlockCount; type SizeGetter = GetBlockCount; const SIZE_GETTER: Self::SizeGetter = GetBlockCount; @@ -210,7 +210,7 @@ impl App { )*) => { match self.args {$( $(#[$variant_attrs])* Args { - quantity: <() as IndexToQuantity<{ quantity_index::$quantity_index }>>::QUANTITY, + quantity: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::QUANTITY, progress: $progress, files, json_output, @@ -224,9 +224,9 @@ impl App { } => Sub { direction: Direction::from_top_down(top_down), bar_alignment: BarAlignment::from_align_right(align_right), - size_getter: <() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SIZE_GETTER, - reporter: <() as CreateReporter<$progress, <<() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SizeGetter as GetSize>::Size>>::create_reporter(report_error), - bytes_format: <<() as IndexToQuantity<{ quantity_index::$quantity_index }>>::SizeGetter as GetSizeUtils>::formatter(bytes_format), + size_getter: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::SIZE_GETTER, + reporter: <() as CreateReporter<$progress, <<() as QuantityUtils<{ quantity_index::$quantity_index }>>::SizeGetter as GetSize>::Size>>::create_reporter(report_error), + bytes_format: <<() as QuantityUtils<{ quantity_index::$quantity_index }>>::SizeGetter as GetSizeUtils>::formatter(bytes_format), files, json_output, column_width_distribution, From 87b1e454a6fbebcc40c2b565df8b6bbd6567656c Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:23:47 +0700 Subject: [PATCH 09/13] refactor: merge `GetSizeUtils` into `QuantityUtils` --- src/app.rs | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/app.rs b/src/app.rs index f235df29..63deeaeb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -128,26 +128,36 @@ impl App { trait QuantityUtils { const QUANTITY: Quantity; - type SizeGetter; + type SizeGetter: GetSize; const SIZE_GETTER: Self::SizeGetter; + fn formatter( + bytes_format: BytesFormat, + ) -> <::Size as size::Size>::DisplayFormat; } impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () { const QUANTITY: Quantity = Quantity::ApparentSize; type SizeGetter = GetApparentSize; const SIZE_GETTER: Self::SizeGetter = GetApparentSize; + fn formatter(bytes_format: BytesFormat) -> BytesFormat { + bytes_format + } } impl QuantityUtils<{ quantity_index::BLOCK_SIZE }> for () { const QUANTITY: Quantity = Quantity::BlockSize; type SizeGetter = GetBlockSize; const SIZE_GETTER: Self::SizeGetter = GetBlockSize; + fn formatter(bytes_format: BytesFormat) -> BytesFormat { + bytes_format + } } impl QuantityUtils<{ quantity_index::BLOCK_COUNT }> for () { const QUANTITY: Quantity = Quantity::BlockCount; type SizeGetter = GetBlockCount; const SIZE_GETTER: Self::SizeGetter = GetBlockCount; + fn formatter(_: BytesFormat) {} } trait CreateReporter { @@ -181,28 +191,6 @@ impl App { } } - trait GetSizeUtils: GetSize { - fn formatter(bytes_format: BytesFormat) -> ::DisplayFormat; - } - - impl GetSizeUtils for GetApparentSize { - fn formatter(bytes_format: BytesFormat) -> BytesFormat { - bytes_format - } - } - - #[cfg(unix)] - impl GetSizeUtils for GetBlockSize { - fn formatter(bytes_format: BytesFormat) -> BytesFormat { - bytes_format - } - } - - #[cfg(unix)] - impl GetSizeUtils for GetBlockCount { - fn formatter(_: BytesFormat) {} - } - macro_rules! run { ($( $(#[$variant_attrs:meta])* @@ -226,7 +214,7 @@ impl App { bar_alignment: BarAlignment::from_align_right(align_right), size_getter: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::SIZE_GETTER, reporter: <() as CreateReporter<$progress, <<() as QuantityUtils<{ quantity_index::$quantity_index }>>::SizeGetter as GetSize>::Size>>::create_reporter(report_error), - bytes_format: <<() as QuantityUtils<{ quantity_index::$quantity_index }>>::SizeGetter as GetSizeUtils>::formatter(bytes_format), + bytes_format: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::formatter(bytes_format), files, json_output, column_width_distribution, From 0c0f2b3260acde5924f1803797efb3ae8755a1a5 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:24:45 +0700 Subject: [PATCH 10/13] fix: add missing `#[cfg(unix)]` --- src/app.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app.rs b/src/app.rs index 63deeaeb..a309fe35 100644 --- a/src/app.rs +++ b/src/app.rs @@ -144,6 +144,7 @@ impl App { } } + #[cfg(unix)] impl QuantityUtils<{ quantity_index::BLOCK_SIZE }> for () { const QUANTITY: Quantity = Quantity::BlockSize; type SizeGetter = GetBlockSize; @@ -153,6 +154,7 @@ impl App { } } + #[cfg(unix)] impl QuantityUtils<{ quantity_index::BLOCK_COUNT }> for () { const QUANTITY: Quantity = Quantity::BlockCount; type SizeGetter = GetBlockCount; From c5c94eb4180fce3f5212dff5b132d44af1c722ff Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:39:37 +0700 Subject: [PATCH 11/13] refactor: simplify call-site syntax of `CreateReporter` --- src/app.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index a309fe35..43ca8ef3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -135,6 +135,9 @@ impl App { ) -> <::Size as size::Size>::DisplayFormat; } + type QuantityIndexToSizeType = + <<() as QuantityUtils>::SizeGetter as GetSize>::Size; + impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () { const QUANTITY: Quantity = Quantity::ApparentSize; type SizeGetter = GetApparentSize; @@ -162,14 +165,15 @@ impl App { fn formatter(_: BytesFormat) {} } - trait CreateReporter { + trait CreateReporter { type Reporter; fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter; } - impl CreateReporter for () + impl CreateReporter for () where - Size: size::Size, + (): QuantityUtils, + QuantityIndexToSizeType: size::Size, { type Reporter = ErrorOnlyReporter; fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter { @@ -177,13 +181,15 @@ impl App { } } - impl CreateReporter for () + impl CreateReporter for () where - Size: size::Size + Into + Send + Sync, - ProgressReport: Default + 'static, - u64: Into, + (): QuantityUtils, + QuantityIndexToSizeType: size::Size + Into + Send + Sync, + ProgressReport>: Default + 'static, + u64: Into>, { - type Reporter = ProgressAndErrorReporter; + type Reporter = + ProgressAndErrorReporter, fn(ErrorReport)>; fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter { ProgressAndErrorReporter::new( ProgressReport::TEXT, @@ -215,7 +221,7 @@ impl App { direction: Direction::from_top_down(top_down), bar_alignment: BarAlignment::from_align_right(align_right), size_getter: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::SIZE_GETTER, - reporter: <() as CreateReporter<$progress, <<() as QuantityUtils<{ quantity_index::$quantity_index }>>::SizeGetter as GetSize>::Size>>::create_reporter(report_error), + reporter: <() as CreateReporter<$progress, { quantity_index::$quantity_index }>>::create_reporter(report_error), bytes_format: <() as QuantityUtils<{ quantity_index::$quantity_index }>>::formatter(bytes_format), files, json_output, From b42e81fc483a3d8dc523a78acba6038448dba030 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:49:06 +0700 Subject: [PATCH 12/13] refactor: move alias closer to its use-site --- src/app.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index 43ca8ef3..1fab9409 100644 --- a/src/app.rs +++ b/src/app.rs @@ -135,9 +135,6 @@ impl App { ) -> <::Size as size::Size>::DisplayFormat; } - type QuantityIndexToSizeType = - <<() as QuantityUtils>::SizeGetter as GetSize>::Size; - impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () { const QUANTITY: Quantity = Quantity::ApparentSize; type SizeGetter = GetApparentSize; @@ -170,6 +167,9 @@ impl App { fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter; } + type QuantityIndexToSizeType = + <<() as QuantityUtils>::SizeGetter as GetSize>::Size; + impl CreateReporter for () where (): QuantityUtils, From 534fd7fae77f0cc8ec1ff2431375beb62c74bb24 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Tue, 8 Jul 2025 15:51:28 +0700 Subject: [PATCH 13/13] refactor: create an alias --- src/app.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1fab9409..e142f74e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -126,13 +126,14 @@ impl App { pub const BLOCK_COUNT: u8 = 2; } + type SizeGetterToDisplayFormat = + <::Size as size::Size>::DisplayFormat; + trait QuantityUtils { const QUANTITY: Quantity; type SizeGetter: GetSize; const SIZE_GETTER: Self::SizeGetter; - fn formatter( - bytes_format: BytesFormat, - ) -> <::Size as size::Size>::DisplayFormat; + fn formatter(bytes_format: BytesFormat) -> SizeGetterToDisplayFormat; } impl QuantityUtils<{ quantity_index::APPARENT_SIZE }> for () {