Skip to content

Commit 240c713

Browse files
committed
feat(api): more multiplication impls for size
1 parent ec1e93f commit 240c713

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/size.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ pub trait Size:
3333
fn display(self, input: Self::DisplayFormat) -> Self::DisplayOutput;
3434
}
3535

36+
macro_rules! impl_mul {
37+
($name:ident: $inner:ident *= $($num_type:ident)+) => {
38+
$(
39+
impl Mul<$num_type> for $name {
40+
type Output = Self;
41+
fn mul(self, rhs: $num_type) -> Self::Output {
42+
self.0.mul(rhs as $inner).into()
43+
}
44+
}
45+
46+
impl Mul<$name> for $num_type {
47+
type Output = $name;
48+
fn mul(self, rhs: $name) -> Self::Output {
49+
rhs * self
50+
}
51+
}
52+
53+
impl MulAssign<$num_type> for $name {
54+
fn mul_assign(&mut self, rhs: $num_type) {
55+
self.0 *= rhs as $inner;
56+
}
57+
}
58+
)+
59+
};
60+
61+
($name:ident: u64) => {
62+
impl_mul!($name: u64 *= u8 u16 u32 u64);
63+
};
64+
}
65+
3666
macro_rules! newtype {
3767
(
3868
$(#[$attribute:meta])*
@@ -65,25 +95,7 @@ macro_rules! newtype {
6595
}
6696
}
6797

68-
impl Mul<$inner> for $name {
69-
type Output = Self;
70-
fn mul(self, rhs: $inner) -> Self::Output {
71-
self.0.mul(rhs).into()
72-
}
73-
}
74-
75-
impl Mul<$name> for $inner {
76-
type Output = $name;
77-
fn mul(self, rhs: $name) -> Self::Output {
78-
rhs * self
79-
}
80-
}
81-
82-
impl MulAssign<$inner> for $name {
83-
fn mul_assign(&mut self, rhs: $inner) {
84-
self.0 *= rhs;
85-
}
86-
}
98+
impl_mul!($name: u64);
8799
};
88100
}
89101

0 commit comments

Comments
 (0)