Skip to content

Commit 6f7081f

Browse files
committed
auto merge of #18827 : bjz/rust/rfc369-numerics, r=alexcrichton
This implements a considerable portion of rust-lang/rfcs#369 (tracked in #18640). Some interpretations had to be made in order to get this to work. The breaking changes are listed below: [breaking-change] - `core::num::{Num, Unsigned, Primitive}` have been deprecated and their re-exports removed from the `{std, core}::prelude`. - `core::num::{Zero, One, Bounded}` have been deprecated. Use the static methods on `core::num::{Float, Int}` instead. There is no equivalent to `Zero::is_zero`. Use `(==)` with `{Float, Int}::zero` instead. - `Signed::abs_sub` has been moved to `std::num::FloatMath`, and is no longer implemented for signed integers. - `core::num::Signed` has been removed, and its methods have been moved to `core::num::Float` and a new trait, `core::num::SignedInt`. The methods now take the `self` parameter by value. - `core::num::{Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}` have been removed, and their methods moved to `core::num::Int`. Their parameters are now taken by value. This means that - `std::time::Duration` no longer implements `core::num::{Zero, CheckedAdd, CheckedSub}` instead defining the required methods non-polymorphically. - `core::num::{zero, one, abs, signum}` have been deprecated. Use their respective methods instead. - The `core::num::{next_power_of_two, is_power_of_two, checked_next_power_of_two}` functions have been deprecated in favor of methods defined a new trait, `core::num::UnsignedInt` - `core::iter::{AdditiveIterator, MultiplicativeIterator}` are now only implemented for the built-in numeric types. - `core::iter::{range, range_inclusive, range_step, range_step_inclusive}` now require `core::num::Int` to be implemented for the type they a re parametrized over.
2 parents a58fc68 + c9e6bda commit 6f7081f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1137
-1036
lines changed

src/doc/guide-lifetimes.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ expensive. So we'd like to define a function that takes the points just as
5151
a reference.
5252

