Skip to content

Commit 45ea30f

Browse files
committed
chore(git): merge from master
2 parents 8495347 + 6654299 commit 45ea30f

2 files changed

Lines changed: 113 additions & 89 deletions

File tree

src/app.rs

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ pub use sub::Sub;
44

55
use crate::{
66
args::{Args, Quantity, Threads},
7-
get_size::GetApparentSize,
7+
bytes_format::BytesFormat,
8+
get_size::{GetApparentSize, GetSize},
89
json_data::{JsonData, UnitAndTree},
910
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
1011
runtime_error::RuntimeError,
11-
size::{self, Bytes},
12+
size,
1213
visualizer::{BarAlignment, Direction, Visualizer},
1314
};
1415
use clap::Parser;
@@ -18,10 +19,7 @@ use std::{io::stdin, time::Duration};
1819
use sysinfo::Disks;
1920

2021
#[cfg(unix)]
21-
use crate::{
22-
get_size::{GetBlockCount, GetBlockSize},
23-
size::Blocks,
24-
};
22+
use crate::get_size::{GetBlockCount, GetBlockSize};
2523

2624
/// The main application.
2725
pub struct App {
@@ -119,40 +117,76 @@ impl App {
119117
ErrorReport::TEXT
120118
};
121119

122-
#[allow(clippy::extra_unused_type_parameters)]
123-
fn error_only_reporter<Size>(
124-
report_error: fn(ErrorReport),
125-
) -> ErrorOnlyReporter<fn(ErrorReport)> {
126-
ErrorOnlyReporter::new(report_error)
120+
trait GetSizeUtils: GetSize<Size: size::Size> {
121+
const INSTANCE: Self;
122+
const QUANTITY: Quantity;
123+
fn formatter(bytes_format: BytesFormat) -> <Self::Size as size::Size>::DisplayFormat;
124+
}
125+
126+
impl GetSizeUtils for GetApparentSize {
127+
const INSTANCE: Self = GetApparentSize;
128+
const QUANTITY: Quantity = Quantity::ApparentSize;
129+
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
130+
bytes_format
131+
}
132+
}
133+
134+
#[cfg(unix)]
135+
impl GetSizeUtils for GetBlockSize {
136+
const INSTANCE: Self = GetBlockSize;
137+
const QUANTITY: Quantity = Quantity::BlockSize;
138+
fn formatter(bytes_format: BytesFormat) -> BytesFormat {
139+
bytes_format
140+
}
141+
}
142+
143+
#[cfg(unix)]
144+
impl GetSizeUtils for GetBlockCount {
145+
const INSTANCE: Self = GetBlockCount;
146+
const QUANTITY: Quantity = Quantity::BlockCount;
147+
fn formatter(_: BytesFormat) {}
148+
}
149+
150+
trait CreateReporter<const REPORT_PROGRESS: bool>: GetSizeUtils {
151+
type Reporter;
152+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter;
153+
}
154+
155+
impl<SizeGetter> CreateReporter<false> for SizeGetter
156+
where
157+
SizeGetter: GetSizeUtils,
158+
{
159+
type Reporter = ErrorOnlyReporter<fn(ErrorReport)>;
160+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
161+
ErrorOnlyReporter::new(report_error)
162+
}
127163
}
128164

129-
fn progress_and_error_reporter<Size>(
130-
report_error: fn(ErrorReport),
131-
) -> ProgressAndErrorReporter<Size, fn(ErrorReport)>
165+
impl<SizeGetter> CreateReporter<true> for SizeGetter
132166
where
133-
Size: size::Size + Into<u64> + Send + Sync,
134-
ProgressReport<Size>: Default + 'static,
135-
u64: Into<Size>,
167+
SizeGetter: GetSizeUtils,
168+
SizeGetter::Size: Into<u64> + Send + Sync,
169+
ProgressReport<SizeGetter::Size>: Default + 'static,
170+
u64: Into<SizeGetter::Size>,
136171
{
137-
ProgressAndErrorReporter::new(
138-
ProgressReport::TEXT,
139-
Duration::from_millis(100),
140-
report_error,
141-
)
172+
type Reporter = ProgressAndErrorReporter<SizeGetter::Size, fn(ErrorReport)>;
173+
fn create_reporter(report_error: fn(ErrorReport)) -> Self::Reporter {
174+
ProgressAndErrorReporter::new(
175+
ProgressReport::TEXT,
176+
Duration::from_millis(100),
177+
report_error,
178+
)
179+
}
142180
}
143181

