Skip to content

Commit 8f79037

Browse files
committed
feat(api): move instead of mutate
1 parent d3656d5 commit 8f79037

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/app/sub.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,12 @@ where
129129
if !no_sort {
130130
data_tree.par_sort_by(|left, right| left.size().cmp(&right.size()).reverse());
131131
}
132-
let deduplication_record = hardlinks_handler.deduplicate(&mut data_tree);
133-
(data_tree, deduplication_record)
132+
133+
// errors caused by failing deduplication shouldn't prevent data_tree from being visualized
134+
match hardlinks_handler.deduplicate(data_tree) {
135+
Ok((data_tree, record)) => (data_tree, Ok(record)),
136+
Err((data_tree, error)) => (data_tree, Err(error)),
137+
}
134138
};
135139

136140
GLOBAL_STATUS_BOARD.clear_line(0);

src/hardlink/aware.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
hardlink_list, DeduplicateSharedSize, HardlinkList, LinkPathList, RecordHardlinks,
2+
deduplicate, hardlink_list, DeduplicateSharedSize, HardlinkList, LinkPathList, RecordHardlinks,
33
RecordHardlinksArgument,
44
};
55
use crate::{
@@ -97,8 +97,8 @@ where
9797
type Error = Infallible;
9898
fn deduplicate(
9999
self,
100-
data_tree: &mut DataTree<OsStringDisplay, Size>,
101-
) -> Result<Self::Report, Self::Error> {
100+
mut data_tree: DataTree<OsStringDisplay, Size>,
101+
) -> deduplicate::Result<Size, Self::Report, Self::Error> {
102102
let record: Self::Report = self.into();
103103
let hardlink_info: Box<[(Size, LinkPathList)]> = record
104104
.iter()
@@ -109,6 +109,6 @@ where
109109
.map(|(size, paths)| (*size, paths.iter().map(AsRef::as_ref).collect()))
110110
.collect();
111111
data_tree.par_deduplicate_hardlinks(&hardlink_info);
112-
Ok(record)
112+
Ok((data_tree, record))
113113
}
114114
}

src/hardlink/deduplicate.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use crate::{data_tree::DataTree, os_string_display::OsStringDisplay, size};
22

3-
// TODO:
4-
// Consider changing `deduplicate` into taking owned `DataTree` and returning `Result<(DataTree, Self::Report), Self::Error>`.
5-
63
// TODO:
74
// Consider changing `deduplicate` into one that transforms `HardlinkDuplicated<DataTree>` into `DataTree`.
85
// `HardlinkDuplicated` (name is non-final) cannot be constructed manually and is the only type accepted by `deduplicate`.
96

7+
type DataTreeTuple<Size, Other> = (DataTree<OsStringDisplay, Size>, Other);
8+
9+
/// Result type of [`DeduplicateSharedSize::deduplicate`].
10+
pub type Result<Size, Report, Error> =
11+
std::result::Result<DataTreeTuple<Size, Report>, DataTreeTuple<Size, Error>>;
12+
1013
/// Ability to correct the sizes in a [`DataTree`] by reducing the size of recorded shared links.
1114
///
1215
/// The input tree is assumed to be not yet deduplicated.
@@ -18,8 +21,8 @@ pub trait DeduplicateSharedSize<Size: size::Size>: Sized {
1821
/// Correct the sizes in a [`DataTree`] by reducing the size of recorded shared links.
1922
fn deduplicate(
2023
self,
21-
data_tree: &mut DataTree<OsStringDisplay, Size>,
22-
) -> Result<Self::Report, Self::Error>;
24+
data_tree: DataTree<OsStringDisplay, Size>,
25+
) -> Result<Size, Self::Report, Self::Error>;
2326
}
2427

2528
/// Do deduplicate the sizes of hardlinks.

src/hardlink/ignorant.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{DeduplicateSharedSize, RecordHardlinks, RecordHardlinksArgument};
1+
use super::{deduplicate, DeduplicateSharedSize, RecordHardlinks, RecordHardlinksArgument};
22
use crate::{data_tree::DataTree, os_string_display::OsStringDisplay, size};
33
use std::convert::Infallible;
44

@@ -37,8 +37,8 @@ where
3737
/// Do nothing.
3838
fn deduplicate(
3939
self,
40-
_: &mut DataTree<OsStringDisplay, Size>,
41-
) -> Result<Self::Report, Self::Error> {
42-
Ok(())
40+
data_tree: DataTree<OsStringDisplay, Size>,
41+
) -> deduplicate::Result<Size, Self::Report, Self::Error> {
42+
Ok((data_tree, ()))
4343
}
4444
}

0 commit comments

Comments
 (0)