-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsize.rs
More file actions
127 lines (113 loc) · 3.46 KB
/
size.rs
File metadata and controls
127 lines (113 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use super::bytes_format::{self, BytesFormat};
use derive_more::{Add, AddAssign, From, Into, Sub, SubAssign, Sum};
use std::{
fmt::{Debug, Display},
iter::Sum,
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
};
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
mod mul_traits {
use std::ops::{Mul, MulAssign};
pub trait MulAssignEx<Rhs>: Mul<Rhs, Output = Self> + MulAssign<Rhs> + Sized {}
impl<Lhs: Mul<Rhs, Output = Lhs> + MulAssign<Rhs>, Rhs> MulAssignEx<Rhs> for Lhs {}
}
use mul_traits::MulAssignEx;
/// Types whose values can be used as disk usage statistic.
pub trait Size:
Debug
+ Default
+ Clone
+ Copy
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Add<Output = Self>
+ AddAssign
+ Sub<Output = Self>
+ SubAssign
+ Sum
+ MulAssignEx<u8>
+ MulAssignEx<u16>
+ MulAssignEx<u32>
+ MulAssignEx<u64>
{
/// Underlying type
type Inner: From<Self> + Into<Self> + Mul<Self, Output = Self>;
/// Format to be used to [`display`](Size::display) the value.
type DisplayFormat: Copy;
/// Return type of [`display`](Size::display).
type DisplayOutput: Display;
/// Display the disk usage in a measurement system.
fn display(self, input: Self::DisplayFormat) -> Self::DisplayOutput;
}
macro_rules! impl_mul {
($name:ident: $inner:ident *= $($num_type:ident)+) => {
$(
impl Mul<$num_type> for $name {
type Output = Self;
fn mul(self, rhs: $num_type) -> Self::Output {
self.0.mul(rhs as $inner).into()
}
}
impl Mul<$name> for $num_type {
type Output = $name;
fn mul(self, rhs: $name) -> Self::Output {
rhs * self
}
}
impl MulAssign<$num_type> for $name {
fn mul_assign(&mut self, rhs: $num_type) {
self.0 *= rhs as $inner;
}
}
)+
};
($name:ident: u64) => {
impl_mul!($name: u64 *= u8 u16 u32 u64);
};
}
macro_rules! newtype {
(
$(#[$attribute:meta])*
$name:ident = $inner:ty;
display: ($display_format:ty) -> $display_output:ty = $display_impl:expr;
) => {
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(From, Into, Add, AddAssign, Sub, SubAssign, Sum)]
#[cfg_attr(feature = "json", derive(Deserialize, Serialize))]
$(#[$attribute])*
pub struct $name($inner);
impl $name {
pub const fn new(inner: $inner) -> Self {
$name(inner)
}
pub const fn inner(self) -> $inner {
self.0
}
}
impl Size for $name {
type Inner = $inner;
type DisplayFormat = $display_format;
type DisplayOutput = $display_output;
fn display(self, format: Self::DisplayFormat) -> Self::DisplayOutput {
let display: fn(Self, Self::DisplayFormat) -> Self::DisplayOutput = $display_impl;
display(self, format)
}
}
impl_mul!($name: u64);
};
}
newtype!(
/// Number of bytes.
Bytes = u64;
display: (BytesFormat) -> bytes_format::Output = |bytes, format| {
format.format(bytes.into())
};
);
newtype!(
/// Number of blocks.
Blocks = u64;
display: (()) -> u64 = |blocks, ()| blocks.inner();
);