File_system/Fundamentals/
Size.rs

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
128
129
130
use core::{
    fmt::{self, Display, Formatter},
    ops::{Add, AddAssign},
};

/// Size type
///
/// This type is used to represent the size of data which can be hold by a file system.
/// Since the size of a file system can be very large, this type is a 64-bit unsigned integer.
///
/// # Examples
///
/// ```rust
/// use File_system::Size_type;
///
/// let Size = Size_type::New(0);
/// ```
#[derive(Default, PartialOrd, PartialEq, Eq, Ord, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Size_type(u64);

impl Display for Size_type {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Size_type {
    pub const fn New(Item: u64) -> Self {
        Size_type(Item)
    }

    pub const fn As_u64(&self) -> u64 {
        self.0
    }
}

impl PartialEq<usize> for Size_type {
    fn eq(&self, other: &usize) -> bool {
        self.0 == *other as u64
    }
}

impl From<usize> for Size_type {
    fn from(item: usize) -> Self {
        Size_type(item as u64)
    }
}

impl From<u64> for Size_type {
    fn from(item: u64) -> Self {
        Size_type(item)
    }
}

impl From<Size_type> for usize {
    fn from(item: Size_type) -> Self {
        item.0 as usize
    }
}

impl From<Size_type> for u64 {
    fn from(item: Size_type) -> Self {
        item.0
    }
}

impl Add<Size_type> for Size_type {
    type Output = Size_type;

    fn add(self, rhs: Size_type) -> Self::Output {
        Size_type(self.0 + rhs.0)
    }
}

impl Add<usize> for Size_type {
    type Output = Size_type;

    fn add(self, rhs: usize) -> Self::Output {
        Size_type(self.0 + rhs as u64)
    }
}

impl Add<u64> for Size_type {
    type Output = Size_type;

    fn add(self, rhs: u64) -> Self::Output {
        Size_type(self.0 + rhs)
    }
}

impl Add<Size_type> for usize {
    type Output = Size_type;

    fn add(self, rhs: Size_type) -> Self::Output {
        Size_type(self as u64 + rhs.0)
    }
}

impl Add<Size_type> for u64 {
    type Output = Size_type;

    fn add(self, rhs: Size_type) -> Self::Output {
        Size_type(self + rhs.0)
    }
}

impl AddAssign<Size_type> for Size_type {
    fn add_assign(&mut self, rhs: Size_type) {
        self.0 += rhs.0;
    }
}

impl AddAssign<usize> for Size_type {
    fn add_assign(&mut self, rhs: usize) {
        self.0 += rhs as u64;
    }
}

impl AddAssign<u64> for Size_type {
    fn add_assign(&mut self, rhs: u64) {
        self.0 += rhs;
    }
}

impl AddAssign<Size_type> for usize {
    fn add_assign(&mut self, rhs: Size_type) {
        *self += rhs.0 as usize;
    }
}