parallel_disk_usage/
size.rs1use super::bytes_format::{self, BytesFormat};
2use derive_more::{Add, AddAssign, From, Into, Sum};
3use std::{
4 fmt::{Debug, Display},
5 iter::Sum,
6 ops::{Add, AddAssign, Mul, MulAssign},
7};
8
9#[cfg(feature = "json")]
10use serde::{Deserialize, Serialize};
11
12pub trait Size:
14 Debug
15 + Default
16 + Clone
17 + Copy
18 + PartialEq
19 + Eq
20 + PartialOrd
21 + Ord
22 + Add<Output = Self>
23 + AddAssign
24 + Sum
25{
26 type Inner: From<Self> + Into<Self> + Mul<Self, Output = Self>;
28 type DisplayFormat: Copy;
30 type DisplayOutput: Display;
32 fn display(self, input: Self::DisplayFormat) -> Self::DisplayOutput;
34}
35
36macro_rules! newtype {
37 (
38 $(#[$attribute:meta])*
39 $name:ident = $inner:ty;
40 display: ($display_format:ty) -> $display_output:ty = $display_impl:expr;
41 ) => {
42 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
43 #[derive(From, Into, Add, AddAssign, Sum)]
44 #[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
45 $(#[$attribute])*
46 pub struct $name($inner);
47
48 impl $name {
49 pub const fn new(inner: $inner) -> Self {
50 $name(inner)
51 }
52
53 pub const fn inner(self) -> $inner {
54 self.0
55 }
56 }
57
58 impl Size for $name {
59 type Inner = $inner;
60 type DisplayFormat = $display_format;
61 type DisplayOutput = $display_output;
62 fn display(self, format: Self::DisplayFormat) -> Self::DisplayOutput {
63 let display: fn(Self, Self::DisplayFormat) -> Self::DisplayOutput = $display_impl;
64 display(self, format)
65 }
66 }
67
68 impl Mul<$inner> for $name {
69 type Output = Self;
70 fn mul(self, rhs: $inner) -> Self::Output {
71 self.0.mul(rhs).into()
72 }
73 }
74
75 impl Mul<$name> for $inner {
76 type Output = $name;
77 fn mul(self, rhs: $name) -> Self::Output {
78 rhs * self
79 }
80 }
81
82 impl MulAssign<$inner> for $name {
83 fn mul_assign(&mut self, rhs: $inner) {
84 self.0 *= rhs;
85 }
86 }
87 };
88}
89
90newtype!(
91 Bytes = u64;
93 display: (BytesFormat) -> bytes_format::Output = |bytes, format| {
94 format.format(bytes.into())
95 };
96);
97
98newtype!(
99 Blocks = u64;
101 display: (()) -> u64 = |blocks, ()| blocks.inner();
102);