Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--lib/text-size/src/size.rs90
1 files changed, 27 insertions, 63 deletions
diff --git a/lib/text-size/src/size.rs b/lib/text-size/src/size.rs
index 93cbcf8579..867a0004ae 100644
--- a/lib/text-size/src/size.rs
+++ b/lib/text-size/src/size.rs
@@ -1,7 +1,7 @@
use {
crate::TextSized,
std::{
- convert::{TryFrom, TryInto},
+ convert::TryFrom,
fmt, iter,
num::TryFromIntError,
ops::{Add, AddAssign, Sub, SubAssign},
@@ -70,70 +70,34 @@ impl TextSize {
}
}
-macro_rules! conversions {
- (From<TextSize> for $gte:ident) => {
- impl From<TextSize> for $gte {
- fn from(value: TextSize) -> $gte {
- value.raw.into()
- }
- }
- };
- (From<$lte:ident> for TextSize) => {
- impl From<$lte> for TextSize {
- fn from(value: $lte) -> TextSize {
- TextSize(value.into())
- }
- }
- };
- (TryFrom<TextSize> for $lt:ident) => {
- impl TryFrom<TextSize> for $lt {
- type Error = TryFromIntError;
- fn try_from(value: TextSize) -> Result<$lt, Self::Error> {
- value.raw.try_into()
- }
- }
- };
- (TryFrom<$gt:ident> for TextSize) => {
- impl TryFrom<$gt> for TextSize {
- type Error = <$gt as TryInto<u32>>::Error;
- fn try_from(value: $gt) -> Result<TextSize, Self::Error> {
- value.try_into().map(TextSize)
- }
- }
- };
- {
- lt u32 [$($lt:ident)*]
- eq u32 [$($eq:ident)*]
- gt u32 [$($gt:ident)*]
- varries [$($var:ident)*]
- } => {
- $(
- conversions!(From<$lt> for TextSize);
- conversions!(TryFrom<TextSize> for $lt);
- )*
-
- $(
- conversions!(From<$eq> for TextSize);
- conversions!(From<TextSize> for $eq);
- )*
-
- $(
- conversions!(TryFrom<$gt> for TextSize);
- conversions!(From<TextSize> for $gt);
- )*
-
- $(
- conversions!(TryFrom<$var> for TextSize);
- conversions!(TryFrom<TextSize> for $var);
- )*
- };
+impl From<u32> for TextSize {
+ fn from(raw: u32) -> Self {
+ TextSize { raw }
+ }
+}
+
+impl From<TextSize> for u32 {
+ fn from(value: TextSize) -> Self {
+ value.raw
+ }
}
-conversions! {
- lt u32 [u8 u16]
- eq u32 [u32]
- gt u32 [u64]
- varries [usize]
+impl TryFrom<usize> for TextSize {
+ type Error = TryFromIntError;
+ fn try_from(value: usize) -> Result<Self, TryFromIntError> {
+ Ok(u32::try_from(value)?.into())
+ }
+}
+
+impl From<TextSize> for usize {
+ fn from(value: TextSize) -> Self {
+ assert_lossless_conversion();
+ return value.raw as usize;
+
+ const fn assert_lossless_conversion() {
+ [()][(std::mem::size_of::<usize>() < std::mem::size_of::<u32>()) as usize]
+ }
+ }
}
macro_rules! arith {