Skip to content

Commit e1c81dc

Browse files
committed
feat(api): stop relying on Into
1 parent 7ef427d commit e1c81dc

1 file changed

Lines changed: 54 additions & 19 deletions

File tree

src/hardlink/hardlink_list/summary.rs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ pub struct Summary<Size> {
4646

4747
/// Ability to summarize into a [`Summary`].
4848
pub trait SummarizeHardlinks<Size>: Sized {
49+
/// The result of [`SummarizeHardlinks::summarize_hardlinks`].
50+
type Summary;
4951
/// Summarize into a summary of shared links and size.
50-
fn summarize_hardlinks(self) -> Summary<Size>;
52+
fn summarize_hardlinks(self) -> Self::Summary;
5153
}
5254

5355
/// Summary of a single unique file.
@@ -65,12 +67,14 @@ impl<Size, Iter> SummarizeHardlinks<Size> for Iter
6567
where
6668
Size: size::Size,
6769
Iter: IntoIterator,
68-
Iter::Item: Into<SingleInodeSummary<Size>>,
70+
Iter::Item: SummarizeHardlinks<Size>,
71+
<Iter::Item as SummarizeHardlinks<Size>>::Summary: Into<SingleInodeSummary<Size>>,
6972
{
70-
fn summarize_hardlinks(self) -> Summary<Size> {
73+
type Summary = Summary<Size>;
74+
fn summarize_hardlinks(self) -> Self::Summary {
7175
let mut summary = Summary::default();
7276
for item in self {
73-
let SingleInodeSummary { links, paths, size } = item.into();
77+
let SingleInodeSummary { links, paths, size } = item.summarize_hardlinks().into();
7478
summary.inodes += 1;
7579
summary.all_links += links;
7680
summary.detected_links += paths;
@@ -95,7 +99,8 @@ where
9599
impl<Size, Item> FromIterator<Item> for Summary<Size>
96100
where
97101
Size: size::Size,
98-
Item: Into<SingleInodeSummary<Size>>,
102+
Item: SummarizeHardlinks<Size>,
103+
Item::Summary: Into<SingleInodeSummary<Size>>,
99104
{
100105
/// Create a summary of shared links and size from an iterator.
101106
fn from_iter<Iter: IntoIterator<Item = Item>>(iter: Iter) -> Self {
@@ -111,7 +116,8 @@ impl<Size: size::Size> HardlinkList<Size> {
111116
}
112117

113118
impl<Size: size::Size> SummarizeHardlinks<Size> for &HardlinkList<Size> {
114-
fn summarize_hardlinks(self) -> Summary<Size> {
119+
type Summary = Summary<Size>;
120+
fn summarize_hardlinks(self) -> Self::Summary {
115121
self.summarize()
116122
}
117123
}
@@ -124,7 +130,8 @@ impl<Size: size::Size> Reflection<Size> {
124130
}
125131

126132
impl<Size: size::Size> SummarizeHardlinks<Size> for &Reflection<Size> {
127-
fn summarize_hardlinks(self) -> Summary<Size> {
133+
type Summary = Summary<Size>;
134+
fn summarize_hardlinks(self) -> Self::Summary {
128135
self.summarize()
129136
}
130137
}
@@ -190,34 +197,62 @@ impl<Size: size::Size> Summary<Size> {
190197
}
191198
}
192199

200+
impl<Size: Copy> SummarizeHardlinks<Size> for ReflectionEntry<Size> {
201+
type Summary = SingleInodeSummary<Size>;
202+
fn summarize_hardlinks(self) -> Self::Summary {
203+
(&self).summarize_hardlinks()
204+
}
205+
}
206+
193207
impl<Size: Copy> From<ReflectionEntry<Size>> for SingleInodeSummary<Size> {
194208
fn from(reflection: ReflectionEntry<Size>) -> Self {
195-
(&reflection).into()
209+
reflection.summarize_hardlinks()
196210
}
197211
}
198212

199-
impl<'r, Size: Copy> From<&'r ReflectionEntry<Size>> for SingleInodeSummary<Size> {
200-
fn from(reflection: &'r ReflectionEntry<Size>) -> Self {
213+
impl<Size: Copy> SummarizeHardlinks<Size> for &ReflectionEntry<Size> {
214+
type Summary = SingleInodeSummary<Size>;
215+
fn summarize_hardlinks(self) -> Self::Summary {
201216
SingleInodeSummary {
202-
links: reflection.links,
203-
paths: reflection.paths.len(),
204-
size: reflection.size,
217+
links: self.links,
218+
paths: self.paths.len(),
219+
size: self.size,
205220
}
206221
}
207222
}
208223

224+
impl<'r, Size: Copy> From<&'r ReflectionEntry<Size>> for SingleInodeSummary<Size> {
225+
fn from(reflection: &'r ReflectionEntry<Size>) -> Self {
226+
reflection.summarize_hardlinks()
227+
}
228+
}
229+
230+
impl<'a, Size: Copy> SummarizeHardlinks<Size> for IterItem<'a, Size> {
231+
type Summary = SingleInodeSummary<Size>;
232+
fn summarize_hardlinks(self) -> Self::Summary {
233+
(&self).summarize_hardlinks()
234+
}
235+
}
236+
209237
impl<'a, Size: Copy> From<IterItem<'a, Size>> for SingleInodeSummary<Size> {
210238
fn from(value: IterItem<'a, Size>) -> Self {
211-
(&value).into()
239+
value.summarize_hardlinks()
212240
}
213241
}
214242

215-
impl<'r, 'a, Size: Copy> From<&'r IterItem<'a, Size>> for SingleInodeSummary<Size> {
216-
fn from(value: &'r IterItem<'a, Size>) -> Self {
243+
impl<'a, Size: Copy> SummarizeHardlinks<Size> for &IterItem<'a, Size> {
244+
type Summary = SingleInodeSummary<Size>;
245+
fn summarize_hardlinks(self) -> Self::Summary {
217246
SingleInodeSummary {
218-
links: value.links(),
219-
paths: value.paths().len(),
220-
size: *value.size(),
247+
links: self.links(),
248+
paths: self.paths().len(),
249+
size: *self.size(),
221250
}
222251
}
223252
}
253+
254+
impl<'r, 'a, Size: Copy> From<&'r IterItem<'a, Size>> for SingleInodeSummary<Size> {
255+
fn from(value: &'r IterItem<'a, Size>) -> Self {
256+
value.summarize_hardlinks()
257+
}
258+
}

0 commit comments

Comments
 (0)