Skip to content

Commit 8666812

Browse files
committed
Take parameters by-value in Signed trait
1 parent e4ead7b commit 8666812

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

src/libcore/num/mod.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ pub trait Signed: Num + Neg<Self> {
162162
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
163163
///
164164
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
165-
fn abs(&self) -> Self;
165+
fn abs(self) -> Self;
166166

167167
/// The positive difference of two numbers.
168168
///
169169
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
170170
/// between `self` and `other` is returned.
171-
fn abs_sub(&self, other: &Self) -> Self;
171+
fn abs_sub(self, other: Self) -> Self;
172172

173173
/// Returns the sign of the number.
174174
///
@@ -183,64 +183,64 @@ pub trait Signed: Num + Neg<Self> {
183183
/// * `0` if the number is zero
184184
/// * `1` if the number is positive
185185
/// * `-1` if the number is negative
186-
fn signum(&self) -> Self;
186+
fn signum(self) -> Self;
187187

188188
/// Returns true if the number is positive and false if the number is zero or negative.
189-
fn is_positive(&self) -> bool;
189+
fn is_positive(self) -> bool;
190190

191191
/// Returns true if the number is negative and false if the number is zero or positive.
192-
fn is_negative(&self) -> bool;
192+
fn is_negative(self) -> bool;
193193
}
194194

195195
macro_rules! signed_impl(
196-
($($t:ty)*) => ($(
197-
impl Signed for $t {
196+
($($T:ty)*) => ($(
197+
impl Signed for $T {
198198
#[inline]
199-
fn abs(&self) -> $t {
200-
if self.is_negative() { -*self } else { *self }
199+
fn abs(self) -> $T {
200+
if self.is_negative() { -self } else { self }
201201
}
202202

203203
#[inline]
204-
fn abs_sub(&self, other: &$t) -> $t {
205-
if *self <= *other { 0 } else { *self - *other }
204+
fn abs_sub(self, other: $T) -> $T {
205+
if self <= other { 0 } else { self - other }
206206
}
207207

208208
#[inline]
209-
fn signum(&self) -> $t {
210-
match *self {
209+
fn signum(self) -> $T {
210+
match self {
211211
n if n > 0 => 1,
212212
0 => 0,
213213
_ => -1,
214214
}
215215
}
216216

217217
#[inline]
218-
fn is_positive(&self) -> bool { *self > 0 }
218+
fn is_positive(self) -> bool { self > 0 }
219219

220220
#[inline]
221-
fn is_negative(&self) -> bool { *self < 0 }
221+
fn is_negative(self) -> bool { self < 0 }
222222
}
223223
)*)
224224
)
225225

226226
signed_impl!(int i8 i16 i32 i64)
227227

228228
macro_rules! signed_float_impl(
229-
($t:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => {
230-
impl Signed for $t {
229+
($T:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => {
230+
impl Signed for $T {
231231
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
232232
#[inline]
233-
fn abs(&self) -> $t {
234-
unsafe { $fabs(*self) }
233+
fn abs(self) -> $T {
234+
unsafe { $fabs(self) }
235235
}
236236

237237
/// The positive difference of two numbers. Returns `0.0` if the number is
238238
/// less than or equal to `other`, otherwise the difference between`self`
239239
/// and `other` is returned.
240240
#[inline]
241-
fn abs_sub(&self, other: &$t) -> $t {
242-
extern { fn $fdim(a: $t, b: $t) -> $t; }
243-
unsafe { $fdim(*self, *other) }
241+
fn abs_sub(self, other: $T) -> $T {
242+
extern { fn $fdim(a: $T, b: $T) -> $T; }
243+
unsafe { $fdim(self, other) }
244244
}
245245

246246
/// # Returns
@@ -249,19 +249,19 @@ macro_rules! signed_float_impl(
249249
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
250250
/// - `NAN` if the number is NaN
251251
#[inline]
252-
fn signum(&self) -> $t {
252+
fn signum(self) -> $T {
253253
if self != self { $nan } else {
254-
unsafe { $fcopysign(1.0, *self) }
254+
unsafe { $fcopysign(1.0, self) }
255255
}
256256
}
257257

258258
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
259259
#[inline]
260-
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == $inf }
260+
fn is_positive(self) -> bool { self > 0.0 || (1.0 / self) == $inf }
261261

262262
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
263263
#[inline]
264-
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == $neg_inf }
264+
fn is_negative(self) -> bool { self < 0.0 || (1.0 / self) == $neg_inf }
265265
}
266266
}
267267
)
@@ -287,7 +287,7 @@ pub fn abs<T: Signed>(value: T) -> T {
287287
/// between `x` and `y` is returned.
288288
#[inline(always)]
289289
pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
290-
x.abs_sub(&y)
290+
x.abs_sub(y)
291291
}
292292

293293
/// Returns the sign of the number.

src/libcoretest/num/int_macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ mod tests {
3939

4040
#[test]
4141
fn test_abs_sub() {
42-
assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
43-
assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
44-
assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
45-
assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
42+
assert!((-1 as $T).abs_sub(1 as $T) == 0 as $T);
43+
assert!((1 as $T).abs_sub(1 as $T) == 0 as $T);
44+
assert!((1 as $T).abs_sub(0 as $T) == 1 as $T);
45+
assert!((1 as $T).abs_sub(-1 as $T) == 2 as $T);
4646
}
4747

4848
#[test]

src/libstd/num/f32.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,20 +593,20 @@ mod tests {
593593

594594
#[test]
595595
fn test_abs_sub() {
596-
assert_eq!((-1f32).abs_sub(&1f32), 0f32);
597-
assert_eq!(1f32.abs_sub(&1f32), 0f32);
598-
assert_eq!(1f32.abs_sub(&0f32), 1f32);
599-
assert_eq!(1f32.abs_sub(&-1f32), 2f32);
600-
assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
601-
assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
602-
assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
603-
assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
596+
assert_eq!((-1f32).abs_sub(1f32), 0f32);
597+
assert_eq!(1f32.abs_sub(1f32), 0f32);
598+
assert_eq!(1f32.abs_sub(0f32), 1f32);
599+
assert_eq!(1f32.abs_sub(-1f32), 2f32);
600+
assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
601+
assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
602+
assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
603+
assert_eq!(0f32.abs_sub(INFINITY), 0f32);
604604
}
605605

606606
#[test]
607607
fn test_abs_sub_nowin() {
608-
assert!(NAN.abs_sub(&-1f32).is_nan());
609-
assert!(1f32.abs_sub(&NAN).is_nan());
608+
assert!(NAN.abs_sub(-1f32).is_nan());
609+
assert!(1f32.abs_sub(NAN).is_nan());
610610
}
611611

612612
#[test]

src/libstd/num/f64.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -591,20 +591,20 @@ mod tests {
591591

592592
#[test]
593593
fn test_abs_sub() {
594-
assert_eq!((-1f64).abs_sub(&1f64), 0f64);
595-
assert_eq!(1f64.abs_sub(&1f64), 0f64);
596-
assert_eq!(1f64.abs_sub(&0f64), 1f64);
597-
assert_eq!(1f64.abs_sub(&-1f64), 2f64);
598-
assert_eq!(NEG_INFINITY.abs_sub(&0f64), 0f64);
599-
assert_eq!(INFINITY.abs_sub(&1f64), INFINITY);
600-
assert_eq!(0f64.abs_sub(&NEG_INFINITY), INFINITY);
601-
assert_eq!(0f64.abs_sub(&INFINITY), 0f64);
594+
assert_eq!((-1f64).abs_sub(1f64), 0f64);
595+
assert_eq!(1f64.abs_sub(1f64), 0f64);
596+
assert_eq!(1f64.abs_sub(0f64), 1f64);
597+
assert_eq!(1f64.abs_sub(-1f64), 2f64);
598+
assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
599+
assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
600+
assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
601+
assert_eq!(0f64.abs_sub(INFINITY), 0f64);
602602
}
603603

604604
#[test]
605605
fn test_abs_sub_nowin() {
606-
assert!(NAN.abs_sub(&-1f64).is_nan());
607-
assert!(1f64.abs_sub(&NAN).is_nan());
606+
assert!(NAN.abs_sub(-1f64).is_nan());
607+
assert!(1f64.abs_sub(NAN).is_nan());
608608
}
609609

610610
#[test]

0 commit comments

Comments
 (0)