Skip to content

Commit fd98a35

Browse files
committed
feat(api): simplify, eliminate .clone()
1 parent 2369f76 commit fd98a35

9 files changed

Lines changed: 73 additions & 100 deletions

File tree

src/app.rs

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ use std::{io::stdin, time::Duration};
2020
use sysinfo::Disks;
2121

2222
#[cfg(unix)]
23-
use crate::{
24-
get_size::{GetBlockCount, GetBlockSize},
25-
hardlink::HardlinkList,
26-
};
23+
use crate::get_size::{GetBlockCount, GetBlockSize};
2724

2825
/// The main application.
2926
pub struct App {
@@ -197,11 +194,7 @@ impl App {
197194
{
198195
type HardlinksHandler: hardlink::RecordHardlinks<Self::Size, Self::Reporter>
199196
+ sub::DeduplicateHardlinkSizes<Self::Size>;
200-
fn create_hardlinks_handler(
201-
record: <Self::HardlinksHandler as sub::DeduplicateHardlinkSizes<Self::Size>>::HardlinkRecord,
202-
) -> Self::HardlinksHandler;
203-
fn init_hardlink_record(
204-
) -> <Self::HardlinksHandler as sub::DeduplicateHardlinkSizes<Self::Size>>::HardlinkRecord;
197+
fn create_hardlinks_handler() -> Self::HardlinksHandler;
205198
}
206199

207200
impl<const REPORT_PROGRESS: bool, SizeGetter>
@@ -211,10 +204,9 @@ impl App {
211204
SizeGetter::Size: Send + Sync,
212205
{
213206
type HardlinksHandler = hardlink::HardlinkIgnorant;
214-
fn create_hardlinks_handler((): ()) -> Self::HardlinksHandler {
207+
fn create_hardlinks_handler() -> Self::HardlinksHandler {
215208
hardlink::HardlinkIgnorant
216209
}
217-
fn init_hardlink_record() {}
218210
}
219211

220212
#[cfg(unix)]
@@ -225,14 +217,9 @@ impl App {
225217
SizeGetter::Size: Send + Sync + 'static,
226218
SizeGetter::Reporter: crate::reporter::Reporter<SizeGetter::Size>,
227219
{
228-
type HardlinksHandler = hardlink::HardlinkAware<'static, Self::Size>;
229-
fn create_hardlinks_handler(
230-
record: &'static HardlinkList<Self::Size>,
231-
) -> Self::HardlinksHandler {
232-
hardlink::HardlinkAware::new(record)
233-
}
234-
fn init_hardlink_record() -> &'static HardlinkList<Self::Size> {
235-
HardlinkList::new().pipe(Box::new).pipe(Box::leak)
220+
type HardlinksHandler = hardlink::HardlinkAware<Self::Size>;
221+
fn create_hardlinks_handler() -> Self::HardlinksHandler {
222+
hardlink::HardlinkAware::new()
236223
}
237224
}
238225

@@ -256,30 +243,21 @@ impl App {
256243
min_ratio,
257244
no_sort,
258245
..
259-
} => {
260-
const DEDUPLICATE_HARDLINKS: bool = cfg!(unix) && $deduplicate_hardlinks;
261-
let hardlink_record = <$size_getter as HardlinkDeduplicationSystem<DEDUPLICATE_HARDLINKS, $progress>>::init_hardlink_record();
262-
let hardlinks_handler = <
263-
$size_getter as HardlinkDeduplicationSystem<DEDUPLICATE_HARDLINKS, $progress>
264-
>::create_hardlinks_handler(hardlink_record);
265-
266-
Sub {
267-
direction: Direction::from_top_down(top_down),
268-
bar_alignment: BarAlignment::from_align_right(align_right),
269-
size_getter: <$size_getter as GetSizeUtils>::INSTANCE,
270-
hardlinks_handler,
271-
hardlink_record,
272-
reporter: <$size_getter as CreateReporter<$progress>>::create_reporter(report_error),
273-
bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format),
274-
files,
275-
json_output,
276-
column_width_distribution,
277-
max_depth,
278-
min_ratio,
279-
no_sort,
280-
}
281-
.run()
282-
},
246+
} => Sub {
247+
direction: Direction::from_top_down(top_down),
248+
bar_alignment: BarAlignment::from_align_right(align_right),
249+
size_getter: <$size_getter as GetSizeUtils>::INSTANCE,
250+
hardlinks_handler: <$size_getter as HardlinkDeduplicationSystem<{ cfg!(unix) && $deduplicate_hardlinks }, $progress>>::create_hardlinks_handler(),
251+
reporter: <$size_getter as CreateReporter<$progress>>::create_reporter(report_error),
252+
bytes_format: <$size_getter as GetSizeUtils>::formatter(bytes_format),
253+
files,
254+
json_output,
255+
column_width_distribution,
256+
max_depth,
257+
min_ratio,
258+
no_sort,
259+
}
260+
.run(),
283261
)*} };
284262
}
285263

