Skip to content

Begin stripping down core::num #18827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/guide-lifetimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expensive. So we'd like to define a function that takes the points just as
a reference.

~~~
# use std::num::Float;
# struct Point {x: f64, y: f64}
# fn sqrt(f: f64) -> f64 { 0.0 }
fn compute_distance(p1: &Point, p2: &Point) -> f64 {
Expand Down
2 changes: 2 additions & 0 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Here is another example showing how futures allow you to background
computations. The workload will be distributed on the available cores.

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

```{rust}
use std::num::Float;
use std::rand;
use std::sync::Arc;

Expand Down
3 changes: 1 addition & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ syn keyword rustTrait FromIterator IntoIterator Extend ExactSize
syn keyword rustTrait Iterator DoubleEndedIterator
syn keyword rustTrait RandomAccessIterator CloneableIterator
syn keyword rustTrait OrdIterator MutableDoubleEndedIterator
syn keyword rustTrait Num NumCast CheckedAdd CheckedSub CheckedMul CheckedDiv
syn keyword rustTrait Signed Unsigned Primitive Int Float
syn keyword rustTrait NumCast Int SignedInt UnsignedInt Float
syn keyword rustTrait FloatMath ToPrimitive FromPrimitive
syn keyword rustTrait Box
syn keyword rustTrait GenericPath Path PosixPath WindowsPath
Expand Down
16 changes: 8 additions & 8 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::cmp;
use std::intrinsics::{TyDesc, get_tydesc};
use std::intrinsics;
use std::mem;
use std::num;
use std::num::{Int, UnsignedInt};
use std::ptr;
use std::rc::Rc;
use std::rt::heap::{allocate, deallocate};
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Drop for Arena {

#[inline]
fn round_up(base: uint, align: uint) -> uint {
(base.checked_add(&(align - 1))).unwrap() & !(align - 1)
(base.checked_add(align - 1)).unwrap() & !(align - 1)
}

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

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

return self.alloc_copy_inner(n_bytes, align);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ impl Arena {
self.chunks.borrow_mut().push(self.head.borrow().clone());

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

return self.alloc_noncopy_inner(n_bytes, align);
}
Expand Down Expand Up @@ -376,8 +376,8 @@ fn calculate_size<T>(capacity: uint) -> uint {
let mut size = mem::size_of::<TypedArenaChunk<T>>();
size = round_up(size, mem::min_align_of::<T>());
let elem_size = mem::size_of::<T>();
let elems_size = elem_size.checked_mul(&capacity).unwrap();
size = size.checked_add(&elems_size).unwrap();
let elems_size = elem_size.checked_mul(capacity).unwrap();
size = size.checked_add(elems_size).unwrap();
size
}

Expand Down Expand Up @@ -432,7 +432,7 @@ impl<T> TypedArenaChunk<T> {
#[inline]
fn end(&self) -> *const u8 {
unsafe {
let size = mem::size_of::<T>().checked_mul(&self.capacity).unwrap();
let size = mem::size_of::<T>().checked_mul(self.capacity).unwrap();
self.start().offset(size as int)
}
}
Expand Down Expand Up @@ -481,7 +481,7 @@ impl<T> TypedArena<T> {
fn grow(&self) {
unsafe {
let chunk = *self.first.borrow_mut();
let new_capacity = (*chunk).capacity.checked_mul(&2).unwrap();
let new_capacity = (*chunk).capacity.checked_mul(2).unwrap();
let chunk = TypedArenaChunk::<T>::new(chunk, new_capacity);
self.ptr.set((*chunk).start() as *const T);
self.end.set((*chunk).end() as *const T);
Expand Down
2 changes: 2 additions & 0 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//!
//! ```
//! use std::collections::{BitvSet, Bitv};
//! use std::num::Float;
//! use std::iter;
//!
//! let max_prime = 10000;
Expand Down Expand Up @@ -69,6 +70,7 @@ use core::default::Default;
use core::fmt;
use core::iter::{Chain, Enumerate, Repeat, Skip, Take};
use core::iter;
use core::num::Int;
use core::slice;
use core::u32;
use std::hash;
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use core::prelude::*;
use core::fmt;
use core::num::Int;

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

Expand Down
1 change: 1 addition & 0 deletions src/libcollections/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use core::intrinsics::TypeId;
use core::mem;
use core::num::Int;

use vec::Vec;

Expand Down
14 changes: 7 additions & 7 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::default::Default;
use core::fmt;
use core::kinds::marker::{ContravariantLifetime, InvariantType};
use core::mem;
use core::num;
use core::num::{Int, UnsignedInt};
use core::ops;
use core::ptr;
use core::raw::Slice as RawSlice;
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<T> Vec<T> {
} else if capacity == 0 {
Vec::new()
} else {
let size = capacity.checked_mul(&mem::size_of::<T>())
let size = capacity.checked_mul(mem::size_of::<T>())
.expect("capacity overflow");
let ptr = unsafe { allocate(size, mem::min_align_of::<T>()) };
Vec { ptr: ptr as *mut T, len: 0, cap: capacity }
Expand Down Expand Up @@ -601,11 +601,11 @@ impl<T> Vec<T> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn reserve(&mut self, additional: uint) {
if self.cap - self.len < additional {
match self.len.checked_add(&additional) {
match self.len.checked_add(additional) {
None => panic!("Vec::reserve: `uint` overflow"),
// if the checked_add
Some(new_cap) => {
let amort_cap = num::next_power_of_two(new_cap);
let amort_cap = new_cap.next_power_of_two();
// next_power_of_two will overflow to exactly 0 for really big capacities
if amort_cap == 0 {
self.grow_capacity(new_cap);
Expand Down Expand Up @@ -638,7 +638,7 @@ impl<T> Vec<T> {
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn reserve_exact(&mut self, additional: uint) {
if self.cap - self.len < additional {
match self.len.checked_add(&additional) {
match self.len.checked_add(additional) {
None => panic!("Vec::reserve: `uint` overflow"),
Some(new_cap) => self.grow_capacity(new_cap)
}
Expand Down Expand Up @@ -971,7 +971,7 @@ impl<T> Vec<T> {
pub fn push(&mut self, value: T) {
if mem::size_of::<T>() == 0 {
// zero-size types consume no memory, so we can't rely on the address space running out
self.len = self.len.checked_add(&1).expect("length overflow");
self.len = self.len.checked_add(1).expect("length overflow");
unsafe { mem::forget(value); }
return
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ impl<T> Vec<T> {
if mem::size_of::<T>() == 0 { return }

if capacity > self.cap {
let size = capacity.checked_mul(&mem::size_of::<T>())
let size = capacity.checked_mul(mem::size_of::<T>())
.expect("capacity overflow");
unsafe {
self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size);
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//! operators, you could do the following:
//!
//! ```rust
//! use core::num::SignedInt;
//!
//! // Our type.
//! struct SketchyNum {
//! num : int
Expand Down
10 changes: 5 additions & 5 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use char;
use fmt;
use iter::{range, DoubleEndedIterator};
use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive};
use num::{Zero, One, cast};
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
use num::cast;
use result::Ok;
use slice::{mod, SlicePrelude};
use str::StrPrelude;
Expand Down Expand Up @@ -79,7 +79,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
* - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
* between digit and exponent sign `'p'`.
*/
pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
pub fn float_to_str_bytes_common<T: Float, U>(
num: T,
radix: uint,
negative_zero: bool,
Expand All @@ -97,8 +97,8 @@ pub fn float_to_str_bytes_common<T: Primitive + Float, U>(
_ => ()
}

let _0: T = Zero::zero();
let _1: T = One::one();
let _0: T = Float::zero();
let _1: T = Float::one();

match num.classify() {
FPNaN => return f("NaN".as_bytes()),
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl<'a, T> Pointer for &'a mut T {
macro_rules! floating(($ty:ident) => {
impl Float for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
use num::{Float, Signed};
use num::Float;

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

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

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

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

let digits = match fmt.precision {
Some(i) => float::DigExact(i),
Expand Down
13 changes: 7 additions & 6 deletions src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use fmt;
use iter::DoubleEndedIterator;
use num::{Int, cast, zero};
use num::{Int, cast};
use slice::SlicePrelude;

/// A type that represents a specific radix
Expand All @@ -35,10 +35,11 @@ trait GenericRadix {
fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
// The radix can be as low as 2, so we need a buffer of at least 64
// characters for a base 2 number.
let zero = Int::zero();
let is_positive = x >= zero;
let mut buf = [0u8, ..64];
let base = cast(self.base()).unwrap();
let mut curr = buf.len();
let is_positive = x >= zero();
let base = cast(self.base()).unwrap();
if is_positive {
// Accumulate each digit of the number from the least significant
// to the most significant figure.
Expand All @@ -47,16 +48,16 @@ trait GenericRadix {
x = x / base; // Deaccumulate the number.
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
curr -= 1;
if x == zero() { break; } // No more digits left to accumulate.
if x == zero { break }; // No more digits left to accumulate.
}
} else {
// Do the same as above, but accounting for two's complement.
for byte in buf.iter_mut().rev() {
let n = -(x % base); // Get the current place value.
let n = zero - (x % base); // Get the current place value.
x = x / base; // Deaccumulate the number.
*byte = self.digit(cast(n).unwrap()); // Store the digit in the buffer.
curr -= 1;
if x == zero() { break; } // No more digits left to accumulate.
if x == zero { break }; // No more digits left to accumulate.
}
}
f.pad_integral(is_positive, self.prefix(), buf[curr..])
Expand Down
Loading