guse/
inodes.rs

1use std::{fmt::Display, ops::Deref};
2
3use crate::fs::VDIR_BIT;
4
5#[derive(Debug, Clone, Copy, Hash, PartialEq)]
6pub enum Inodes {
7    NormalIno(u64),
8    VirtualIno(u64),
9}
10
11#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
12pub struct NormalIno(u64);
13
14impl NormalIno {
15    #[inline]
16    pub fn to_virt(&self) -> VirtualIno {
17        VirtualIno(self | VDIR_BIT)
18    }
19
20    #[inline]
21    pub fn to_virt_u64(&self) -> u64 {
22        self | VDIR_BIT
23    }
24
25    #[inline]
26    pub fn to_norm_u64(&self) -> u64 {
27        self.0
28    }
29}
30
31#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
32pub struct VirtualIno(u64);
33
34impl VirtualIno {
35    #[inline]
36    #[must_use]
37    pub fn to_norm(self) -> NormalIno {
38        NormalIno(self.0 & !VDIR_BIT)
39    }
40
41    #[inline]
42    #[must_use]
43    pub fn to_norm_u64(&self) -> u64 {
44        self.0 & !VDIR_BIT
45    }
46
47    #[inline]
48    #[must_use]
49    pub fn to_virt_u64(&self) -> u64 {
50        self.0
51    }
52}
53
54impl Inodes {
55    #[inline]
56    #[must_use]
57    pub fn to_norm(self) -> NormalIno {
58        match self {
59            Inodes::NormalIno(ino) => NormalIno(ino),
60            Inodes::VirtualIno(ino) => {
61                let ino = ino & !VDIR_BIT;
62                NormalIno(ino)
63            }
64        }
65    }
66
67    #[inline]
68    #[must_use]
69    pub fn to_virt(self) -> VirtualIno {
70        match self {
71            Inodes::NormalIno(ino) => {
72                let ino = ino | VDIR_BIT;
73                VirtualIno(ino)
74            }
75            Inodes::VirtualIno(ino) => VirtualIno(ino),
76        }
77    }
78
79    #[inline]
80    #[must_use]
81    pub fn to_u64_n(self) -> u64 {
82        match self {
83            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino & !VDIR_BIT,
84        }
85    }
86
87    #[inline]
88    #[must_use]
89    pub fn to_u64_v(self) -> u64 {
90        match self {
91            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino | VDIR_BIT,
92        }
93    }
94}
95
96impl From<u64> for Inodes {
97    fn from(value: u64) -> Self {
98        if (value & VDIR_BIT) != 0 {
99            Inodes::VirtualIno(value)
100        } else {
101            Inodes::NormalIno(value)
102        }
103    }
104}
105
106impl From<u64> for NormalIno {
107    fn from(value: u64) -> Self {
108        Self(value & !VDIR_BIT)
109    }
110}
111
112impl From<u64> for VirtualIno {
113    fn from(value: u64) -> Self {
114        Self(value | VDIR_BIT)
115    }
116}
117
118impl From<NormalIno> for u64 {
119    fn from(n: NormalIno) -> Self {
120        n.0
121    }
122}
123
124impl From<VirtualIno> for u64 {
125    fn from(v: VirtualIno) -> Self {
126        v.0
127    }
128}
129
130impl From<Inodes> for u64 {
131    fn from(i: Inodes) -> Self {
132        match i {
133            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino,
134        }
135    }
136}
137
138impl From<&Inodes> for u64 {
139    fn from(i: &Inodes) -> Self {
140        match *i {
141            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino,
142        }
143    }
144}
145
146impl AsRef<u64> for Inodes {
147    fn as_ref(&self) -> &u64 {
148        match self {
149            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino,
150        }
151    }
152}
153
154impl std::ops::BitAnd<u64> for &Inodes {
155    type Output = u64;
156    fn bitand(self, rhs: u64) -> Self::Output {
157        u64::from(self) & rhs
158    }
159}
160
161impl std::ops::BitOr<u64> for &NormalIno {
162    type Output = u64;
163    fn bitor(self, rhs: u64) -> Self::Output {
164        self.0 | rhs
165    }
166}
167
168impl Deref for Inodes {
169    type Target = u64;
170    fn deref(&self) -> &Self::Target {
171        match self {
172            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino,
173        }
174    }
175}
176
177impl Display for Inodes {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        match self {
180            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => write!(f, "{ino}"),
181        }
182    }
183}
184
185impl Display for NormalIno {
186    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187        write!(f, "{}", self.0)
188    }
189}
190
191impl Display for VirtualIno {
192    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193        write!(f, "{}", self.0)
194    }
195}
196
197impl PartialEq<u64> for Inodes {
198    fn eq(&self, other: &u64) -> bool {
199        match self {
200            Inodes::NormalIno(ino) | Inodes::VirtualIno(ino) => ino == other,
201        }
202    }
203}
204
205impl PartialOrd for VirtualIno {
206    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
207        Some(self.cmp(other))
208    }
209}
210
211impl Ord for VirtualIno {
212    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
213        self.0.cmp(&other.0)
214    }
215}