src/app/sub.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ where
2222
Report: ParallelReporter<Size> + Sync,
2323
Size: size::Size + Into<u64> + Serialize + Send + Sync,
2424
SizeGetter: GetSize<Size = Size> + Copy + Sync,
25-
HardlinksHandler: RecordHardlinks<Size, Report> + DeduplicateHardlinkSizes<Size> + Copy + Sync,
25+
HardlinksHandler: RecordHardlinks<Size, Report> + DeduplicateHardlinkSizes<Size> + Sync,
2626
JsonTree<Size>: Into<JsonDataBody>,
2727
{
2828
/// List of files and/or directories.
@@ -43,8 +43,6 @@ where
4343
pub size_getter: SizeGetter,
4444
/// Handle to detect, record, and deduplicate hardlinks.
4545
pub hardlinks_handler: HardlinksHandler,
46-
/// Record of detected hardlinks.
47-
pub hardlink_record: HardlinksHandler::HardlinkRecord,
4846
/// Reports measurement progress.
4947
pub reporter: Report,
5048
/// Minimal size proportion required to appear.
@@ -58,7 +56,7 @@ where
5856
Size: size::Size + Into<u64> + Serialize + Send + Sync,
5957
Report: ParallelReporter<Size> + Sync,
6058
SizeGetter: GetSize<Size = Size> + Copy + Sync,
61-
HardlinksHandler: RecordHardlinks<Size, Report> + DeduplicateHardlinkSizes<Size> + Copy + Sync,
59+
HardlinksHandler: RecordHardlinks<Size, Report> + DeduplicateHardlinkSizes<Size> + Sync,
6260
JsonTree<Size>: Into<JsonDataBody>,
6361
{
6462
/// Run the sub program.
@@ -73,7 +71,6 @@ where
7371
max_depth,
7472
size_getter,
7573
hardlinks_handler,
76-
hardlink_record,
7774
reporter,
7875
min_ratio,
7976
no_sort,
@@ -88,7 +85,7 @@ where
8885
reporter: &reporter,
8986
root,
9087
size_getter,
91-
hardlinks_recorder: hardlinks_handler,
88+
hardlinks_recorder: &hardlinks_handler,
9289
max_depth,
9390
}
9491
.into()
@@ -100,7 +97,6 @@ where
10097
return Sub {
10198
files: vec![".".into()],
10299
hardlinks_handler,
103-
hardlink_record,
104100
reporter,
105101
..self
106102
}
@@ -133,8 +129,7 @@ where
133129
if !no_sort {
134130
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());
135131
}
136-
let deduplication_record =
137-
HardlinksHandler::deduplicate_hardlink_sizes(&mut data_tree, hardlink_record);
132+
let deduplication_record = hardlinks_handler.deduplicate_hardlink_sizes(&mut data_tree);
138133
(data_tree, deduplication_record)
139134
};
140135

@@ -172,15 +167,13 @@ where
172167
}
173168

