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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#![allow(cast_lossless)]
use consts::SECTOR_SIZE;
use std::borrow::Borrow;
use std::fmt;
use std::iter::Sum;
use std::ops::{Add, Deref, Div, Mul, Rem};
use serde;
use super::deviceinfo::{DM_NAME_LEN, DM_UUID_LEN};
use super::errors::ErrorKind;
use super::result::{DmError, DmResult};
const META_BLOCK_SIZE: Sectors = Sectors(8);
#[allow(dead_code)]
const MAX_META_DEV_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));
macro_rules! self_div {
($T:ident) => {
impl Div<$T> for $T {
type Output = u64;
fn div(self, rhs: $T) -> u64 {
self.0 / *rhs
}
}
};
}
macro_rules! debug {
($T:ident) => {
impl fmt::Debug for $T {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, stringify!($T))?;
write!(f, "({})", **self)
}
}
};
}
macro_rules! serde {
($T:ident) => {
impl serde::Serialize for $T {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_u64(**self)
}
}
impl<'de> serde::Deserialize<'de> for $T {
fn deserialize<D>(deserializer: D) -> Result<$T, D::Error>
where
D: serde::de::Deserializer<'de>,
{
Ok($T(serde::Deserialize::deserialize(deserializer)?))
}
}
};
}
macro_rules! sum {
($T:ident) => {
impl Sum for $T {
fn sum<I: Iterator<Item = $T>>(iter: I) -> $T {
iter.fold($T::default(), Add::add)
}
}
};
}
macro_rules! unsigned_div {
($t:ty, $T:ident) => {
impl Div<$t> for $T {
type Output = $T;
fn div(self, rhs: $t) -> $T {
$T(self.0 / rhs as u64)
}
}
};
}
macro_rules! unsigned_mul {
($t:ty, $T:ident) => {
impl Mul<$t> for $T {
type Output = $T;
fn mul(self, rhs: $t) -> $T {
$T(self.0 * rhs as u64)
}
}
impl Mul<$T> for $t {
type Output = $T;
fn mul(self, rhs: $T) -> $T {
$T(self as u64 * rhs.0)
}
}
};
}
macro_rules! unsigned_rem {
($t:ty, $T:ident) => {
impl Rem<$t> for $T {
type Output = $T;
fn rem(self, rhs: $t) -> $T {
$T(self.0 % rhs as u64)
}
}
};
}
macro_rules! rem {
($T:ident) => {
impl Rem<$T> for $T {
type Output = $T;
fn rem(self, rhs: $T) -> $T {
$T(self.0 % rhs.0 as u64)
}
}
};
}
macro_rules! checked_add {
($T: ident) => {
pub fn checked_add(&self, other: $T) -> Option<$T> {
self.0.checked_add(other.0).map($T)
}
}
}
macro_attr! {
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct DataBlocks(pub u64);
}
NewtypeAdd! { () pub struct DataBlocks(u64); }
NewtypeAddAssign! { () pub struct DataBlocks(u64); }
NewtypeDeref! { () pub struct DataBlocks(u64); }
NewtypeFrom! { () pub struct DataBlocks(u64); }
NewtypeSub! { () pub struct DataBlocks(u64); }
NewtypeSubAssign! { () pub struct DataBlocks(u64); }
self_div!(DataBlocks);
serde!(DataBlocks);
debug!(DataBlocks);
unsigned_div!(u64, DataBlocks);
unsigned_div!(u32, DataBlocks);
unsigned_div!(u16, DataBlocks);
unsigned_div!(u8, DataBlocks);
unsigned_div!(usize, DataBlocks);
unsigned_mul!(u64, DataBlocks);
unsigned_mul!(u32, DataBlocks);
unsigned_mul!(u16, DataBlocks);
unsigned_mul!(u8, DataBlocks);
unsigned_mul!(usize, DataBlocks);
impl fmt::Display for DataBlocks {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} data blocks", self.0)
}
}
macro_attr! {
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct MetaBlocks(pub u64);
}
impl MetaBlocks {
pub fn sectors(self) -> Sectors {
self.0 * META_BLOCK_SIZE
}
}
NewtypeAdd! { () pub struct MetaBlocks(u64); }
NewtypeAddAssign! { () pub struct MetaBlocks(u64); }
NewtypeDeref! { () pub struct MetaBlocks(u64); }
NewtypeFrom! { () pub struct MetaBlocks(u64); }
NewtypeSub! { () pub struct MetaBlocks(u64); }
NewtypeSubAssign! { () pub struct MetaBlocks(u64); }
self_div!(MetaBlocks);
serde!(MetaBlocks);
debug!(MetaBlocks);
unsigned_div!(u64, MetaBlocks);
unsigned_div!(u32, MetaBlocks);
unsigned_div!(u16, MetaBlocks);
unsigned_div!(u8, MetaBlocks);
unsigned_div!(usize, MetaBlocks);
unsigned_mul!(u64, MetaBlocks);
unsigned_mul!(u32, MetaBlocks);
unsigned_mul!(u16, MetaBlocks);
unsigned_mul!(u8, MetaBlocks);
unsigned_mul!(usize, MetaBlocks);
impl fmt::Display for MetaBlocks {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} meta blocks", self.0)
}
}
macro_attr! {
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Bytes(pub u64);
}
impl Bytes {
pub fn sectors(self) -> Sectors {
Sectors(self.0 / SECTOR_SIZE as u64)
}
checked_add!(Bytes);
}
NewtypeAdd! { () pub struct Bytes(u64); }
NewtypeAddAssign! { () pub struct Bytes(u64); }
NewtypeDeref! { () pub struct Bytes(u64); }
NewtypeFrom! { () pub struct Bytes(u64); }
NewtypeSub! { () pub struct Bytes(u64); }
NewtypeSubAssign! { () pub struct Bytes(u64); }
self_div!(Bytes);
serde!(Bytes);
debug!(Bytes);
sum!(Bytes);
unsigned_div!(u64, Bytes);
unsigned_div!(u32, Bytes);
unsigned_div!(u16, Bytes);
unsigned_div!(u8, Bytes);
unsigned_div!(usize, Bytes);
unsigned_mul!(u64, Bytes);
unsigned_mul!(u32, Bytes);
unsigned_mul!(u16, Bytes);
unsigned_mul!(u8, Bytes);
unsigned_mul!(usize, Bytes);
impl fmt::Display for Bytes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} bytes", self.0)
}
}
macro_attr! {
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Sectors(pub u64);
}
impl Sectors {
pub fn bytes(self) -> Bytes {
Bytes(self.0 * SECTOR_SIZE as u64)
}
pub fn metablocks(self) -> MetaBlocks {
MetaBlocks(self / META_BLOCK_SIZE)
}
checked_add!(Sectors);
}
NewtypeAdd! { () pub struct Sectors(u64); }
NewtypeAddAssign! { () pub struct Sectors(u64); }
NewtypeDeref! { () pub struct Sectors(u64); }
NewtypeFrom! { () pub struct Sectors(u64); }
NewtypeSub! { () pub struct Sectors(u64); }
NewtypeSubAssign! { () pub struct Sectors(u64); }
self_div!(Sectors);
serde!(Sectors);
debug!(Sectors);
sum!(Sectors);
unsigned_div!(u64, Sectors);
unsigned_div!(u32, Sectors);
unsigned_div!(u16, Sectors);
unsigned_div!(u8, Sectors);
unsigned_div!(usize, Sectors);
unsigned_mul!(u64, Sectors);
unsigned_mul!(u32, Sectors);
unsigned_mul!(u16, Sectors);
unsigned_mul!(u8, Sectors);
unsigned_mul!(usize, Sectors);
unsigned_rem!(u64, Sectors);
unsigned_rem!(u32, Sectors);
unsigned_rem!(u16, Sectors);
unsigned_rem!(u8, Sectors);
unsigned_rem!(usize, Sectors);
rem!(Sectors);
impl fmt::Display for Sectors {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} sectors", self.0)
}
}
fn str_check(value: &str, max_allowed_chars: usize) -> DmResult<()> {
if !value.is_ascii() {
let err_msg = format!("value {} has some non-ascii characters", value);
return Err(DmError::Core(ErrorKind::InvalidArgument(err_msg).into()));
}
let num_chars = value.len();
if num_chars == 0 {
return Err(DmError::Core(
ErrorKind::InvalidArgument("value has zero characters".into()).into(),
));
}
if num_chars > max_allowed_chars {
let err_msg = format!(
"value {} has {} chars which is greater than maximum allowed {}",
value, num_chars, max_allowed_chars
);
return Err(DmError::Core(ErrorKind::InvalidArgument(err_msg).into()));
}
Ok(())
}
macro_rules! str_id {
($B:ident, $O:ident, $MAX:ident, $check:ident) => {
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct $B {
inner: str,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $O {
inner: String,
}
impl $B {
pub fn new(value: &str) -> DmResult<&$B> {
$check(value, $MAX - 1)?;
Ok(unsafe { &*(value as *const str as *const $B) })
}
pub fn as_bytes(&self) -> &[u8] {
self.inner.as_bytes()
}
}
impl ToOwned for $B {
type Owned = $O;
fn to_owned(&self) -> $O {
$O {
inner: self.inner.to_owned(),
}
}
}
impl fmt::Display for $B {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", &self.inner)
}
}
impl $O {
pub fn new(value: String) -> DmResult<$O> {
$check(&value, $MAX - 1)?;
Ok($O { inner: value })
}
}
impl AsRef<$B> for $O {
fn as_ref(&self) -> &$B {
self
}
}
impl Borrow<$B> for $O {
fn borrow(&self) -> &$B {
self.deref()
}
}
impl Deref for $O {
type Target = $B;
fn deref(&self) -> &$B {
$B::new(&self.inner).expect("inner satisfies all correctness criteria for $B::new")
}
}
};
}
str_id!(DmName, DmNameBuf, DM_NAME_LEN, str_check);
str_id!(DmUuid, DmUuidBuf, DM_UUID_LEN, str_check);
#[derive(Debug, PartialEq, Eq)]
pub enum DevId<'a> {
Name(&'a DmName),
Uuid(&'a DmUuid),
}
impl<'a> fmt::Display for DevId<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DevId::Name(name) => write!(f, "{}", name),
DevId::Uuid(uuid) => write!(f, "{}", uuid),
}
}
}
const DM_TARGET_TYPE_LEN: usize = 16;
str_id!(TargetType, TargetTypeBuf, DM_TARGET_TYPE_LEN, str_check);
#[cfg(test)]
mod tests {
use super::super::errors::Error;
use super::*;
#[test]
fn test_usize() {
assert_eq!(Sectors(0) * 32usize, Sectors(0));
}
#[test]
pub fn test_empty_name() {
assert!(match DmName::new("") {
Err(DmError::Core(Error(ErrorKind::InvalidArgument(_), _))) => true,
_ => false,
})
}
#[test]
pub fn test_sectors_remainder() {
assert_eq!(Sectors(13) % Sectors(11), Sectors(2));
assert_eq!(Sectors(13) % 11usize, Sectors(2));
}
}