Skip to content

Commit 18575c2

Browse files
committed
Forward euclid methods when possible
1 parent 7861645 commit 18575c2

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn main() {
1919
ac.emit_expression_cfg("1u32.reverse_bits()", "has_reverse_bits");
2020
ac.emit_expression_cfg("1u32.trailing_ones()", "has_leading_trailing_ones");
2121
ac.emit_expression_cfg("{ let mut x = 1; x += &2; }", "has_int_assignop_ref");
22+
ac.emit_expression_cfg("1u32.div_euclid(1u32)", "has_div_euclid");
2223

2324
autocfg::rerun_path("build.rs");
2425
}

src/ops/euclid.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,28 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
4848
fn rem_euclid(&self, v: &Self) -> Self;
4949
}
5050

51+
macro_rules! euclid_forward_impl {
52+
($($t:ty)*) => {$(
53+
#[cfg(has_div_euclid)]
54+
impl Euclid for $t {
55+
#[inline]
56+
fn div_euclid(&self, v: &$t) -> Self {
57+
<$t>::div_euclid(*self, *v)
58+
}
59+
60+
#[inline]
61+
fn rem_euclid(&self, v: &$t) -> Self {
62+
<$t>::rem_euclid(*self, *v)
63+
}
64+
}
65+
)*}
66+
}
67+
5168
macro_rules! euclid_int_impl {
5269
($($t:ty)*) => {$(
70+
euclid_forward_impl!($t);
71+
72+
#[cfg(not(has_div_euclid))]
5373
impl Euclid for $t {
5474
#[inline]
5575
fn div_euclid(&self, v: &$t) -> Self {
@@ -79,6 +99,9 @@ macro_rules! euclid_int_impl {
7999

80100
macro_rules! euclid_uint_impl {
81101
($($t:ty)*) => {$(
102+
euclid_forward_impl!($t);
103+
104+
#[cfg(not(has_div_euclid))]
82105
impl Euclid for $t {
83106
#[inline]
84107
fn div_euclid(&self, v: &$t) -> Self {
@@ -100,6 +123,10 @@ euclid_int_impl!(i128);
100123
#[cfg(has_i128)]
101124
euclid_uint_impl!(u128);
102125

126+
#[cfg(all(has_div_euclid, feature = "std"))]
127+
euclid_forward_impl!(f32 f64);
128+
129+
#[cfg(not(all(has_div_euclid, feature = "std")))]
103130
impl Euclid for f32 {
104131
#[inline]
105132
fn div_euclid(&self, v: &f32) -> f32 {
@@ -121,6 +148,7 @@ impl Euclid for f32 {
121148
}
122149
}
123150

151+
#[cfg(not(all(has_div_euclid, feature = "std")))]
124152
impl Euclid for f64 {
125153
#[inline]
126154
fn div_euclid(&self, v: &f64) -> f64 {
@@ -152,8 +180,28 @@ pub trait CheckedEuclid: Euclid {
152180
fn checked_rem_euclid(&self, v: &Self) -> Option<Self>;
153181
}
154182

183+
macro_rules! checked_euclid_forward_impl {
184+
($($t:ty)*) => {$(
185+
#[cfg(has_div_euclid)]
186+
impl CheckedEuclid for $t {
187+
#[inline]
188+
fn checked_div_euclid(&self, v: &$t) -> Option<Self> {
189+
<$t>::checked_div_euclid(*self, *v)
190+
}
191+
192+
#[inline]
193+
fn checked_rem_euclid(&self, v: &$t) -> Option<Self> {
194+
<$t>::checked_rem_euclid(*self, *v)
195+
}
196+
}
197+
)*}
198+
}
199+
155200
macro_rules! checked_euclid_int_impl {
156201
($($t:ty)*) => {$(
202+
checked_euclid_forward_impl!($t);
203+
204+
#[cfg(not(has_div_euclid))]
157205
impl CheckedEuclid for $t {
158206
#[inline]
159207
fn checked_div_euclid(&self, v: &$t) -> Option<$t> {
@@ -178,6 +226,9 @@ macro_rules! checked_euclid_int_impl {
178226

179227
macro_rules! checked_euclid_uint_impl {
180228
($($t:ty)*) => {$(
229+
checked_euclid_forward_impl!($t);
230+
231+
#[cfg(not(has_div_euclid))]
181232
impl CheckedEuclid for $t {
182233
#[inline]
183234
fn checked_div_euclid(&self, v: &$t) -> Option<$t> {

0 commit comments

Comments
 (0)