Skip to content

Commit b25bcd0

Browse files
authored
Merge pull request #429 from mulkieran/master-display
Add implementation of Display to range! macro
2 parents ed76930 + b1bcbc9 commit b25bcd0

2 files changed

Lines changed: 20 additions & 30 deletions

File tree

src/range_macros.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// An omnibus macro that includes all simple macros.
66
macro_rules! range {
7-
($T: ident) => {
7+
($T: ident, $display_name: expr) => {
88
macro_attr! {
99
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
1010
/// A type for $T
@@ -21,6 +21,7 @@ macro_rules! range {
2121
NewtypeSubAssign! { () pub struct $T(u64); }
2222

2323
debug_macro!($T);
24+
display!($T, $display_name);
2425
serde!($T);
2526
sum!($T);
2627

@@ -52,6 +53,16 @@ macro_rules! debug_macro {
5253
};
5354
}
5455

56+
macro_rules! display {
57+
($T:ident, $display_name:expr) => {
58+
impl fmt::Display for $T {
59+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60+
write!(f, "{} {}", self.0, $display_name)
61+
}
62+
}
63+
};
64+
}
65+
5566
macro_rules! serde {
5667
($T:ident) => {
5768
impl serde::Serialize for $T {
@@ -232,7 +243,7 @@ mod tests {
232243
use std::ops::{Add, Div, Mul, Rem};
233244
use std::u64;
234245

235-
range!(Units);
246+
range!(Units, "units");
236247

237248
#[test]
238249
/// Test implicit derivations for Units
@@ -268,6 +279,9 @@ mod tests {
268279

269280
// Test Debug
270281
assert_eq!(format!("{:?}", Units(3)), "Units(3)");
282+
283+
// Test Display
284+
assert_eq!(format!("{:}", Units(3)), "3 units");
271285
}
272286

273287
#[test]

src/types.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,9 @@ const META_BLOCK_SIZE: Sectors = Sectors(8);
2828
#[allow(dead_code)]
2929
const MAX_META_DEV_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));
3030

31-
range!(DataBlocks);
31+
range!(DataBlocks, "data blocks");
3232

33-
impl fmt::Display for DataBlocks {
34-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35-
write!(f, "{} data blocks", self.0)
36-
}
37-
}
38-
39-
range!(MetaBlocks);
33+
range!(MetaBlocks, "meta blocks");
4034

4135
impl MetaBlocks {
4236
/// Return the number of Sectors in the MetaBlocks.
@@ -45,13 +39,7 @@ impl MetaBlocks {
4539
}
4640
}
4741

48-
impl fmt::Display for MetaBlocks {
49-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50-
write!(f, "{} meta blocks", self.0)
51-
}
52-
}
53-
54-
range!(Bytes);
42+
range!(Bytes, "bytes");
5543

5644
impl Bytes {
5745
/// Return the number of Sectors fully contained in these bytes.
@@ -60,13 +48,7 @@ impl Bytes {
6048
}
6149
}
6250

63-
impl fmt::Display for Bytes {
64-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65-
write!(f, "{} bytes", self.0)
66-
}
67-
}
68-
69-
range!(Sectors);
51+
range!(Sectors, "sectors");
7052

7153
impl Sectors {
7254
/// The number of bytes in these sectors.
@@ -80,12 +62,6 @@ impl Sectors {
8062
}
8163
}
8264

83-
impl fmt::Display for Sectors {
84-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85-
write!(f, "{} sectors", self.0)
86-
}
87-
}
88-
8965
/// Returns an error if value is unsuitable.
9066
fn str_check(value: &str, max_allowed_chars: usize) -> DmResult<()> {
9167
if !value.is_ascii() {

0 commit comments

Comments
 (0)