提交 e965ba85 编写于 作者: B Brendan Zabarauskas

Remove lots of numeric traits from the preludes

Num, NumCast, Unsigned, Float, Primitive and Int have been removed.
上级 891559e3
......@@ -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 {
......
......@@ -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;
......@@ -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;
......
......@@ -38,7 +38,7 @@
use std::intrinsics::{TyDesc, get_tydesc};
use std::intrinsics;
use std::mem;
use std::num::UnsignedInt;
use std::num::{Int, UnsignedInt};
use std::ptr;
use std::rc::Rc;
use std::rt::heap::{allocate, deallocate};
......
......@@ -22,6 +22,7 @@
//!
//! ```
//! use std::collections::{BitvSet, Bitv};
//! use std::num::Float;
//! use std::iter;
//!
//! let max_prime = 10000;
......@@ -69,6 +70,7 @@
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;
......
......@@ -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)
......
......@@ -69,6 +69,7 @@
use alloc::rc::Rc;
use core::intrinsics::TypeId;
use core::mem;
use core::num::Int;
use vec::Vec;
......
......@@ -21,7 +21,7 @@
use core::fmt;
use core::kinds::marker::{ContravariantLifetime, InvariantType};
use core::mem;
use core::num::UnsignedInt;
use core::num::{Int, UnsignedInt};
use core::ops;
use core::ptr;
use core::raw::Slice as RawSlice;
......
......@@ -234,6 +234,8 @@ fn trunc(self) -> f32 {
/// The fractional part of the number, satisfying:
///
/// ```rust
/// use core::num::Float;
///
/// let x = 1.65f32;
/// assert!(x == x.trunc() + x.fract())
/// ```
......
......@@ -240,6 +240,8 @@ fn trunc(self) -> f64 {
/// The fractional part of the number, satisfying:
///
/// ```rust
/// use core::num::Float;
///
/// let x = 1.65f64;
/// assert!(x == x.trunc() + x.fract())
/// ```
......
......@@ -204,6 +204,8 @@ pub trait Int
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_ones(), 3);
......@@ -215,6 +217,8 @@ pub trait Int
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0b01001100u8;
///
/// assert_eq!(n.count_zeros(), 5);
......@@ -230,6 +234,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0b0101000u16;
///
/// assert_eq!(n.leading_zeros(), 10);
......@@ -242,6 +248,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0b0101000u16;
///
/// assert_eq!(n.trailing_zeros(), 3);
......@@ -254,6 +262,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0x3456789ABCDEF012u64;
///
......@@ -267,6 +277,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xDEF0123456789ABCu64;
///
......@@ -279,6 +291,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
/// let m = 0xEFCDAB8967452301u64;
///
......@@ -293,6 +307,8 @@ fn count_zeros(self) -> uint {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
......@@ -313,6 +329,8 @@ fn from_be(x: Self) -> Self {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
......@@ -333,6 +351,8 @@ fn from_le(x: Self) -> Self {
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "big") {
......@@ -353,6 +373,8 @@ fn to_be(self) -> Self { // or not to be?
/// # Example
///
/// ```rust
/// use std::num::Int;
///
/// let n = 0x0123456789ABCDEFu64;
///
/// if cfg!(target_endian = "little") {
......
......@@ -51,9 +51,7 @@
pub use iter::{FromIterator, Extend};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, CloneableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};
pub use num::{Num, NumCast};
pub use num::{Signed, Unsigned, Float};
pub use num::{Primitive, Int, ToPrimitive, FromPrimitive};
pub use num::{Signed, ToPrimitive, FromPrimitive};
pub use option::{Option, Some, None};
pub use ptr::RawPtr;
pub use result::{Result, Ok, Err};
......
......@@ -12,7 +12,6 @@
use core::iter::order::*;
use core::uint;
use core::cmp;
use core::num;
use core::ops::Slice;
use test::Bencher;
......@@ -689,50 +688,6 @@ fn test_double_ended_range() {
#[test]
fn test_range() {
/// A mock type to check Range when ToPrimitive returns None
struct Foo;
impl ToPrimitive for Foo {
fn to_i64(&self) -> Option<i64> { None }
fn to_u64(&self) -> Option<u64> { None }
}
impl Add<Foo, Foo> for Foo {
fn add(&self, _: &Foo) -> Foo {
Foo
}
}
impl PartialEq for Foo {
fn eq(&self, _: &Foo) -> bool {
true
}
}
impl PartialOrd for Foo {
fn partial_cmp(&self, _: &Foo) -> Option<Ordering> {
None
}
}
impl Clone for Foo {
fn clone(&self) -> Foo {
Foo
}
}
impl Mul<Foo, Foo> for Foo {
fn mul(&self, _: &Foo) -> Foo {
Foo
}
}
impl num::One for Foo {
fn one() -> Foo {
Foo
}
}
assert!(range(0i, 5).collect::<Vec<int>>() == vec![0i, 1, 2, 3, 4]);
assert!(range(-10i, -1).collect::<Vec<int>>() ==
vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]);
......@@ -746,7 +701,6 @@ fn one() -> Foo {
// this test is only meaningful when sizeof uint < sizeof u64
assert_eq!(range(uint::MAX - 1, uint::MAX).size_hint(), (1, Some(1)));
assert_eq!(range(-10i, -1).size_hint(), (9, Some(9)));
assert_eq!(range(Foo, Foo).size_hint(), (0, None));
}
#[test]
......
......@@ -15,6 +15,7 @@
mod tests {
use core::$T_i::*;
use core::int;
use core::num::Int;
use num;
#[test]
......
......@@ -8,7 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::num::cast;
use core::cmp::PartialEq;
use core::fmt::Show;
use core::num::{NumCast, cast};
use core::ops::{Add, Sub, Mul, Div, Rem};
mod int_macros;
mod i8;
......@@ -24,7 +27,12 @@
mod uint;
/// Helper function for testing numeric operations
pub fn test_num<T: Int + ::std::fmt::Show>(ten: T, two: T) {
pub fn test_num<T>(ten: T, two: T) where
T: PartialEq + NumCast
+ Add<T, T> + Sub<T, T>
+ Mul<T, T> + Div<T, T>
+ Rem<T, T> + Show
{
assert_eq!(ten.add(&two), cast(12i).unwrap());
assert_eq!(ten.sub(&two), cast(8i).unwrap());
assert_eq!(ten.mul(&two), cast(20i).unwrap());
......
......@@ -14,6 +14,7 @@
#[cfg(test)]
mod tests {
use core::$T_i::*;
use core::num::Int;
use num;
#[test]
......
......@@ -11,6 +11,7 @@
//! The ChaCha random number generator.
use core::prelude::*;
use core::num::Int;
use {Rng, SeedableRng, Rand};
......
......@@ -23,6 +23,7 @@
#![experimental]
use core::prelude::*;
use core::num::Int;
use {Rng, Rand};
......
......@@ -13,6 +13,7 @@
// this is surprisingly complicated to be both generic & correct
use core::prelude::*;
use core::num::Int;
use Rng;
use distributions::{Sample, IndependentSample};
......@@ -162,6 +163,7 @@ fn sample_range<R: Rng>(r: &Range<$ty>, rng: &mut R) -> $ty {
#[cfg(test)]
mod tests {
use std::num::Int;
use std::prelude::*;
use distributions::{Sample, IndependentSample};
use super::Range;
......
......@@ -114,10 +114,11 @@ pub enum Error {
pub mod reader {
use std::char;
use std::mem::transmute;
use std::int;
use std::option::{None, Option, Some};
use std::io::extensions::u64_from_be_bytes;
use std::mem::transmute;
use std::num::Int;
use std::option::{None, Option, Some};
use serialize;
......
......@@ -23,6 +23,7 @@
use std::iter;
use std::mem;
use std::num::Int;
pub fn run(sess: &session::Session, llmod: ModuleRef,
tm: TargetMachineRef, reachable: &[String]) {
......
......@@ -21,6 +21,7 @@
use std::fmt;
use std::iter::AdditiveIterator;
use std::iter::range_inclusive;
use std::num::Float;
use std::slice;
use syntax::ast::*;
use syntax::ast_util::walk_pat;
......
......@@ -21,6 +21,7 @@
use middle::trans::type_::Type;
use std::num::Int;
use syntax::abi;
use syntax::ast;
......
......@@ -15,6 +15,7 @@
#![allow(deprecated)] // to_be32
use std::iter::range_step;
use std::num::Int;
use std::slice::bytes::{MutableByteVector, copy_memory};
use serialize::hex::ToHex;
......@@ -530,6 +531,7 @@ mod tests {
use self::rand::isaac::IsaacRng;
use self::rand::Rng;
use serialize::hex::FromHex;
use std::num::Int;
// A normal addition - no overflow occurs
#[test]
......
......@@ -199,7 +199,7 @@ fn main() {
use std::{char, f64, fmt, io, num, str};
use std::io::MemWriter;
use std::mem::{swap, transmute};
use std::num::{FPNaN, FPInfinite};
use std::num::{Float, FPNaN, FPInfinite, Int};
use std::str::ScalarValue;
use std::string;
use std::vec::Vec;
......@@ -2455,6 +2455,7 @@ mod tests {
TrailingCharacters, TrailingComma};
use std::{i64, u64, f32, f64, io};
use std::collections::TreeMap;
use std::num::Float;
use std::string;
#[deriving(Decodable, Eq, PartialEq, Show)]
......
......@@ -190,6 +190,7 @@
```rust
use std::fmt;
use std::f64;
use std::num::Float;
struct Vector2D {
x: int,
......
......@@ -20,6 +20,7 @@
use from_str::FromStr;
use intrinsics;
use libc::c_int;
use num::{Float, FloatMath};
use num::strconv;
use num;
......
......@@ -19,6 +19,7 @@
use from_str::FromStr;
use intrinsics;
use libc::c_int;
use num::{Float, FloatMath};
use num::strconv;
use num;
......
......@@ -18,7 +18,9 @@
use option::Option;
#[cfg(test)] use cmp::PartialEq;
#[cfg(test)] use fmt::Show;
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
pub use core::num::{Num, div_rem, Zero, zero, One, one};
pub use core::num::{Signed, abs, signum};
......@@ -135,7 +137,12 @@ pub fn abs_sub<T: FloatMath>(x: T, y: T) -> T {
/// Helper function for testing numeric operations
#[cfg(test)]
pub fn test_num<T: Int + Show>(ten: T, two: T) {
pub fn test_num<T>(ten: T, two: T) where
T: PartialEq + NumCast
+ Add<T, T> + Sub<T, T>
+ Mul<T, T> + Div<T, T>
+ Rem<T, T> + Show
{
assert_eq!(ten.add(&two), cast(12i).unwrap());
assert_eq!(ten.sub(&two), cast(8i).unwrap());
assert_eq!(ten.mul(&two), cast(20i).unwrap());
......
......@@ -67,9 +67,7 @@
#[doc(no_inline)] pub use iter::{Iterator, DoubleEndedIterator};
#[doc(no_inline)] pub use iter::{RandomAccessIterator, CloneableIterator};
#[doc(no_inline)] pub use iter::{OrdIterator, MutableDoubleEndedIterator};
#[doc(no_inline)] pub use num::{Num, NumCast};
#[doc(no_inline)] pub use num::{Signed, Unsigned, Primitive, Int, Float};
#[doc(no_inline)] pub use num::{FloatMath, ToPrimitive, FromPrimitive};
#[doc(no_inline)] pub use num::{Signed, ToPrimitive, FromPrimitive};
#[doc(no_inline)] pub use boxed::Box;
#[doc(no_inline)] pub use option::{Option, Some, None};
#[doc(no_inline)] pub use path::{GenericPath, Path, PosixPath, WindowsPath};
......
......@@ -78,6 +78,7 @@ mod test {
use super::ReaderRng;
use io::MemReader;
use num::Int;
use rand::Rng;
#[test]
......
......@@ -15,6 +15,7 @@
use prelude::*;
use sys::{last_error, retry, fs};
use c_str::CString;
use num::Int;
use path::BytesContainer;
use collections;
......
......@@ -11,6 +11,7 @@
use alloc::arc::Arc;
use libc::{mod, c_char, c_int};
use mem;
use num::Int;
use ptr::{mod, null, null_mut};
use rt::mutex;
use io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr};
......
......@@ -18,6 +18,7 @@
extern crate libc;
use num;
use num::Int;
use prelude::*;
use io::{mod, IoResult, IoError};
use sys_common::mkerr_libc;
......
......@@ -20,6 +20,7 @@
use std::fmt;
use std::fmt::Show;
use std::num::Int;
use std::rc::Rc;
use serialize::{Encodable, Decodable, Encoder, Decoder};
......
......@@ -20,6 +20,7 @@
use std::cell::{Cell, RefCell};
use std::io::File;
use std::rc::Rc;
use std::num::Int;
use std::str;
use std::iter;
......
......@@ -87,6 +87,7 @@
use std::io::fs::PathExtensions;
use std::mem::replace;
use std::mem;
use std::num::Float;
use std::rc::Rc;
use std::iter;
......
......@@ -60,6 +60,7 @@
use std::io::stdio::StdWriter;
use std::io::{File, ChanReader, ChanWriter};
use std::io;
use std::num::{Int, FloatMath};
use std::os;
use std::string::String;
use std::task::TaskBuilder;
......
......@@ -16,6 +16,7 @@
use std::hash::Hash;
use std::io;
use std::mem;
use std::num::{Float, FloatMath};
fn local_cmp<T:Float>(x: T, y: T) -> Ordering {
// arbitrarily decide that NaNs are larger than everything.
......@@ -1042,11 +1043,11 @@ fn t(s: &Summary<f64>, expected: String) {
}
#[test]
fn test_sum_f64s() {
assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(0.0), 5.2999);
assert_eq!([0.5f64, 3.2321f64, 1.5678f64].sum(), 5.2999);
}
#[test]
fn test_sum_f64_between_ints_that_sum_to_0() {
assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(0.0), 1.2);
assert_eq!([1e30f64, 1.2f64, -1e30f64].sum(), 1.2);
}
}
......@@ -1058,7 +1059,7 @@ mod bench {
#[bench]
pub fn sum_three_items(b: &mut Bencher) {
b.iter(|| {
[1e20f64, 1.5f64, -1e20f64].sum(0.0);
[1e20f64, 1.5f64, -1e20f64].sum();
})
}
#[bench]
......@@ -1067,7 +1068,7 @@ pub fn sum_many_f64(b: &mut Bencher) {
let v = Vec::from_fn(500, |i| nums[i%5]);
b.iter(|| {
v.as_slice().sum(0.0);
v.as_slice().sum();
})
}
}
......@@ -13,6 +13,7 @@
// ignore-lexer-test FIXME #15679
use std::f32::consts::PI;
use std::num::{Float, FloatMath};
use std::rand::{Rng, StdRng};
struct Vec2 {
......
......@@ -43,6 +43,7 @@
use std::io;
use std::io::{BufferedWriter, File};
use std::cmp::min;
use std::num::Float;
use std::os;
const LINE_LENGTH: uint = 60;
......
......@@ -19,6 +19,7 @@
use std::collections::HashMap;
use std::mem::replace;
use std::num::Float;
use std::option;
use std::os;
use std::string::String;
......
......@@ -38,6 +38,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
use std::num::Float;
const PI: f64 = 3.141592653589793;
const SOLAR_MASS: f64 = 4.0 * PI * PI;
const YEAR: f64 = 365.24;
......
......@@ -45,6 +45,7 @@
use std::iter::AdditiveIterator;
use std::mem;
use std::num::Float;
use std::os;
use std::raw::Repr;
use std::simd::f64x2;
......
......@@ -15,6 +15,7 @@
use std::io;
use std::io::stdio::StdReader;
use std::io::BufferedReader;
use std::num::Int;
use std::os;
// Computes a single solution to a given 9x9 sudoku
......
......@@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::num::Int;
extern "C" fn foo<T: Int>(a: T, b: T) -> T { a + b }
fn main() {
......
......@@ -11,6 +11,7 @@
extern crate collections;
use std::collections::Bitv;
use std::num::Float;
fn main() {
// Generate sieve of Eratosthenes for n up to 1e6
......
......@@ -12,6 +12,7 @@
// A more complex example of numeric extensions
use std::cmp::{PartialEq, PartialOrd};
use std::num::NumCast;
pub trait TypeExt {}
......
......@@ -11,11 +11,11 @@
use std::cmp::{PartialEq, PartialOrd};
use std::num::NumCast;
pub trait NumExt: PartialEq + PartialOrd + Num + NumCast {}
pub trait NumExt: PartialEq + PartialOrd + NumCast {}
impl NumExt for f32 {}
fn num_eq_one<T:NumExt>(n: T) {
fn num_eq_one<T: NumExt>(n: T) {
println!("{}", n == NumCast::from(1i).unwrap())
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册