Skip to content

Commit 50868c6

Browse files
committed
Refactor to_primitive_int/uint macros
1 parent 6d7bbb1 commit 50868c6

File tree

1 file changed

+60
-83
lines changed

1 file changed

+60
-83
lines changed

src/cast.rs

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use core::{f32, f64};
44
use core::mem::size_of;
55
use core::num::Wrapping;
66

7-
use identities::Zero;
8-
use bounds::Bounded;
97
use float::FloatCore;
108

119
/// A generic trait for converting a value to a number.
@@ -79,62 +77,52 @@ pub trait ToPrimitive {
7977
}
8078

8179
macro_rules! impl_to_primitive_int_to_int {
82-
($SrcT:ty, $DstT:ty, $slf:expr) => (
83-
{
84-
if size_of::<$SrcT>() <= size_of::<$DstT>() {
85-
Some($slf as $DstT)
80+
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
81+
#[inline]
82+
fn $method(&self) -> Option<$DstT> {
83+
let min = $DstT::MIN as $SrcT;
84+
let max = $DstT::MAX as $SrcT;
85+
if size_of::<$SrcT>() <= size_of::<$DstT>() || (min <= *self && *self <= max) {
86+
Some(*self as $DstT)
8687
} else {
87-
let n = $slf as i64;
88-
let min_value: $DstT = Bounded::min_value();
89-
let max_value: $DstT = Bounded::max_value();
90-
if min_value as i64 <= n && n <= max_value as i64 {
91-
Some($slf as $DstT)
92-
} else {
93-
None
94-
}
88+
None
9589
}
9690
}
97-
)
91+
)*}
9892
}
9993

10094
macro_rules! impl_to_primitive_int_to_uint {
101-
($SrcT:ty, $DstT:ty, $slf:expr) => (
102-
{
103-
let zero: $SrcT = Zero::zero();
104-
let max_value: $DstT = Bounded::max_value();
105-
if zero <= $slf && $slf as u64 <= max_value as u64 {
106-
Some($slf as $DstT)
95+
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
96+
#[inline]
97+
fn $method(&self) -> Option<$DstT> {
98+
let max = $DstT::MAX as u64;
99+
if 0 <= *self && (size_of::<$SrcT>() < size_of::<$DstT>() || *self as u64 <= max) {
100+
Some(*self as $DstT)
107101
} else {
108102
None
109103
}
110104
}
111-
)
105+
)*}
112106
}
113107

114108
macro_rules! impl_to_primitive_int {
115-
($T:ty) => (
109+
($T:ident) => (
116110
impl ToPrimitive for $T {
117-
#[inline]
118-
fn to_isize(&self) -> Option<isize> { impl_to_primitive_int_to_int!($T, isize, *self) }
119-
#[inline]
120-
fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8, *self) }
121-
#[inline]
122-
fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16, *self) }
123-
#[inline]
124-
fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32, *self) }
125-
#[inline]
126-
fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64, *self) }
111+
impl_to_primitive_int_to_int! { $T:
112+
fn to_isize -> isize;
113+
fn to_i8 -> i8;
114+
fn to_i16 -> i16;
115+
fn to_i32 -> i32;
116+
fn to_i64 -> i64;
117+
}
127118

128-
#[inline]
129-
fn to_usize(&self) -> Option<usize> { impl_to_primitive_int_to_uint!($T, usize, *self) }
130-
#[inline]
131-
fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8, *self) }
132-
#[inline]
133-
fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16, *self) }
134-
#[inline]
135-
fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32, *self) }
136-
#[inline]
137-
fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64, *self) }
119+
impl_to_primitive_int_to_uint! { $T:
120+
fn to_usize -> usize;
121+
fn to_u8 -> u8;
122+
fn to_u16 -> u16;
123+
fn to_u32 -> u32;
124+
fn to_u64 -> u64;
125+
}
138126

139127
#[inline]
140128
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
@@ -151,62 +139,51 @@ impl_to_primitive_int!(i32);
151139
impl_to_primitive_int!(i64);
152140

153141
macro_rules! impl_to_primitive_uint_to_int {
154-
($DstT:ty, $slf:expr) => (
155-
{
156-
let max_value: $DstT = Bounded::max_value();
157-
if $slf as u64 <= max_value as u64 {
158-
Some($slf as $DstT)
142+
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
143+
#[inline]
144+
fn $method(&self) -> Option<$DstT> {
145+
let max = $DstT::MAX as u64;
146+
if size_of::<$SrcT>() < size_of::<$DstT>() || *self as u64 <= max {
147+
Some(*self as $DstT)
159148
} else {
160149
None
161150
}
162151
}
163-
)
152+
)*}
164153
}
165154

166155
macro_rules! impl_to_primitive_uint_to_uint {
167-
($SrcT:ty, $DstT:ty, $slf:expr) => (
168-
{
169-
if size_of::<$SrcT>() <= size_of::<$DstT>() {
170-
Some($slf as $DstT)
156+
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
157+
#[inline]
158+
fn $method(&self) -> Option<$DstT> {
159+
let max = $DstT::MAX as $SrcT;
160+
if size_of::<$SrcT>() <= size_of::<$DstT>() || *self <= max {
161+
Some(*self as $DstT)
171162
} else {
172-
let zero: $SrcT = Zero::zero();
173-
let max_value: $DstT = Bounded::max_value();
174-
if zero <= $slf && $slf as u64 <= max_value as u64 {
175-
Some($slf as $DstT)
176-
} else {
177-
None
178-
}
163+
None
179164
}
180165
}
181-
)
166+
)*}
182167
}
183168

184169
macro_rules! impl_to_primitive_uint {
185-
($T:ty) => (
170+
($T:ident) => (
186171
impl ToPrimitive for $T {
187-
#[inline]
188-
fn to_isize(&self) -> Option<isize> { impl_to_primitive_uint_to_int!(isize, *self) }
189-
#[inline]
190-
fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8, *self) }
191-
#[inline]
192-
fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16, *self) }
193-
#[inline]
194-
fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32, *self) }
195-
#[inline]
196-
fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64, *self) }
172+
impl_to_primitive_uint_to_int! { $T:
173+
fn to_isize -> isize;
174+
fn to_i8 -> i8;
175+
fn to_i16 -> i16;
176+
fn to_i32 -> i32;
177+
fn to_i64 -> i64;
178+
}
197179

198-
#[inline]
199-
fn to_usize(&self) -> Option<usize> {
200-
impl_to_primitive_uint_to_uint!($T, usize, *self)
180+
impl_to_primitive_uint_to_uint! { $T:
181+
fn to_usize -> usize;
182+
fn to_u8 -> u8;
183+
fn to_u16 -> u16;
184+
fn to_u32 -> u32;
185+
fn to_u64 -> u64;
201186
}
202-
#[inline]
203-
fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8, *self) }
204-
#[inline]
205-
fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16, *self) }
206-
#[inline]
207-
fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32, *self) }
208-
#[inline]
209-
fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64, *self) }
210187

211188
#[inline]
212189
fn to_f32(&self) -> Option<f32> { Some(*self as f32) }

0 commit comments

Comments
 (0)