Skip to content

Commit e388a80

Browse files
committed
auto merge of #7117 : jensnockert/rust/freestanding, r=cmr
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 #7090 reopened against master.
2 parents 5aa0ca9 + 20a2fbd commit e388a80

Some content is hidden

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

41 files changed

+178
-376
lines changed

doc/rust.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,11 @@ Use declarations support a number of convenient shortcuts:
802802
An example of `use` declarations:
803803

804804
~~~~
805-
use std::float::sin;
805+
use std::num::sin;
806806
use std::option::{Some, None};
807807
808808
fn main() {
809-
// Equivalent to 'info!(std::float::sin(1.0));'
809+
// Equivalent to 'info!(std::num::sin(1.0));'
810810
info!(sin(1.0));
811811
812812
// Equivalent to 'info!(~[std::option::Some(1.0), std::option::None]);'

doc/tutorial.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,13 @@ types.
503503
504504
~~~~
505505
# use std::float;
506+
# use std::num::atan;
506507
fn angle(vector: (float, float)) -> float {
507508
let pi = float::consts::pi;
508509
match vector {
509510
(0f, y) if y < 0f => 1.5 * pi,
510511
(0f, y) => 0.5 * pi,
511-
(x, y) => float::atan(y / x)
512+
(x, y) => atan(y / x)
512513
}
513514
}
514515
~~~~
@@ -1728,10 +1729,9 @@ To call such a method, just prefix it with the type name and a double colon:
17281729

17291730
~~~~
17301731
# use std::float::consts::pi;
1731-
# use std::float::sqrt;
17321732
struct Circle { radius: float }
17331733
impl Circle {
1734-
fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
1734+
fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
17351735
}
17361736
let c = Circle::new(42.5);
17371737
~~~~
@@ -1997,16 +1997,15 @@ implementation to use.
19971997

19981998
~~~~
19991999
# use std::float::consts::pi;
2000-
# use std::float::sqrt;
20012000
trait Shape { fn new(area: float) -> Self; }
20022001
struct Circle { radius: float }
20032002
struct Square { length: float }
20042003
20052004
impl Shape for Circle {
2006-
fn new(area: float) -> Circle { Circle { radius: sqrt(area / pi) } }
2005+
fn new(area: float) -> Circle { Circle { radius: (area / pi).sqrt() } }
20072006
}
20082007
impl Shape for Square {
2009-
fn new(area: float) -> Square { Square { length: sqrt(area) } }
2008+
fn new(area: float) -> Square { Square { length: (area).sqrt() } }
20102009
}
20112010
20122011
let area = 42.5;
@@ -2154,14 +2153,13 @@ Now, we can implement `Circle` on a type only if we also implement `Shape`.
21542153

21552154
~~~~
21562155
# use std::float::consts::pi;
2157-
# use std::float::sqrt;
21582156
# trait Shape { fn area(&self) -> float; }
21592157
# trait Circle : Shape { fn radius(&self) -> float; }
21602158
# struct Point { x: float, y: float }
21612159
# fn square(x: float) -> float { x * x }
21622160
struct CircleStruct { center: Point, radius: float }
21632161
impl Circle for CircleStruct {
2164-
fn radius(&self) -> float { sqrt(self.area() / pi) }
2162+
fn radius(&self) -> float { (self.area() / pi).sqrt() }
21652163
}
21662164
impl Shape for CircleStruct {
21672165
fn area(&self) -> float { pi * square(self.radius) }
@@ -2190,12 +2188,11 @@ Likewise, supertrait methods may also be called on trait objects.
21902188

21912189
~~~ {.xfail-test}
21922190
# use std::float::consts::pi;
2193-
# use std::float::sqrt;
21942191
# trait Shape { fn area(&self) -> float; }
21952192
# trait Circle : Shape { fn radius(&self) -> float; }
21962193
# struct Point { x: float, y: float }
21972194
# struct CircleStruct { center: Point, radius: float }
2198-
# impl Circle for CircleStruct { fn radius(&self) -> float { sqrt(self.area() / pi) } }
2195+
# impl Circle for CircleStruct { fn radius(&self) -> float { (self.area() / pi).sqrt() } }
21992196
# impl Shape for CircleStruct { fn area(&self) -> float { pi * square(self.radius) } }
22002197
22012198
let concrete = @CircleStruct{center:Point{x:3f,y:4f},radius:5f};

src/libextra/arena.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use list::{MutList, MutCons, MutNil};
4040
use std::at_vec;
4141
use std::cast::{transmute, transmute_mut, transmute_mut_region};
4242
use std::cast;
43+
use std::num;
4344
use std::ptr;
4445
use std::sys;
4546
use std::uint;
@@ -175,7 +176,7 @@ impl Arena {
175176
fn alloc_pod_grow(&mut self, n_bytes: uint, align: uint) -> *u8 {
176177
// Allocate a new chunk.
177178
let chunk_size = at_vec::capacity(self.pod_head.data);
178-
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
179+
let new_min_chunk_size = num::max(n_bytes, chunk_size);
179180
self.chunks = @mut MutCons(copy self.pod_head, self.chunks);
180181
self.pod_head =
181182
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
@@ -217,7 +218,7 @@ impl Arena {
217218
-> (*u8, *u8) {
218219
// Allocate a new chunk.
219220
let chunk_size = at_vec::capacity(self.head.data);
220-
let new_min_chunk_size = uint::max(n_bytes, chunk_size);
221+
let new_min_chunk_size = num::max(n_bytes, chunk_size);
221222
self.chunks = @mut MutCons(copy self.head, self.chunks);
222223
self.head =
223224
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);

src/libextra/bitv.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313

1414
use std::cmp;
15+
use std::num;
1516
use std::ops;
1617
use std::uint;
1718
use std::vec;
@@ -726,7 +727,7 @@ impl Set<uint> for BitvSet {
726727
}
727728
let nbits = self.capacity();
728729
if value >= nbits {
729-
let newsize = uint::max(value, nbits * 2) / uint::bits + 1;
730+
let newsize = num::max(value, nbits * 2) / uint::bits + 1;
730731
assert!(newsize > self.bitv.storage.len());
731732
self.bitv.storage.grow(newsize, &0);
732733
}
@@ -825,7 +826,7 @@ impl BitvSet {
825826
/// and w1/w2 are the words coming from the two vectors self, other.
826827
fn each_common(&self, other: &BitvSet,
827828
f: &fn(uint, uint, uint) -> bool) -> bool {
828-
let min = uint::min(self.bitv.storage.len(),
829+
let min = num::min(self.bitv.storage.len(),
829830
other.bitv.storage.len());
830831
self.bitv.storage.slice(0, min).iter().enumerate().advance(|(i, &w)| {
831832
f(i * uint::bits, w, other.bitv.storage[i])
@@ -843,7 +844,7 @@ impl BitvSet {
843844
f: &fn(bool, uint, uint) -> bool) -> bool {
844845
let len1 = self.bitv.storage.len();
845846
let len2 = other.bitv.storage.len();
846-
let min = uint::min(len1, len2);
847+
let min = num::min(len1, len2);
847848

848849
/* only one of these loops will execute and that's the point */
849850
for self.bitv.storage.slice(min, len1).iter().enumerate().advance |(i, &w)| {

src/libextra/deque.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! A double-ended queue implemented as a circular buffer
1212
13+
use std::num;
1314
use std::uint;
1415
use std::vec;
1516
use std::iterator::FromIterator;
@@ -51,7 +52,7 @@ impl<T> Deque<T> {
5152
/// Create an empty Deque with space for at least `n` elements.
5253
pub fn with_capacity(n: uint) -> Deque<T> {
5354
Deque{nelts: 0, lo: 0,
54-
elts: vec::from_fn(uint::max(MINIMUM_CAPACITY, n), |_| None)}
55+
elts: vec::from_fn(num::max(MINIMUM_CAPACITY, n), |_| None)}
5556
}
5657

5758
/// Return a reference to the first element in the deque

src/libextra/net/tcp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::comm::{stream, Port, SharedChan};
2828
use std::ptr;
2929
use std::result::{Result};
3030
use std::result;
31-
use std::uint;
31+
use std::num;
3232
use std::vec;
3333

3434
pub mod rustrt {
@@ -880,7 +880,7 @@ impl io::Reader for TcpSocketBuf {
880880
let needed = len - count;
881881
if nbuffered > 0 {
882882
unsafe {
883-
let ncopy = uint::min(nbuffered, needed);
883+
let ncopy = num::min(nbuffered, needed);
884884
let dst = ptr::mut_offset(
885885
vec::raw::to_mut_ptr(buf), count);
886886
let src = ptr::offset(

src/libextra/num/bigint.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ A BigInt is a combination of BigUint and Sign.
2121

2222
use std::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
2323
use std::int;
24+
use std::num;
2425
use std::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, Orderable};
2526
use std::str;
2627
use std::uint;
@@ -204,7 +205,7 @@ impl Unsigned for BigUint {}
204205
impl Add<BigUint, BigUint> for BigUint {
205206

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

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

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

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

@@ -297,7 +298,7 @@ impl Mul<BigUint, BigUint> for BigUint {
297298

298299

299300
fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) {
300-
let mid = uint::min(a.data.len(), n);
301+
let mid = num::min(a.data.len(), n);
301302
return (BigUint::from_slice(a.data.slice(mid, a.data.len())),
302303
BigUint::from_slice(a.data.slice(0, mid)));
303304
}
@@ -482,7 +483,7 @@ impl Integer for BigUint {
482483
impl IntConvertible for BigUint {
483484

484485
fn to_int(&self) -> int {
485-
uint::min(self.to_uint(), int::max_value as uint) as int
486+
num::min(self.to_uint(), int::max_value as uint) as int
486487
}
487488

488489

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

10561057
fn to_int(&self) -> int {
10571058
match self.sign {
1058-
Plus => uint::min(self.to_uint(), int::max_value as uint) as int,
1059+
Plus => num::min(self.to_uint(), int::max_value as uint) as int,
10591060
Zero => 0,
1060-
Minus => uint::min((-self).to_uint(),
1061+
Minus => num::min((-self).to_uint(),
10611062
(int::max_value as uint) + 1) as int
10621063
}
10631064
}

src/libextra/par.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111

1212
use std::cast;
13+
use std::num;
1314
use std::ptr;
1415
use std::sys;
15-
use std::uint;
1616
use std::vec;
1717
use future_spawn = future::spawn;
1818

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

4949
let items_per_task = len / num_tasks;
5050

5151
let mut futures = ~[];
5252
let mut base = 0u;
5353
info!("spawning tasks");
5454
while base < len {
55-
let end = uint::min(len, base + items_per_task);
55+
let end = num::min(len, base + items_per_task);
5656
do xs.as_imm_buf |p, _len| {
5757
let f = f();
5858
let base = base;

src/libextra/sort.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ mod test_qsort {
840840

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

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

845845
let immut_names = names;
846846

src/libextra/stats.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use sort;
1212
use std::cmp;
1313
use std::io;
1414
use std::num;
15-
use std::f64;
1615
use std::vec;
1716

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

180179
fn std_dev(self) -> f64 {
181-
f64::sqrt(self.var())
180+
self.var().sqrt()
182181
}
183182

184183
fn std_dev_pct(self) -> f64 {

src/libextra/time.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#[allow(missing_doc)];
1212

1313

14-
use std::i32;
1514
use std::int;
1615
use std::io;
16+
use std::num;
1717
use std::str;
1818

1919
static NSEC_PER_SEC: i32 = 1_000_000_000_i32;
@@ -249,7 +249,7 @@ impl Tm {
249249
} else {
250250
let s = self.strftime("%Y-%m-%dT%H:%M:%S");
251251
let sign = if self.tm_gmtoff > 0_i32 { '+' } else { '-' };
252-
let mut m = i32::abs(self.tm_gmtoff) / 60_i32;
252+
let mut m = num::abs(self.tm_gmtoff) / 60_i32;
253253
let h = m / 60_i32;
254254
m -= h * 60_i32;
255255
s + fmt!("%c%02d:%02d", sign, h as int, m as int)
@@ -832,7 +832,7 @@ priv fn do_strftime(format: &str, tm: &Tm) -> ~str {
832832
'Z' => copy tm.tm_zone,
833833
'z' => {
834834
let sign = if tm.tm_gmtoff > 0_i32 { '+' } else { '-' };
835-
let mut m = i32::abs(tm.tm_gmtoff) / 60_i32;
835+
let mut m = num::abs(tm.tm_gmtoff) / 60_i32;
836836
let h = m / 60_i32;
837837
m -= h * 60_i32;
838838
fmt!("%c%02d%02d", sign, h as int, m as int)

src/libextra/treemap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! `TotalOrd`.
1414
1515

16-
use std::uint;
16+
use std::num;
1717
use std::util::{swap, replace};
1818

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

6565
let (a_len, b_len) = (a.len(), b.len());
66-
for uint::min(a_len, b_len).times {
66+
for num::min(a_len, b_len).times {
6767
let (key_a, value_a) = x.next().unwrap();
6868
let (key_b, value_b) = y.next().unwrap();
6969
if *key_a < *key_b { return true; }

src/librustc/back/rpath.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use metadata::cstore;
1414
use metadata::filesearch;
1515

1616
use std::hashmap::HashSet;
17+
use std::num;
1718
use std::os;
1819
use std::uint;
1920
use std::util;
@@ -141,7 +142,7 @@ pub fn get_relative_to(abs1: &Path, abs2: &Path) -> Path {
141142
assert!(len1 > 0);
142143
assert!(len2 > 0);
143144

144-
let max_common_path = uint::min(len1, len2) - 1;
145+
let max_common_path = num::min(len1, len2) - 1;
145146
let mut start_idx = 0;
146147
while start_idx < max_common_path
147148
&& split1[start_idx] == split2[start_idx] {

0 commit comments

Comments
 (0)