Skip to content

Commit f1552a0

Browse files
bors[bot]matklad
andauthored
Merge #6
6: Alternative set of conversions r=matklad a=matklad @CAD97 what do yo think about this set of conversions? The idea here is that * we provide From's for `u32`, because `TextSize` is, transparently, an `u32`. * we don't provide other fixed sized conversions -- I don't think I've ever needed them, and `TextSize` is not exactly the number, so forcing the occasional user to two a two step conversion seems OK: first covnersions changes to a raw repr, the second conversion is a numeric cast. This is in contrast two a one-step conversion, which mixes both numeric cast and raw-typed. * We provide `TryFrom<usize>` (because that can fail) and `Into<usize>` (because that can't fail. If you are on 16bit machine, you probably want to use u16 for TextSize anyway). Unlike stdlib, we can be more aggressive here. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents c4f6e51 + b862177 commit f1552a0

File tree

1 file changed

+27
-63
lines changed

1 file changed

+27
-63
lines changed

src/size.rs

Lines changed: 27 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},
@@ -70,70 +70,34 @@ impl TextSize {
7070
}
7171
}
7272

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

132-
conversions! {
133-
lt u32 [u8 u16]
134-
eq u32 [u32]
135-
gt u32 [u64]
136-
varries [usize]
85+
impl TryFrom<usize> for TextSize {
86+
type Error = TryFromIntError;
87+
fn try_from(value: usize) -> Result<Self, TryFromIntError> {
88+
Ok(u32::try_from(value)?.into())
89+
}
90+
}
91+
92+
impl From<TextSize> for usize {
93+
fn from(value: TextSize) -> Self {
94+
assert_lossless_conversion();
95+
return value.raw as usize;
96+
97+
const fn assert_lossless_conversion() {
98+
[()][(std::mem::size_of::<usize>() < std::mem::size_of::<u32>()) as usize]
99+
}
100+
}
137101
}
138102

139103
macro_rules! arith {

0 commit comments

Comments
 (0)