Skip to content

Commit 0f4acdb

Browse files
committed
Alternative set of conversions
1 parent ea66e56 commit 0f4acdb

File tree

1 file changed

+22
-63
lines changed

1 file changed

+22
-63
lines changed

src/size.rs

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {
22
crate::TextSized,
33
std::{
4-
convert::{TryFrom, TryInto},
4+
convert::TryFrom,
55
fmt, iter,
66
num::TryFromIntError,
77
ops::{Add, AddAssign, Sub, SubAssign},
@@ -76,70 +76,29 @@ impl TextSize {
7676
}
7777
}
7878

79-
macro_rules! conversions {
80-
(From<TextSize> for $gte:ident) => {
81-
impl From<TextSize> for $gte {
82-
fn from(value: TextSize) -> $gte {
83-
value.raw.into()
84-
}
85-
}
86-
};
87-
(From<$lte:ident> for TextSize) => {
88-
impl From<$lte> for TextSize {
89-
fn from(value: $lte) -> TextSize {
90-
TextSize(value.into())
91-
}
92-
}
93-
};
94-
(TryFrom<TextSize> for $lt:ident) => {
95-
impl TryFrom<TextSize> for $lt {
96-
type Error = TryFromIntError;
97-
fn try_from(value: TextSize) -> Result<$lt, Self::Error> {
98-
value.raw.try_into()
99-
}
100-
}
101-
};
102-
(TryFrom<$gt:ident> for TextSize) => {
103-
impl TryFrom<$gt> for TextSize {
104-
type Error = <$gt as TryInto<u32>>::Error;
105-
fn try_from(value: $gt) -> Result<TextSize, Self::Error> {
106-
value.try_into().map(TextSize)
107-
}
108-
}
109-
};
110-
{
111-
lt u32 [$($lt:ident)*]
112-
eq u32 [$($eq:ident)*]
113-
gt u32 [$($gt:ident)*]
114-
varries [$($var:ident)*]
115-
} => {
116-
$(
117-
conversions!(From<$lt> for TextSize);
118-
conversions!(TryFrom<TextSize> for $lt);
119-
)*
120-
121-
$(
122-
conversions!(From<$eq> for TextSize);
123-
conversions!(From<TextSize> for $eq);
124-
)*
125-
126-
$(
127-
conversions!(TryFrom<$gt> for TextSize);
128-
conversions!(From<TextSize> for $gt);
129-
)*
130-
131-
$(
132-
conversions!(TryFrom<$var> for TextSize);
133-
conversions!(TryFrom<TextSize> for $var);
134-
)*
135-
};
79+
impl From<u32> for TextSize {
80+
fn from(raw: u32) -> Self {
81+
TextSize { raw }
82+
}
83+
}
84+
85+
impl From<TextSize> for u32 {
86+
fn from(value: TextSize) -> Self {
87+
value.raw
88+
}
13689
}
13790

138-
conversions! {
139-
lt u32 [u8 u16]
140-
eq u32 [u32]
141-
gt u32 [u64]
142-
varries [usize]
91+
impl TryFrom<usize> for TextSize {
92+
type Error = TryFromIntError;
93+
fn try_from(value: usize) -> Result<Self, TryFromIntError> {
94+
Ok(u32::try_from(value)?.into())
95+
}
96+
}
97+
98+
impl From<TextSize> for usize {
99+
fn from(value: TextSize) -> Self {
100+
value.raw as usize
101+
}
143102
}
144103

145104
// NB: We do not provide the transparent-ref impls like the stdlib does.

0 commit comments

Comments
 (0)