144182
macro_rules! run {
145183
($(
146184
$(#[$variant_attrs:meta])*
147-
{
148-
$size:ty => $format:expr;
149-
$quantity:ident => $size_getter:ident;
150-
$progress:literal => $create_reporter:ident;
151-
}
185+
$size_getter:ident, $progress:literal;
152186
)*) => { match self.args {$(
153187
$(#[$variant_attrs])*
154188
Args {
155-
quantity: Quantity::$quantity,
189+
quantity: <$size_getter as GetSizeUtils>::QUANTITY,
156190
progress: $progress,
157191
files,
158192
json_output,
@@ -166,9 +200,9 @@ impl App {
166200
} => Sub {
167201
direction: Direction::from_top_down(top_down),
168202
bar_alignment: BarAlignment::from_align_right(align_right),
169-
size_getter: $size_getter,
170-
reporter: $create_reporter::<$size>(report_error),
171-
bytes_format: $format(bytes_format),
203+
size_getter: <$size_getter as GetSizeUtils>::INSTANCE,
204+
reporter: <$size_getter as CreateReporter<$progress>>::create_reporter(report_error),
205+
bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format),
172206
files,
173207
json_output,
174208
column_width_distribution,
@@ -181,45 +215,12 @@ impl App {
181215
}
182216

183217
run! {
184-
{
185-
Bytes => |x| x;
186-
ApparentSize => GetApparentSize;
187-
false => error_only_reporter;
188-
}
189-
190-
{
191-
Bytes => |x| x;
192-
ApparentSize => GetApparentSize;
193-
true => progress_and_error_reporter;
194-
}
195-
196-
#[cfg(unix)]
197-
{
198-
Bytes => |x| x;
199-
BlockSize => GetBlockSize;
200-
false => error_only_reporter;
201-
}
202-
203-
#[cfg(unix)]
204-
{
205-
Bytes => |x| x;
206-
BlockSize => GetBlockSize;
207-
true => progress_and_error_reporter;
208-
}
209-
210-
#[cfg(unix)]
211-
{
212-
Blocks => |_| ();
213-
BlockCount => GetBlockCount;
214-
false => error_only_reporter;
215-
}
216-
217-
#[cfg(unix)]
218-
{
219-
Blocks => |_| ();
220-
BlockCount => GetBlockCount;
221-
true => progress_and_error_reporter;
222-
}
218+
GetApparentSize, false;
219+
GetApparentSize, true;
220+
#[cfg(unix)] GetBlockSize, false;
221+
#[cfg(unix)] GetBlockSize, true;
222+
#[cfg(unix)] GetBlockCount, false;
223+
#[cfg(unix)] GetBlockCount, true;
223224
}
224225
}
225226
}

src/size.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ use std::{
99
#[cfg(feature = "json")]
1010
use serde::{Deserialize, Serialize};
1111

12+
mod mul_traits {
13+
use std::ops::{Mul, MulAssign};
14+
pub trait MulAssignEx<Rhs>: Mul<Rhs, Output = Self> + MulAssign<Rhs> + Sized {}
15+
impl<Lhs: Mul<Rhs, Output = Lhs> + MulAssign<Rhs>, Rhs> MulAssignEx<Rhs> for Lhs {}
16+
}
17+
use mul_traits::MulAssignEx;
18+
1219
/// Types whose values can be used as disk usage statistic.
1320
pub trait Size:
1421
Debug
@@ -24,6 +31,10 @@ pub trait Size:
2431
+ Sub<Output = Self>
2532
+ SubAssign
2633
+ Sum
34+
+ MulAssignEx<u8>
35+
+ MulAssignEx<u16>
36+
+ MulAssignEx<u32>
37+
+ MulAssignEx<u64>
2738
{
2839
/// Underlying type
2940
type Inner: From<Self> + Into<Self> + Mul<Self, Output = Self>;
@@ -35,6 +46,36 @@ pub trait Size:
3546
fn display(self, input: Self::DisplayFormat) -> Self::DisplayOutput;
3647
}
3748

49+
macro_rules! impl_mul {
50+
($name:ident: $inner:ident *= $($num_type:ident)+) => {
51+
$(
52+
impl Mul<$num_type> for $name {
53+
type Output = Self;
54+
fn mul(self, rhs: $num_type) -> Self::Output {
55+
self.0.mul(rhs as $inner).into()
56+
}
57+
}
58+
59+
impl Mul<$name> for $num_type {
60+
type Output = $name;
61+
fn mul(self, rhs: $name) -> Self::Output {
62+
rhs * self
63+
}
64+
}
65+
66+
impl MulAssign<$num_type> for $name {
67+
fn mul_assign(&mut self, rhs: $num_type) {
68+
self.0 *= rhs as $inner;
69+
}
70+
}
71+
)+
72+
};
73+
74+
($name:ident: u64) => {
75+
impl_mul!($name: u64 *= u8 u16 u32 u64);
76+
};
77+
}
78+
3879
macro_rules! newtype {
3980
(
4081
$(#[$attribute:meta])*
@@ -67,25 +108,7 @@ macro_rules! newtype {
67108
}
68109
}
69110

70-
impl Mul<$inner> for $name {
71-
type Output = Self;
72-
fn mul(self, rhs: $inner) -> Self::Output {
73-
self.0.mul(rhs).into()
74-
}
75-
}
76-
77-
impl Mul<$name> for $inner {
78-
type Output = $name;
79-
fn mul(self, rhs: $name) -> Self::Output {
80-
rhs * self
81-
}
82-
}
83-
84-
impl MulAssign<$inner> for $name {
85-
fn mul_assign(&mut self, rhs: $inner) {
86-
self.0 *= rhs;
87-
}
88-
}
111+
impl_mul!($name: u64);
89112
};
90113
}
91114

0 commit comments

Comments
 (0)