Skip to content

Commit

Permalink
Change the defintion of checked_add! so it can be used in range!
Browse files Browse the repository at this point in the history
It's tidier that way.

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Feb 13, 2019
1 parent e9b7142 commit a5362c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/range_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ macro_rules! range {
pub struct $T(pub u64);
}

checked_add!($T);

NewtypeAdd! { () pub struct $T(u64); }
NewtypeAddAssign! { () pub struct $T(u64); }
NewtypeDeref! { () pub struct $T(u64); }
Expand Down Expand Up @@ -195,9 +197,11 @@ macro_rules! rem {

macro_rules! checked_add {
($T: ident) => {
/// Add two items of type $T, return None if overflow.
pub fn checked_add(&self, other: $T) -> Option<$T> {
self.0.checked_add(other.0).map($T)
impl $T {
/// Add two items of type $T, return None if overflow.
pub fn checked_add(&self, other: $T) -> Option<$T> {
self.0.checked_add(other.0).map($T)
}
}
}
};
}
10 changes: 0 additions & 10 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ const MAX_META_DEV_SIZE: MetaBlocks = MetaBlocks(255 * ((1 << 14) - 64));

range!(DataBlocks);

impl DataBlocks {
checked_add!(DataBlocks);
}

impl fmt::Display for DataBlocks {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} data blocks", self.0)
Expand All @@ -47,8 +43,6 @@ impl MetaBlocks {
pub fn sectors(self) -> Sectors {
self.0 * META_BLOCK_SIZE
}

checked_add!(MetaBlocks);
}

impl fmt::Display for MetaBlocks {
Expand All @@ -64,8 +58,6 @@ impl Bytes {
pub fn sectors(self) -> Sectors {
Sectors(self.0 / SECTOR_SIZE as u64)
}

checked_add!(Bytes);
}

impl fmt::Display for Bytes {
Expand All @@ -86,8 +78,6 @@ impl Sectors {
pub fn metablocks(self) -> MetaBlocks {
MetaBlocks(self / META_BLOCK_SIZE)
}

checked_add!(Sectors);
}

impl fmt::Display for Sectors {
Expand Down

0 comments on commit a5362c3

Please sign in to comment.