Skip to content

Commit

Permalink
auto merge of rust-lang#7117 : jensnockert/rust/freestanding, r=cmr
Browse files Browse the repository at this point in the history
The free-standing functions in f32, f64, i8, i16, i32, i64, u8, u16,
u32, u64, float, int, and uint are replaced with generic functions in
num instead.

This means that instead of having to know everywhere what the type is, like

~~~
f64::sin(x)
~~~

You can simply write code that uses the type-generic versions in num instead, this works for all types that implement the corresponding trait in num.

~~~
num::sin(x)
~~~

Note 1: If you were previously using any of those functions, just replace them
with the corresponding function with the same name in num.

Note 2: If you were using a function that corresponds to an operator, use the
operator instead.

Note 3: This is just rust-lang#7090 reopened against master.
  • Loading branch information
bors committed Jul 9, 2013
2 parents 5aa0ca9 + 20a2fbd commit e388a80
Show file tree
Hide file tree
Showing 41 changed files with 178 additions and 376 deletions.
4 changes: 2 additions & 2 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,11 @@ Use declarations support a number of convenient shortcuts:
An example of `use` declarations:

~~~~
use std::float::sin;
use std::num::sin;
use std::option::{Some, None};
fn main() {
// Equivalent to 'info!(std::float::sin(1.0));'
// Equivalent to 'info!(std::num::sin(1.0));'
info!(sin(1.0));
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'
Expand Down
17 changes: 7 additions & 10 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,13 @@ types.
~~~~
# use std::float;
# use std::num::atan;
fn angle(vector: (float, float)) -> float {
let pi = float::consts::pi;
match vector {
(0f, y) if y < 0f => 1.5 * pi,
(0f, y) => 0.5 * pi,
(x, y) => float::atan(y / x)
(x, y) => atan(y / x)
}
}
~~~~
Expand Down Expand Up @@ -1728,10 +1729,9 @@ To call such a method, just prefix it with the type name and a double colon:

~~~~
# use std::float::consts::pi;
# use std::float::sqrt;
struct Circle { radius: float }
impl Circle {
fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
}
let c = Circle::new(42.5);
~~~~
Expand Down Expand Up @@ -1997,16 +1997,15 @@ implementation to use.

~~~~
# use std::float::consts::pi;
# use std::float::sqrt;
trait Shape { fn new(area: float) -> Self; }
struct Circle { radius: float }
struct Square { length: float }
impl Shape for Circle {
fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
}
impl Shape for Square {
fn new(area: float) -> Square { Square { length: sqrt(area) } }
fn new(area: float) -> Square { Square { length: (area).sqrt() } }
}
let area = 42.5;
Expand Down Expand Up @@ -2154,14 +2153,13 @@ Now, we can implement `Circle` on a type only if we also implement `Shape`.

~~~~
# use std::float::consts::pi;
# use std::float::sqrt;
# trait Shape { fn area(&self) -> float; }
# trait Circle : Shape { fn radius(&self) -> float; }
# struct Point { x: float, y: float }
# fn square(x: float) -> float { x * x }
struct CircleStruct { center: Point, radius: float }
impl Circle for CircleStruct {
fn radius(&self) -> float { sqrt(self.area() / pi) }
fn radius(&self) -> float { (self.area() / pi).sqrt() }
}
impl Shape for CircleStruct {
fn area(&self) -> float { pi * square(self.radius) }
Expand Down Expand Up @@ -2190,12 +2188,11 @@ Likewise, supertrait methods may also be called on trait objects.

~~~ {.xfail-test}
# use std::float::consts::pi;
# use std::float::sqrt;
# trait Shape { fn area(&self) -> float; }
# trait Circle : Shape { fn radius(&self) -> float; }
# struct Point { x: float, y: float }
# struct CircleStruct { center: Point, radius: float }
# impl Circle for CircleStruct { fn radius(&self) -> float { sqrt(self.area() / pi) } }
# impl Circle for CircleStruct { fn radius(&self) -> float { (self.area() / pi).sqrt() } }
# impl Shape for CircleStruct { fn area(&self) -> float { pi * square(self.radius) } }
let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};
Expand Down
5 changes: 3 additions & 2 deletions src/libextra/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use list::{MutList, MutCons, MutNil};
use std::at_vec;
use std::cast::{transmute, transmute_mut, transmute_mut_region};
use std::cast;
use std::num;
use std::ptr;
use std::sys;
use std::uint;
Expand Down Expand Up @@ -175,7 +176,7 @@ impl Arena {
fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
// Allocate a new chunk.
let chunk_size = at_vec::capacity(self.pod_head.data);
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
let new_min_chunk_size = num::max(n_bytes, chunk_size);
self.chunks = @mut MutCons(copy self.pod_head, self.chunks);
self.pod_head =
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
Expand Down Expand Up @@ -217,7 +218,7 @@ impl Arena {
-> (*u8, *u8) {
// Allocate a new chunk.
let chunk_size = at_vec::capacity(self.head.data);
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
let new_min_chunk_size = num::max(n_bytes, chunk_size);
self.chunks = @mut MutCons(copy self.head, self.chunks);
self.head =
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
Expand Down
7 changes: 4 additions & 3 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


use std::cmp;
use std::num;
use std::ops;
use std::uint;
use std::vec;
Expand Down Expand Up @@ -726,7 +727,7 @@ impl Set<uint> for BitvSet {
}
let nbits = self.capacity();
if value >= nbits {
let newsize = uint::max(value, nbits * 2) / uint::bits + 1;
let newsize = num::max(value, nbits * 2) / uint::bits + 1;
assert!(newsize > self.bitv.storage.len());
self.bitv.storage.grow(newsize, &0);
}
Expand Down Expand Up @@ -825,7 +826,7 @@ impl BitvSet {
/// and w1/w2 are the words coming from the two vectors self, other.
fn each_common(&self, other: &BitvSet,
f: &fn(uint, uint, uint) -> bool) -> bool {
let min = uint::min(self.bitv.storage.len(),
let min = num::min(self.bitv.storage.len(),
other.bitv.storage.len());
self.bitv.storage.slice(0, min).iter().enumerate().advance(|(i, &w)| {
f(i * uint::bits, w, other.bitv.storage[i])
Expand All @@ -843,7 +844,7 @@ impl BitvSet {
f: &fn(bool, uint, uint) -> bool) -> bool {
let len1 = self.bitv.storage.len();
let len2 = other.bitv.storage.len();
let min = uint::min(len1, len2);
let min = num::min(len1, len2);

/* only one of these loops will execute and that's the point */
for self.bitv.storage.slice(min, len1).iter().enumerate().advance |(i, &w)| {
Expand Down
3 changes: 2 additions & 1 deletion src/libextra/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

//! A double-ended queue implemented as a circular buffer

use std::num;
use std::uint;
use std::vec;
use std::iterator::FromIterator;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl<T> Deque<T> {
/// Create an empty Deque with space for at least `n` elements.
pub fn with_capacity(n: uint) -> Deque<T> {
Deque{nelts: 0, lo: 0,
elts: vec::from_fn(uint::max(MINIMUM_CAPACITY, n), |_| None)}
elts: vec::from_fn(num::max(MINIMUM_CAPACITY, n), |_| None)}
}

/// Return a reference to the first element in the deque
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::comm::{stream, Port, SharedChan};
use std::ptr;
use std::result::{Result};
use std::result;
use std::uint;
use std::num;
use std::vec;

pub mod rustrt {
Expand Down Expand Up @@ -880,7 +880,7 @@ impl io::Reader for TcpSocketBuf {
let needed = len - count;
if nbuffered > 0 {
unsafe {
let ncopy = uint::min(nbuffered, needed);
let ncopy = num::min(nbuffered, needed);
let dst = ptr::mut_offset(
vec::raw::to_mut_ptr(buf), count);
let src = ptr::offset(
Expand Down
17 changes: 9 additions & 8 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ A BigInt is a combination of BigUint and Sign.

use std::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use std::int;
use std::num;
use std::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, Orderable};
use std::str;
use std::uint;
Expand Down Expand Up @@ -204,7 +205,7 @@ impl Unsigned for BigUint {}
impl Add<BigUint, BigUint> for BigUint {

fn add(&self, other: &BigUint) -> BigUint {
let new_len = uint::max(self.data.len(), other.data.len());
let new_len = num::max(self.data.len(), other.data.len());

let mut carry = 0;
let mut sum = do vec::from_fn(new_len) |i| {
Expand All @@ -224,7 +225,7 @@ impl Add<BigUint, BigUint> for BigUint {
impl Sub<BigUint, BigUint> for BigUint {

fn sub(&self, other: &BigUint) -> BigUint {
let new_len = uint::max(self.data.len(), other.data.len());
let new_len = num::max(self.data.len(), other.data.len());

let mut borrow = 0;
let diff = do vec::from_fn(new_len) |i| {
Expand Down Expand Up @@ -260,7 +261,7 @@ impl Mul<BigUint, BigUint> for BigUint {
// = a1*b1 * base^2 +
// (a1*b1 + a0*b0 - (a1-b0)*(b1-a0)) * base +
// a0*b0
let half_len = uint::max(s_len, o_len) / 2;
let half_len = num::max(s_len, o_len) / 2;
let (sHi, sLo) = cut_at(self, half_len);
let (oHi, oLo) = cut_at(other, half_len);

Expand Down Expand Up @@ -297,7 +298,7 @@ impl Mul<BigUint, BigUint> for BigUint {


fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) {
let mid = uint::min(a.data.len(), n);
let mid = num::min(a.data.len(), n);
return (BigUint::from_slice(a.data.slice(mid, a.data.len())),
BigUint::from_slice(a.data.slice(0, mid)));
}
Expand Down Expand Up @@ -482,7 +483,7 @@ impl Integer for BigUint {
impl IntConvertible for BigUint {

fn to_int(&self) -> int {
uint::min(self.to_uint(), int::max_value as uint) as int
num::min(self.to_uint(), int::max_value as uint) as int
}


Expand Down Expand Up @@ -580,7 +581,7 @@ impl BigUint {
let mut n: BigUint = Zero::zero();
let mut power: BigUint = One::one();
loop {
let start = uint::max(end, unit_len) - unit_len;
let start = num::max(end, unit_len) - unit_len;
match uint::parse_bytes(buf.slice(start, end), radix) {
// FIXME(#6102): Assignment operator for BigInt causes ICE
// Some(d) => n += BigUint::from_uint(d) * power,
Expand Down Expand Up @@ -1055,9 +1056,9 @@ impl IntConvertible for BigInt {

fn to_int(&self) -> int {
match self.sign {
Plus => uint::min(self.to_uint(), int::max_value as uint) as int,
Plus => num::min(self.to_uint(), int::max_value as uint) as int,
Zero => 0,
Minus => uint::min((-self).to_uint(),
Minus => num::min((-self).to_uint(),
(int::max_value as uint) + 1) as int
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@


use std::cast;
use std::num;
use std::ptr;
use std::sys;
use std::uint;
use std::vec;
use future_spawn = future::spawn;

Expand Down Expand Up @@ -44,15 +44,15 @@ fn map_slices<A:Copy + Send,B:Copy + Send>(
~[f()(0u, xs)]
}
else {
let num_tasks = uint::min(MAX_TASKS, len / MIN_GRANULARITY);
let num_tasks = num::min(MAX_TASKS, len / MIN_GRANULARITY);

let items_per_task = len / num_tasks;

let mut futures = ~[];
let mut base = 0u;
info!("spawning tasks");
while base < len {
let end = uint::min(len, base + items_per_task);
let end = num::min(len, base + items_per_task);
do xs.as_imm_buf |p, _len| {
let f = f();
let base = base;
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ mod test_qsort {

let expected = ~[1, 2, 3];

do quick_sort(names) |x, y| { int::le(*x, *y) };
do quick_sort(names) |x, y| { *x < *y };

let immut_names = names;

Expand Down
3 changes: 1 addition & 2 deletions src/libextra/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use sort;
use std::cmp;
use std::io;
use std::num;
use std::f64;
use std::vec;

// NB: this can probably be rewritten in terms of num::Num
Expand Down Expand Up @@ -178,7 +177,7 @@ impl<'self> Stats for &'self [f64] {
}

fn std_dev(self) -> f64 {
f64::sqrt(self.var())
self.var().sqrt()
}

fn std_dev_pct(self) -> f64 {
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#[allow(missing_doc)];


use std::i32;
use std::int;
use std::io;
use std::num;
use std::str;

static NSEC_PER_SEC: i32 = 1_000_000_000_i32;
Expand Down Expand Up @@ -249,7 +249,7 @@ impl Tm {
} else {
let s = self.strftime("%Y-%m-%dT%H:%M:%S");
let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' };
let mut m = i32::abs(self.tm_gmtoff) / 60_i32;
let mut m = num::abs(self.tm_gmtoff) / 60_i32;
let h = m / 60_i32;
m -= h * 60_i32;
s + fmt!("%c%02d:%02d", sign, h as int, m as int)
Expand Down Expand Up @@ -832,7 +832,7 @@ priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
'Z' => copy tm.tm_zone,
'z' => {
let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
let mut m = i32::abs(tm.tm_gmtoff) / 60_i32;
let mut m = num::abs(tm.tm_gmtoff) / 60_i32;
let h = m / 60_i32;
m -= h * 60_i32;
fmt!("%c%02d%02d", sign, h as int, m as int)
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! `TotalOrd`.


use std::uint;
use std::num;
use std::util::{swap, replace};

// This is implemented as an AA tree, which is a simplified variation of
Expand Down Expand Up @@ -63,7 +63,7 @@ fn lt<K: Ord + TotalOrd, V: Ord>(a: &TreeMap<K, V>,
let mut y = b.iter();

let (a_len, b_len) = (a.len(), b.len());
for uint::min(a_len, b_len).times {
for num::min(a_len, b_len).times {
let (key_a, value_a) = x.next().unwrap();
let (key_b, value_b) = y.next().unwrap();
if *key_a < *key_b { return true; }
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use metadata::cstore;
use metadata::filesearch;

use std::hashmap::HashSet;
use std::num;
use std::os;
use std::uint;
use std::util;
Expand Down Expand Up @@ -141,7 +142,7 @@ pub fn get_relative_to(abs1: &Path, abs2: &Path) -> Path {
assert!(len1 > 0);
assert!(len2 > 0);

let max_common_path = uint::min(len1, len2) - 1;
let max_common_path = num::min(len1, len2) - 1;
let mut start_idx = 0;
while start_idx < max_common_path
&& split1[start_idx] == split2[start_idx] {
Expand Down
Loading

0 comments on commit e388a80

Please sign in to comment.