174169
/// Subroutines used by [`Sub`] to deduplicate sizes of detected hardlinks and report about it.
175-
pub trait DeduplicateHardlinkSizes<Size: size::Size> {
176-
/// Record of detected hardlinks.
177-
type HardlinkRecord;
170+
pub trait DeduplicateHardlinkSizes<Size: size::Size>: Sized {
178171
/// Report created by [`DeduplicateHardlinkSizes::deduplicate_hardlink_sizes`].
179172
type DeduplicationReport;
180173
/// Deduplicate the sizes of detected hardlinks and return a report object.
181174
fn deduplicate_hardlink_sizes(
175+
self,
182176
data_tree: &mut DataTree<OsStringDisplay, Size>,
183-
record: Self::HardlinkRecord,
184177
) -> Result<Self::DeduplicationReport, RuntimeError>;
185178
/// Handle the report.
186179
fn report_deduplication_results(
@@ -194,20 +187,20 @@ pub trait DeduplicateHardlinkSizes<Size: size::Size> {
194187
}
195188

196189
#[cfg(unix)]
197-
impl<'a, Size> DeduplicateHardlinkSizes<Size> for crate::hardlink::HardlinkAware<'a, Size>
190+
impl<Size> DeduplicateHardlinkSizes<Size> for crate::hardlink::HardlinkAware<Size>
198191
where
199192
DataTree<OsStringDisplay, Size>: Send,
200193
Size: size::Size + Sync,
201194
{
202-
type HardlinkRecord = &'a crate::hardlink::HardlinkList<Size>;
203-
type DeduplicationReport = &'a crate::hardlink::HardlinkList<Size>;
195+
type DeduplicationReport = crate::hardlink::HardlinkList<Size>;
204196

205197
fn deduplicate_hardlink_sizes(
198+
self,
206199
data_tree: &mut DataTree<OsStringDisplay, Size>,
207-
record: Self::HardlinkRecord,
208200
) -> Result<Self::DeduplicationReport, RuntimeError> {
209201
use crate::hardlink::LinkPathList;
210202
use std::path::Path;
203+
let record: Self::DeduplicationReport = self.into();
211204
let hardlink_info: Box<[(Size, LinkPathList)]> = record
212205
.iter()
213206
.map(|values| (*values.size(), values.links().clone()))
@@ -248,11 +241,7 @@ where
248241
fn reflect_deduplication_results(
249242
report: Self::DeduplicationReport,
250243
) -> Result<Option<HardlinkListReflection<Size>>, RuntimeError> {
251-
if report.is_empty() {
252-
Ok(None)
253-
} else {
254-
report.clone().into_reflection().pipe(Some).pipe(Ok)
255-
}
244+
report.into_reflection().pipe(Some).pipe(Ok)
256245
}
257246
}
258247

@@ -261,12 +250,11 @@ where
261250
DataTree<OsStringDisplay, Size>: Send,
262251
Size: size::Size + Sync,
263252
{
264-
type HardlinkRecord = ();
265253
type DeduplicationReport = ();
266254

267255
fn deduplicate_hardlink_sizes(
256+
self,
268257
_: &mut DataTree<OsStringDisplay, Size>,
269-
_: Self::HardlinkRecord,
270258
) -> Result<Self::DeduplicationReport, RuntimeError> {
271259
Ok(())
272260
}

src/fs_tree_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::{
2929
/// };
3030
/// let builder = FsTreeBuilder {
3131
/// root: std::env::current_dir().unwrap(),
32-
/// hardlinks_recorder: HardlinkIgnorant,
32+
/// hardlinks_recorder: &HardlinkIgnorant,
3333
/// size_getter: GetApparentSize,
3434
/// reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT),
3535
/// max_depth: 10,
@@ -42,14 +42,14 @@ where
4242
Report: Reporter<Size> + Sync + ?Sized,
4343
Size: size::Size + Send + Sync,
4444
SizeGetter: GetSize<Size = Size> + Sync,
45-
HardlinksRecorder: RecordHardlinks<Size, Report> + Sync,
45+
HardlinksRecorder: RecordHardlinks<Size, Report> + Sync + ?Sized,
4646
{
4747
/// Root of the directory tree.
4848
pub root: PathBuf,
4949
/// Returns size of an item.
5050
pub size_getter: SizeGetter,
5151
/// Handle to detect and record hardlinks.
52-
pub hardlinks_recorder: HardlinksRecorder,
52+
pub hardlinks_recorder: &'a HardlinksRecorder,
5353
/// Reports progress to external system.
5454
pub reporter: &'a Report,
5555
/// Deepest level of descendent display in the graph. The sizes beyond the max depth still count toward total.
@@ -63,7 +63,7 @@ where
6363
Report: Reporter<Size> + Sync + ?Sized,
6464
Size: size::Size + Send + Sync,
6565
SizeGetter: GetSize<Size = Size> + Sync,
66-
HardlinksRecorder: RecordHardlinks<Size, Report> + Sync,
66+
HardlinksRecorder: RecordHardlinks<Size, Report> + Sync + ?Sized,
6767
{
6868
/// Create a [`DataTree`] from an [`FsTreeBuilder`].
6969
fn from(builder: FsTreeBuilder<Size, SizeGetter, HardlinksRecorder, Report>) -> Self {

src/hardlink/aware.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,35 @@ use crate::{
55
reporter::{event::HardlinkDetection, Event, Reporter},
66
size,
77
};
8+
use derive_more::{AsMut, AsRef, From, Into};
9+
use pipe_trait::Pipe;
10+
use smart_default::SmartDefault;
811
use std::{fmt::Debug, os::unix::fs::MetadataExt};
912

1013
/// Be aware of hardlinks. Treat them as links that share space.
1114
/// Detect files with more than 1 links and record them.
1215
/// Deduplicate them (remove duplicated size) from total size to
1316
/// accurately reflect the real size of their containers.
14-
#[derive(Debug, Clone, Copy)]
15-
pub struct Aware<'a, Size> {
17+
#[derive(Debug, SmartDefault, Clone, AsRef, AsMut, From, Into)]
18+
pub struct Aware<Size> {
1619
/// Map an inode number to its size and detected paths.
17-
record: &'a HardlinkList<Size>,
20+
record: HardlinkList<Size>,
1821
}
1922

2023
pub use Aware as HardlinkAware;
2124

22-
impl<'a, Size> Aware<'a, Size> {
25+
impl<Size> Aware<Size> {
26+
pub fn new() -> Self {
27+
HardlinkList::default().pipe(Aware::from)
28+
}
29+
2330
/// Create a detector/recorder of hardlinks.
24-
pub fn new(record: &'a HardlinkList<Size>) -> Self {
25-
Aware { record }
31+
pub fn from_record(record: HardlinkList<Size>) -> Self {
32+
Aware::from(record)
2633
}
2734
}
2835

29-
impl<'a, Size, Report> RecordHardlinks<Size, Report> for Aware<'a, Size>
36+
impl<Size, Report> RecordHardlinks<Size, Report> for Aware<Size>
3037
where
3138
Size: size::Size + Eq + Debug,
3239
Report: Reporter<Size> + ?Sized,

src/hardlink/record.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ pub trait RecordHardlinks<Size, Reporter: ?Sized> {
3434
}
3535

3636
/// Do detect and record hardlinks.
37-
pub type Do<'a, Size> = super::HardlinkAware<'a, Size>;
37+
pub type Do<Size> = super::HardlinkAware<Size>;
3838
/// Do not detect nor record hardlinks.
3939
pub type DoNot = super::HardlinkIgnorant;

tests/_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ where
172172
let measure = |suffix: &str| {
173173
FsTreeBuilder {
174174
size_getter,
175-
hardlinks_recorder: HardlinkIgnorant,
175+
hardlinks_recorder: &HardlinkIgnorant,
176176
reporter: &ErrorOnlyReporter::new(|error| {
177177
panic!("Unexpected call to report_error: {error:?}")
178178
}),

tests/cli_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn fs_errors() {
132132
let builder = FsTreeBuilder {
133133
root: workspace.to_path_buf(),
134134
size_getter: GetApparentSize,
135-
hardlinks_recorder: HardlinkIgnorant,
135+
hardlinks_recorder: &HardlinkIgnorant,
136136
reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT),
137137
max_depth: 10,
138138
};

tests/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn json_output() {
8383
let builder = FsTreeBuilder {
8484
root: workspace.to_path_buf(),
8585
size_getter: GetApparentSize,
86-
hardlinks_recorder: HardlinkIgnorant,
86+
hardlinks_recorder: &HardlinkIgnorant,
8787
reporter: &ErrorOnlyReporter::new(ErrorReport::SILENT),
8888
max_depth: 10,
8989
};

0 commit comments

Comments
 (0)