Skip to content

Commit 9c5034c

Browse files
SparrowLiicuviper
authored andcommitted
take references as parameters
1 parent 50afb16 commit 9c5034c

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pub use bounds::Bounded;
3333
#[cfg(any(feature = "std", feature = "libm"))]
3434
pub use float::Float;
3535
pub use float::FloatConst;
36-
pub use float::FloatCore;
3736
// pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`.
3837
pub use cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive};
3938
pub use identities::{one, zero, One, Zero};

src/ops/euclid.rs

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
1515
///
1616
/// let a: i32 = 7;
1717
/// let b: i32 = 4;
18-
/// assert_eq!(Euclid::div_euclid(a, b), 1); // 7 > 4 * 1
19-
/// assert_eq!(Euclid::div_euclid(-a, b), -2); // -7 >= 4 * -2
20-
/// assert_eq!(Euclid::div_euclid(a, -b), -1); // 7 >= -4 * -1
21-
/// assert_eq!(Euclid::div_euclid(-a, -b), 2); // -7 >= -4 * 2
18+
/// assert_eq!(Euclid::div_euclid(&a, &b), 1); // 7 > 4 * 1
19+
/// assert_eq!(Euclid::div_euclid(&-a, &b), -2); // -7 >= 4 * -2
20+
/// assert_eq!(Euclid::div_euclid(&a, &-b), -1); // 7 >= -4 * -1
21+
/// assert_eq!(Euclid::div_euclid(&-a, &-b), 2); // -7 >= -4 * 2
2222
/// ```
23-
fn div_euclid(self, v: Self) -> Self;
23+
fn div_euclid(&self, v: &Self) -> Self;
2424