5353
~~~
54+
# use std::num::Float;
5455
# struct Point {x: f64, y: f64}
5556
# fn sqrt(f: f64) -> f64 { 0.0 }
5657
fn compute_distance(p1: &Point, p2: &Point) -> f64 {

src/doc/guide-tasks.md

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Here is another example showing how futures allow you to background
225225
computations. The workload will be distributed on the available cores.
226226

227227
```{rust}
228+
# use std::num::Float;
228229
# use std::sync::Future;
229230
fn partial_sum(start: uint) -> f64 {
230231
let mut local_sum = 0f64;
@@ -262,6 +263,7 @@ several computations on a single large vector of floats. Each task needs the
262263
full vector to perform its duty.
263264

264265
```{rust}
266+
use std::num::Float;
265267
use std::rand;
266268
use std::sync::Arc;
267269

src/etc/vim/syntax/rust.vim

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ syn keyword rustTrait FromIterator IntoIterator Extend ExactSize
9797
syn keyword rustTrait Iterator DoubleEndedIterator
9898
syn keyword rustTrait RandomAccessIterator CloneableIterator
9999
syn keyword rustTrait OrdIterator MutableDoubleEndedIterator
100-
syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul CheckedDiv
101-
syn keyword rustTrait Signed Unsigned Primitive Int Float
100+
syn keyword rustTrait NumCast Int SignedInt UnsignedInt Float
102101
syn keyword rustTrait FloatMath ToPrimitive FromPrimitive
103102
syn keyword rustTrait Box
104103
syn keyword rustTrait GenericPath Path PosixPath WindowsPath

src/libarena/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::cmp;
3838
use std::intrinsics::{TyDesc, get_tydesc};
3939
use std::intrinsics;
4040
use std::mem;
41-
use std::num;
41+
use std::num::{Int, UnsignedInt};
4242
use std::ptr;
4343
use std::rc::Rc;
4444
use std::rt::heap::{allocate, deallocate};
@@ -132,7 +132,7 @@ impl Drop for Arena {
132132

133133
#[inline]
134134
fn round_up(base: uint, align: uint) -> uint {
135-
(base.checked_add(&(align - 1))).unwrap() & !(align - 1)
135+
(base.checked_add(align - 1)).unwrap() & !(align - 1)
136136
}
137137

138138
// Walk down a chunk, running the destructors for any objects stored
@@ -187,7 +187,7 @@ impl Arena {
187187
self.chunks.borrow_mut().push(self.copy_head.borrow().clone());
188188

189189
*self.copy_head.borrow_mut() =
190-
chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
190+
chunk((new_min_chunk_size + 1u).next_power_of_two(), true);
191191

192192
return self.alloc_copy_inner(n_bytes, align);
193193
}
@@ -228,7 +228,7 @@ impl Arena {
228228
self.chunks.borrow_mut().push(self.head.borrow().clone());
229229

230230
*self.head.borrow_mut() =
231-
chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
231+
chunk((new_min_chunk_size + 1u).next_power_of_two(), false);
232232

233233
return self.alloc_noncopy_inner(n_bytes, align);
234234
}
@@ -376,8 +376,8 @@ fn calculate_size<T>(capacity: uint) -> uint {
376376
let mut size = mem::size_of::<TypedArenaChunk<T>>();
377377
size = round_up(size, mem::min_align_of::<T>());
378378
let elem_size = mem::size_of::<T>();
379-
let elems_size = elem_size.checked_mul(&capacity).unwrap();
380-
size = size.checked_add(&elems_size).unwrap();
379+
let elems_size = elem_size.checked_mul(capacity).unwrap();
380+
size = size.checked_add(elems_size).unwrap();
381381
size
382382
}
383383

@@ -432,7 +432,7 @@ impl<T> TypedArenaChunk<T> {
432432
#[inline]
433433
fn end(&self) -> *const u8 {
434434
unsafe {
435-
let size = mem::size_of::<T>().checked_mul(&self.capacity).unwrap();
435+
let size = mem::size_of::<T>().checked_mul(self.capacity).unwrap();
436436
self.start().offset(size as int)
437437
}
438438
}
@@ -481,7 +481,7 @@ impl<T> TypedArena<T> {
481481
fn grow(&self) {
482482
unsafe {
483483
let chunk = *self.first.borrow_mut();
484-
let new_capacity = (*chunk).capacity.checked_mul(&2).unwrap();
484+
let new_capacity = (*chunk).capacity.checked_mul(2).unwrap();
485485
let chunk = TypedArenaChunk::<T>::new(chunk, new_capacity);
486486
self.ptr.set((*chunk).start() as *const T);
487487
self.end.set((*chunk).end() as *const T);

src/libcollections/bit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
//!
2323
//! ```
2424
//! use std::collections::{BitvSet, Bitv};
25+
//! use std::num::Float;
2526
//! use std::iter;
2627
//!
2728
//! let max_prime = 10000;
@@ -69,6 +70,7 @@ use core::default::Default;
6970
use core::fmt;
7071
use core::iter::{Chain, Enumerate, Repeat, Skip, Take};
7172
use core::iter;
73+
use core::num::Int;
7274
use core::slice;
7375
use core::u32;
7476
use std::hash;

src/libcollections/enum_set.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1616
use core::prelude::*;
1717
use core::fmt;
18+
use core::num::Int;
1819

1920
// FIXME(contentions): implement union family of methods? (general design may be wrong here)
2021

src/libcollections/hash/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use alloc::boxed::Box;
6969
use alloc::rc::Rc;
7070
use core::intrinsics::TypeId;
7171
use core::mem;
72+
use core::num::Int;
7273

7374
use vec::Vec;
7475

src/libcollections/vec.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::default::Default;
2121
use core::fmt;
2222
use core::kinds::marker::{ContravariantLifetime, InvariantType};
2323
use core::mem;
24-
use core::num;
24+
use core::num::{Int, UnsignedInt};
2525
use core::ops;
2626
use core::ptr;
2727
use core::raw::Slice as RawSlice;
@@ -161,7 +161,7 @@ impl<T> Vec<T> {
161161
} else if capacity == 0 {
162162
Vec::new()
163163
} else {
164-
let size = capacity.checked_mul(&mem::size_of::<T>())
164+
let size = capacity.checked_mul(mem::size_of::<T>())
165165
.expect("capacity overflow");
166166
let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
167167
Vec { ptr: ptr as *mut T, len: 0, cap: capacity }
@@ -587,11 +587,11 @@ impl<T> Vec<T> {
587587
#[unstable = "matches collection reform specification, waiting for dust to settle"]
588588
pub fn reserve(&mut self, additional: uint) {
589589
if self.cap - self.len < additional {
590-
match self.len.checked_add(&additional) {
590+
match self.len.checked_add(additional) {
591591
None => panic!("Vec::reserve: `uint` overflow"),
592592
// if the checked_add
593593
Some(new_cap) => {
594-
let amort_cap = num::next_power_of_two(new_cap);
594+
let amort_cap = new_cap.next_power_of_two();
595595
// next_power_of_two will overflow to exactly 0 for really big capacities
596596
if amort_cap == 0 {
597597
self.grow_capacity(new_cap);
@@ -624,7 +624,7 @@ impl<T> Vec<T> {
624624
#[unstable = "matches collection reform specification, waiting for dust to settle"]
625625
pub fn reserve_exact(&mut self, additional: uint) {
626626
if self.cap - self.len < additional {
627-
match self.len.checked_add(&additional) {
627+
match self.len.checked_add(additional) {
628628
None => panic!("Vec::reserve: `uint` overflow"),
629629
Some(new_cap) => self.grow_capacity(new_cap)
630630
}
@@ -957,7 +957,7 @@ impl<T> Vec<T> {
957957
pub fn push(&mut self, value: T) {
958958
if mem::size_of::<T>() == 0 {
959959
// zero-size types consume no memory, so we can't rely on the address space running out
960-
self.len = self.len.checked_add(&1).expect("length overflow");
960+
self.len = self.len.checked_add(1).expect("length overflow");
961961
unsafe { mem::forget(value); }
962962
return
963963
}
@@ -1050,7 +1050,7 @@ impl<T> Vec<T> {
10501050
if mem::size_of::<T>() == 0 { return }
10511051

10521052
if capacity > self.cap {
1053-
let size = capacity.checked_mul(&mem::size_of::<T>())
1053+
let size = capacity.checked_mul(mem::size_of::<T>())
10541054
.expect("capacity overflow");
10551055
unsafe {
10561056
self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size);

src/libcore/cmp.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
//! operators, you could do the following:
2020
//!
2121
//! ```rust
22+
//! use core::num::SignedInt;
23+
//!
2224
//! // Our type.
2325
//! struct SketchyNum {
2426
//! num : int

src/libcore/fmt/float.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
use char;
1414
use fmt;
1515
use iter::{range, DoubleEndedIterator};
16-
use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive};
17-
use num::{Zero, One, cast};
16+
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
17+
use num::cast;
1818
use result::Ok;
1919
use slice::{mod, SlicePrelude};
2020
use str::StrPrelude;
@@ -79,7 +79,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
7979
* - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
8080
* between digit and exponent sign `'p'`.
8181
*/
82-
pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
82+
pub fn float_to_str_bytes_common<T: Float, U>(
8383
num: T,
8484
radix: uint,
8585
negative_zero: bool,
@@ -97,8 +97,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
9797
_ => ()
9898
}
9999

100-
let _0: T = Zero::zero();
101-
let _1: T = One::one();
100+
let _0: T = Float::zero();
101+
let _1: T = Float::one();
102102

103103
match num.classify() {
104104
FPNaN => return f("NaN".as_bytes()),

src/libcore/fmt/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ impl<'a, T> Pointer for &'a mut T {
620620
macro_rules! floating(($ty:ident) => {
621621
impl Float for $ty {
622622
fn fmt(&self, fmt: &mut Formatter) -> Result {
623-
use num::{Float, Signed};
623+
use num::Float;
624624

625625
let digits = match fmt.precision {
626626
Some(i) => float::DigExact(i),
@@ -641,7 +641,7 @@ macro_rules! floating(($ty:ident) => {
641641

642642
impl LowerExp for $ty {
643643
fn fmt(&self, fmt: &mut Formatter) -> Result {
644-
use num::{Float, Signed};
644+
use num::Float;
645645

646646
let digits = match fmt.precision {
647647
Some(i) => float::DigExact(i),
@@ -662,7 +662,7 @@ macro_rules! floating(($ty:ident) => {
662662

663663
impl UpperExp for $ty {
664664
fn fmt(&self, fmt: &mut Formatter) -> Result {
665-
use num::{Float, Signed};
665+
use num::Float;
666666

667667
let digits = match fmt.precision {
668668
Some(i) => float::DigExact(i),

src/libcore/fmt/num.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use fmt;
1818
use iter::DoubleEndedIterator;
19-
use num::{Int, cast, zero};
19+
use num::{Int, cast};
2020
use slice::SlicePrelude;
2121

2222
/// A type that represents a specific radix
@@ -35,10 +35,11 @@ trait GenericRadix {
3535
fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
3636
// The radix can be as low as 2, so we need a buffer of at least 64
3737
// characters for a base 2 number.
38+
let zero = Int::zero();
39+
let is_positive = x >= zero;
3840
let mut buf = [0u8, ..64];
39-
let base = cast(self.base()).unwrap();
4041
let mut curr = buf.len();
41-
let is_positive = x >= zero();
42+
let base = cast(self.base()).unwrap();
4243
if is_positive {
4344
// Accumulate each digit of the number from the least significant
4445
// to the most significant figure.
@@ -47,16 +48,16 @@ trait GenericRadix {
4748
x = x / base; // Deaccumulate the number.
4849
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
4950
curr -= 1;
50-
if x == zero() { break; } // No more digits left to accumulate.
51+
if x == zero { break }; // No more digits left to accumulate.
5152
}
5253
} else {
5354
// Do the same as above, but accounting for two's complement.
5455
for byte in buf.iter_mut().rev() {
55-
let n = -(x % base); // Get the current place value.
56+
let n = zero - (x % base); // Get the current place value.
5657
x = x / base; // Deaccumulate the number.
5758
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
5859
curr -= 1;
59-
if x == zero() { break; } // No more digits left to accumulate.
60+
if x == zero { break }; // No more digits left to accumulate.
6061
}
6162
}
6263
f.pad_integral(is_positive, self.prefix(), buf[curr..])

0 commit comments

Comments
 (0)