2525
/// Calculates the least nonnegative remainder of `self (mod v)`.
2626
///
@@ -40,30 +40,31 @@ pub trait Euclid: Sized + Div<Self, Output = Self> + Rem<Self, Output = Self> {
4040
///
4141
/// let a: i32 = 7;
4242
/// let b: i32 = 4;
43-
/// assert_eq!(Euclid::rem_euclid(a, b), 3);
44-
/// assert_eq!(Euclid::rem_euclid(-a, b), 1);
45-
/// assert_eq!(Euclid::rem_euclid(a, -b), 3);
46-
/// assert_eq!(Euclid::rem_euclid(-a, -b), 1);
43+
/// assert_eq!(Euclid::rem_euclid(&a, &b), 3);
44+
/// assert_eq!(Euclid::rem_euclid(&-a, &b), 1);
45+
/// assert_eq!(Euclid::rem_euclid(&a, &-b), 3);
46+
/// assert_eq!(Euclid::rem_euclid(&-a, &-b), 1);
4747
/// ```
48-
fn rem_euclid(self, v: Self) -> Self;
48+
fn rem_euclid(&self, v: &Self) -> Self;
4949
}
50+
5051
macro_rules! euclid_int_impl {
5152
($($t:ty)*) => {$(
5253
impl Euclid for $t {
5354
#[inline]
54-
fn div_euclid(self, v: $t) -> Self {
55+
fn div_euclid(&self, v: &$t) -> Self {
5556
let q = self / v;
5657
if self % v < 0 {
57-
return if v > 0 { q - 1 } else { q + 1 }
58+
return if *v > 0 { q - 1 } else { q + 1 }
5859
}
5960
q
6061
}
6162

6263
#[inline]
63-
fn rem_euclid(self, v: $t) -> Self {
64+
fn rem_euclid(&self, v: &$t) -> Self {
6465
let r = self % v;
6566
if r < 0 {
66-
if v < 0 {
67+
if *v < 0 {
6768
r - v
6869
} else {
6970
r + v
@@ -75,21 +76,23 @@ macro_rules! euclid_int_impl {
7576
}
7677
)*}
7778
}
79+
7880
macro_rules! euclid_uint_impl {
7981
($($t:ty)*) => {$(
8082
impl Euclid for $t {
8183
#[inline]
82-
fn div_euclid(self, v: $t) -> Self {
84+
fn div_euclid(&self, v: &$t) -> Self {
8385
self / v
8486
}
8587

8688
#[inline]
87-
fn rem_euclid(self, v: $t) -> Self {
89+
fn rem_euclid(&self, v: &$t) -> Self {
8890
self % v
8991
}
9092
}
9193
)*}
9294
}
95+
9396
euclid_int_impl!(isize i8 i16 i32 i64);
9497
euclid_uint_impl!(usize u8 u16 u32 u64);
9598
#[cfg(has_i128)]
@@ -99,19 +102,19 @@ euclid_uint_impl!(u128);
99102

100103
impl Euclid for f32 {
101104
#[inline]
102-
fn div_euclid(self, v: f32) -> f32 {
103-
let q = <f32 as ::FloatCore>::trunc(self / v);
105+
fn div_euclid(&self, v: &f32) -> f32 {
106+
let q = <f32 as ::float::FloatCore>::trunc(self / v);
104107
if self % v < 0.0 {
105-
return if v > 0.0 { q - 1.0 } else { q + 1.0 };
108+
return if *v > 0.0 { q - 1.0 } else { q + 1.0 };
106109
}
107110
q
108111
}
109112

110113
#[inline]
111-
fn rem_euclid(self, v: f32) -> f32 {
114+
fn rem_euclid(&self, v: &f32) -> f32 {
112115
let r = self % v;
113116
if r < 0.0 {
114-
r + <f32 as ::FloatCore>::abs(v)
117+
r + <f32 as ::float::FloatCore>::abs(*v)
115118
} else {
116119
r
117120
}
@@ -120,19 +123,19 @@ impl Euclid for f32 {
120123

121124
impl Euclid for f64 {
122125
#[inline]
123-
fn div_euclid(self, v: f64) -> f64 {
124-
let q = <f64 as ::FloatCore>::trunc(self / v);
126+
fn div_euclid(&self, v: &f64) -> f64 {
127+
let q = <f64 as ::float::FloatCore>::trunc(self / v);
125128
if self % v < 0.0 {
126-
return if v > 0.0 { q - 1.0 } else { q + 1.0 };
129+
return if *v > 0.0 { q - 1.0 } else { q + 1.0 };
127130
}
128131
q
129132
}
130133

131134
#[inline]
132-
fn rem_euclid(self, v: f64) -> f64 {
135+
fn rem_euclid(&self, v: &f64) -> f64 {
133136
let r = self % v;
134137
if r < 0.0 {
135-
r + <f64 as ::FloatCore>::abs(v)
138+
r + <f64 as ::float::FloatCore>::abs(*v)
136139
} else {
137140
r
138141
}
@@ -142,27 +145,28 @@ impl Euclid for f64 {
142145
pub trait CheckedEuclid: Euclid {
143146
/// Performs euclid division that returns `None` instead of panicking on division by zero
144147
/// and instead of wrapping around on underflow and overflow.
145-
fn checked_div_euclid(self, v: Self) -> Option<Self>;
148+
fn checked_div_euclid(&self, v: &Self) -> Option<Self>;
146149

147150
/// Finds the euclid remainder of dividing two numbers, checking for underflow, overflow and
148151
/// division by zero. If any of that happens, `None` is returned.
149-
fn checked_rem_euclid(self, v: Self) -> Option<Self>;
152+
fn checked_rem_euclid(&self, v: &Self) -> Option<Self>;
150153
}
154+
151155
macro_rules! checked_euclid_int_impl {
152156
($($t:ty)*) => {$(
153157
impl CheckedEuclid for $t {
154158
#[inline]
155-
fn checked_div_euclid(self, v: $t) -> Option<$t> {
156-
if v == 0 || (self == Self::min_value() && v == -1) {
159+
fn checked_div_euclid(&self, v: &$t) -> Option<$t> {
160+
if *v == 0 || (*self == Self::min_value() && *v == -1) {
157161
None
158162
} else {
159163
Some(Euclid::div_euclid(self, v))
160164
}
161165
}
162166

163167
#[inline]
164-
fn checked_rem_euclid(self, v: $t) -> Option<$t> {
165-
if v == 0 || (self == Self::min_value() && v == -1) {
168+
fn checked_rem_euclid(&self, v: &$t) -> Option<$t> {
169+
if *v == 0 || (*self == Self::min_value() && *v == -1) {
166170
None
167171
} else {
168172
Some(Euclid::rem_euclid(self, v))
@@ -171,21 +175,22 @@ macro_rules! checked_euclid_int_impl {
171175
}
172176
)*}
173177
}
178+
174179
macro_rules! checked_euclid_uint_impl {
175180
($($t:ty)*) => {$(
176181
impl CheckedEuclid for $t {
177182
#[inline]
178-
fn checked_div_euclid(self, v: $t) -> Option<$t> {
179-
if v == 0{
183+
fn checked_div_euclid(&self, v: &$t) -> Option<$t> {
184+
if *v == 0 {
180185
None
181186
} else {
182187
Some(Euclid::div_euclid(self, v))
183188
}
184189
}
185190

186191
#[inline]
187-
fn checked_rem_euclid(self, v: $t) -> Option<$t> {
188-
if v == 0{
192+
fn checked_rem_euclid(&self, v: &$t) -> Option<$t> {
193+
if *v == 0 {
189194
None
190195
} else {
191196
Some(Euclid::rem_euclid(self, v))
@@ -194,6 +199,7 @@ macro_rules! checked_euclid_uint_impl {
194199
}
195200
)*}
196201
}
202+
197203
checked_euclid_int_impl!(isize i8 i16 i32 i64);
198204
checked_euclid_uint_impl!(usize u8 u16 u32 u64);
199205
#[cfg(has_i128)]
@@ -213,8 +219,8 @@ mod tests {
213219
{
214220
let x: $t = 10;
215221
let y: $t = 3;
216-
assert_eq!(Euclid::div_euclid(x, y),3);
217-
assert_eq!(Euclid::rem_euclid(x, y),1);
222+
assert_eq!(Euclid::div_euclid(&x, &y), 3);
223+
assert_eq!(Euclid::rem_euclid(&x, &y), 1);
218224
}
219225
)+
220226
};
@@ -231,13 +237,13 @@ mod tests {
231237
{
232238
let x: $t = 10;
233239
let y: $t = -3;
234-
assert_eq!(Euclid::div_euclid(x, y),-3);
235-
assert_eq!(Euclid::div_euclid(-x, y),4);
236-
assert_eq!(Euclid::rem_euclid(x, y),1);
237-
assert_eq!(Euclid::rem_euclid(-x, y),2);
238-
let x: $t = $t::min_value()+1;
240+
assert_eq!(Euclid::div_euclid(&x, &y), -3);
241+
assert_eq!(Euclid::div_euclid(&-x, &y), 4);
242+
assert_eq!(Euclid::rem_euclid(&x, &y), 1);
243+
assert_eq!(Euclid::rem_euclid(&-x, &y), 2);
244+
let x: $t = $t::min_value() + 1;
239245
let y: $t = -1;
240-
assert_eq!(Euclid::div_euclid(x, y),$t::max_value());
246+
assert_eq!(Euclid::div_euclid(&x, &y), $t::max_value());
241247
}
242248
)+
243249
};
@@ -254,14 +260,14 @@ mod tests {
254260
{
255261
let x: $t = 12.1;
256262
let y: $t = 3.2;
257-
assert!(Euclid::div_euclid(x, y)*y+Euclid::rem_euclid(x, y)-x
258-
<=46.4 * <$t as ::FloatCore>::epsilon());
259-
assert!(Euclid::div_euclid(x, -y)*-y+Euclid::rem_euclid(x, -y)-x
260-
<= 46.4 * <$t as ::FloatCore>::epsilon());
261-
assert!(Euclid::div_euclid(-x, y)*y+Euclid::rem_euclid(-x, y)-(-x)
262-
<= 46.4 * <$t as ::FloatCore>::epsilon());
263-
assert!(Euclid::div_euclid(-x, -y)*-y+Euclid::rem_euclid(-x, -y)-(-x)
264-
<= 46.4 * <$t as ::FloatCore>::epsilon());
263+
assert!(Euclid::div_euclid(&x, &y) * y + Euclid::rem_euclid(&x, &y) - x
264+
<=46.4 * <$t as ::float::FloatCore>::epsilon());
265+
assert!(Euclid::div_euclid(&x, &-y) * -y + Euclid::rem_euclid(&x, &-y) - x
266+
<= 46.4 * <$t as ::float::FloatCore>::epsilon());
267+
assert!(Euclid::div_euclid(&-x, &y) * y + Euclid::rem_euclid(&-x, &y) + x
268+
<= 46.4 * <$t as ::float::FloatCore>::epsilon());
269+
assert!(Euclid::div_euclid(&-x, &-y) * -y + Euclid::rem_euclid(&-x, &-y) + x
270+
<= 46.4 * <$t as ::float::FloatCore>::epsilon());
265271
}
266272
)+
267273
};
@@ -276,10 +282,10 @@ mod tests {
276282
($($t:ident)+) => {
277283
$(
278284
{
279-
assert_eq!(CheckedEuclid::checked_div_euclid($t::min_value(), -1),None);
280-
assert_eq!(CheckedEuclid::checked_rem_euclid($t::min_value(), -1),None);
281-
assert_eq!(CheckedEuclid::checked_div_euclid(1, 0),None);
282-
assert_eq!(CheckedEuclid::checked_rem_euclid(1, 0),None);
285+
assert_eq!(CheckedEuclid::checked_div_euclid(&$t::min_value(), &-1), None);
286+
assert_eq!(CheckedEuclid::checked_rem_euclid(&$t::min_value(), &-1), None);
287+
assert_eq!(CheckedEuclid::checked_div_euclid(&1, &0), None);
288+
assert_eq!(CheckedEuclid::checked_rem_euclid(&1, &0), None);
283289
}
284290
)+
285291
};

0 commit comments

Comments
 (0)