Skip to content

Commit e0528a5

Browse files
committed
auto merge of #9209 : blake2-ppc/rust/from-str, r=thestinger
Remove these in favor of the two traits themselves and the wrapper function std::from_str::from_str. Add the function std::num::from_str_radix in the corresponding role for the FromStrRadix trait. With `from_str` in the prelude, and `from_str_radix` in `std::num`, the feature is unfied under the type annotation of these functions instead of using the modules-named-as-types (std::uint and others): What was before: let n = std::uint::from_str("1"); let m = std::i32::from_str_radix("10", 16); is now: let n = from_str::<uint>("1"); let m = std::num::from_str_radix::<i32>("10", 16);
2 parents 4ecb0a3 + 8522341 commit e0528a5

35 files changed

+132
-158
lines changed

doc/tutorial-conditions.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn read_int_pairs() -> ~[(int,int)] {
9191
[a, b] => {
9292
9393
// 5. Try parsing both fields as ints.
94-
match (int::from_str(a), int::from_str(b)) {
94+
match (from_str::<int>(a), from_str::<int>(b)) {
9595
9696
// 6. If parsing succeeded for both, push both.
9797
(Some(a), Some(b)) => pairs.push((a,b)),
@@ -124,7 +124,7 @@ for conveying a value of type `T`, represented as `Some(T)`
124124
_or_ the sentinel `None`, to indicate the absence of a `T` value.
125125
For simple APIs, it may be sufficient to encode errors as `Option<T>`,
126126
returning `Some(T)` on success and `None` on error.
127-
In the example program, the call to `int::from_str` returns `Option<int>`
127+
In the example program, the call to `from_str::<int>` returns `Option<int>`
128128
with the understanding that "all parse errors" result in `None`.
129129
The resulting `Option<int>` values are matched against the pattern `(Some(a), Some(b))`
130130
in steps 5 and 6 in the example program,
@@ -161,7 +161,7 @@ This second mechanism for indicating an error is called a `Result`.
161161
The type `std::result::Result<T,E>` is another simple `enum` type with two forms, `Ok(T)` and `Err(E)`.
162162
The `Result` type is not substantially different from the `Option` type in terms of its ergonomics.
163163
Its main advantage is that the error constructor `Err(E)` can convey _more detail_ about the error.
164-
For example, the `int::from_str` API could be reformed
164+
For example, the `from_str` API could be reformed
165165
to return a `Result` carrying an informative description of a parse error,
166166
like this:
167167

@@ -172,7 +172,7 @@ enum IntParseErr {
172172
BadChar(char)
173173
}
174174
175-
fn int::from_str(&str) -> Result<int,IntParseErr> {
175+
fn from_str(&str) -> Result<int,IntParseErr> {
176176
// ...
177177
}
178178
~~~~
@@ -297,8 +297,8 @@ fn read_int_pairs() -> ~[(int,int)] {
297297
let line = fi.read_line();
298298
let fields = line.word_iter().to_owned_vec();
299299
match fields {
300-
[a, b] => pairs.push((int::from_str(a).unwrap(),
301-
int::from_str(b).unwrap())),
300+
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
301+
from_str::<int>(b).unwrap())),
302302
303303
// Explicitly fail on malformed lines.
304304
_ => fail!()
@@ -398,8 +398,8 @@ fn read_int_pairs() -> ~[(int,int)] {
398398
let line = fi.read_line();
399399
let fields = line.word_iter().to_owned_vec();
400400
match fields {
401-
[a, b] => pairs.push((int::from_str(a).unwrap(),
402-
int::from_str(b).unwrap())),
401+
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
402+
from_str::<int>(b).unwrap())),
403403
404404
// On malformed lines, call the condition handler and
405405
// push whatever the condition handler returns.
@@ -475,8 +475,8 @@ fn read_int_pairs() -> ~[(int,int)] {
475475
let line = fi.read_line();
476476
let fields = line.word_iter().to_owned_vec();
477477
match fields {
478-
[a, b] => pairs.push((int::from_str(a).unwrap(),
479-
int::from_str(b).unwrap())),
478+
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
479+
from_str::<int>(b).unwrap())),
480480
_ => pairs.push(malformed_line::cond.raise(line.clone()))
481481
}
482482
}
@@ -553,8 +553,8 @@ fn read_int_pairs() -> ~[(int,int)] {
553553
let line = fi.read_line();
554554
let fields = line.word_iter().to_owned_vec();
555555
match fields {
556-
[a, b] => pairs.push((int::from_str(a).unwrap(),
557-
int::from_str(b).unwrap())),
556+
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
557+
from_str::<int>(b).unwrap())),
558558
559559
// On malformed lines, call the condition handler and
560560
// either ignore the line (if the handler returns `None`)
@@ -649,8 +649,8 @@ fn read_int_pairs() -> ~[(int,int)] {
649649
let line = fi.read_line();
650650
let fields = line.word_iter().to_owned_vec();
651651
match fields {
652-
[a, b] => pairs.push((int::from_str(a).unwrap(),
653-
int::from_str(b).unwrap())),
652+
[a, b] => pairs.push((from_str::<int>(a).unwrap(),
653+
from_str::<int>(b).unwrap())),
654654
655655
// On malformed lines, call the condition handler and
656656
// take action appropriate to the enum value returned.
@@ -776,7 +776,7 @@ fn main() {
776776
// Parse an int; if parsing fails, call the condition handler and
777777
// return whatever it returns.
778778
fn parse_int(x: &str) -> int {
779-
match int::from_str(x) {
779+
match from_str::<int>(x) {
780780
Some(v) => v,
781781
None => malformed_int::cond.raise(x.to_owned())
782782
}
@@ -833,8 +833,8 @@ There are three other things to note in this variant of the example program:
833833
so long as the `raise` occurs within a callee (of any depth) of the logic protected by the `trap` call,
834834
it will invoke the handler.
835835

836-
- This variant insulates callers from a design choice in the `int` library:
837-
the `int::from_str` function was designed to return an `Option<int>`,
836+
- This variant insulates callers from a design choice in the library:
837+
the `from_str` function was designed to return an `Option<int>`,
838838
but this program insulates callers from that choice,
839839
routing all `None` values that arise from parsing integers in this file into the condition.
840840

@@ -873,4 +873,4 @@ To compensate for this risk, correct C++ and Java code must program in an extrem
873873
or else risk introducing silent and very difficult-to-debug errors due to control resuming in a corrupted heap after a caught exception.
874874
These errors are frequently memory-safety errors, which Rust strives to eliminate,
875875
and so Rust unwinding is unrecoverable within a single task:
876-
once unwinding starts, the entire local heap of a task is destroyed and the task is terminated.
876+
once unwinding starts, the entire local heap of a task is destroyed and the task is terminated.

src/libextra/fileinput.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ mod test {
538538

539539
do input_vec_state(filenames) |line, state| {
540540
let nums: ~[&str] = line.split_iter(' ').collect();
541-
let file_num = uint::from_str(nums[0]).unwrap();
542-
let line_num = uint::from_str(nums[1]).unwrap();
541+
let file_num = from_str::<uint>(nums[0]).unwrap();
542+
let line_num = from_str::<uint>(nums[1]).unwrap();
543543
assert_eq!(line_num, state.line_num_file);
544544
assert_eq!(file_num * 3 + line_num, state.line_num);
545545
true

src/libextra/semver.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::io::{ReaderUtil};
1919
use std::io;
2020
use std::option::{Option, Some, None};
2121
use std::to_str::ToStr;
22-
use std::uint;
2322

2423
#[deriving(Clone, Eq)]
2524
pub enum Identifier {
@@ -140,7 +139,7 @@ fn take_nonempty_prefix(rdr: @io::Reader,
140139

141140
fn take_num(rdr: @io::Reader, ch: char) -> (uint, char) {
142141
let (s, ch) = take_nonempty_prefix(rdr, ch, char::is_digit);
143-
match uint::from_str(s) {
142+
match from_str::<uint>(s) {
144143
None => { bad_parse::cond.raise(()); (0, ch) },
145144
Some(i) => (i, ch)
146145
}
@@ -149,7 +148,7 @@ fn take_num(rdr: @io::Reader, ch: char) -> (uint, char) {
149148
fn take_ident(rdr: @io::Reader, ch: char) -> (Identifier, char) {
150149
let (s,ch) = take_nonempty_prefix(rdr, ch, char::is_alphanumeric);
151150
if s.iter().all(char::is_digit) {
152-
match uint::from_str(s) {
151+
match from_str::<uint>(s) {
153152
None => { bad_parse::cond.raise(()); (Numeric(0), ch) },
154153
Some(i) => (Numeric(i), ch)
155154
}

src/libextra/test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use std::task;
3737
use std::to_str::ToStr;
3838
use std::f64;
3939
use std::os;
40-
use std::uint;
4140

4241

4342
// The name of a test. By convention this follows the rules for rust
@@ -253,7 +252,7 @@ pub fn parse_opts(args: &[~str]) -> OptRes {
253252
let ratchet_metrics = ratchet_metrics.map_move(|s| Path(s));
254253

255254
let ratchet_noise_percent = getopts::opt_maybe_str(&matches, "ratchet-noise-percent");
256-
let ratchet_noise_percent = ratchet_noise_percent.map_move(|s| f64::from_str(s).unwrap());
255+
let ratchet_noise_percent = ratchet_noise_percent.map_move(|s| from_str::<f64>(s).unwrap());
257256

258257
let save_metrics = getopts::opt_maybe_str(&matches, "save-metrics");
259258
let save_metrics = save_metrics.map_move(|s| Path(s));
@@ -281,7 +280,7 @@ pub fn opt_shard(maybestr: Option<~str>) -> Option<(uint,uint)> {
281280
None => None,
282281
Some(s) => {
283282
match s.split_iter('.').to_owned_vec() {
284-
[a, b] => match (uint::from_str(a), uint::from_str(b)) {
283+
[a, b] => match (from_str::<uint>(a), from_str::<uint>(b)) {
285284
(Some(a), Some(b)) => Some((a,b)),
286285
_ => None
287286
},

src/libstd/num/int_macros.rs

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,6 @@ impl Primitive for $T {
383383

384384
// String conversion functions and impl str -> num
385385

386-
/// Parse a string as a number in base 10.
387-
#[inline]
388-
pub fn from_str(s: &str) -> Option<$T> {
389-
strconv::from_str_common(s, 10u, true, false, false,
390-
strconv::ExpNone, false, false)
391-
}
392-
393-
/// Parse a string as a number in the given base.
394-
#[inline]
395-
pub fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
396-
strconv::from_str_common(s, radix, true, false, false,
397-
strconv::ExpNone, false, false)
398-
}
399-
400386
/// Parse a byte slice as a number in the given base.
401387
#[inline]
402388
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
@@ -407,14 +393,16 @@ pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
407393
impl FromStr for $T {
408394
#[inline]
409395
fn from_str(s: &str) -> Option<$T> {
410-
from_str(s)
396+
strconv::from_str_common(s, 10u, true, false, false,
397+
strconv::ExpNone, false, false)
411398
}
412399
}
413400

414401
impl FromStrRadix for $T {
415402
#[inline]
416403
fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
417-
from_str_radix(s, radix)
404+
strconv::from_str_common(s, radix, true, false, false,
405+
strconv::ExpNone, false, false)
418406
}
419407
}
420408

@@ -462,10 +450,7 @@ mod tests {
462450
use super::*;
463451

464452
use int;
465-
use i16;
466453
use i32;
467-
use i64;
468-
use i8;
469454
use num;
470455
use sys;
471456

@@ -670,20 +655,20 @@ mod tests {
670655

671656
#[test]
672657
fn test_from_str() {
673-
assert_eq!(from_str("0"), Some(0 as $T));
674-
assert_eq!(from_str("3"), Some(3 as $T));
675-
assert_eq!(from_str("10"), Some(10 as $T));
676-
assert_eq!(i32::from_str("123456789"), Some(123456789 as i32));
677-
assert_eq!(from_str("00100"), Some(100 as $T));
658+
assert_eq!(from_str::<$T>("0"), Some(0 as $T));
659+
assert_eq!(from_str::<$T>("3"), Some(3 as $T));
660+
assert_eq!(from_str::<$T>("10"), Some(10 as $T));
661+
assert_eq!(from_str::<i32>("123456789"), Some(123456789 as i32));
662+
assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
678663

679-
assert_eq!(from_str("-1"), Some(-1 as $T));
680-
assert_eq!(from_str("-3"), Some(-3 as $T));
681-
assert_eq!(from_str("-10"), Some(-10 as $T));
682-
assert_eq!(i32::from_str("-123456789"), Some(-123456789 as i32));
683-
assert_eq!(from_str("-00100"), Some(-100 as $T));
664+
assert_eq!(from_str::<$T>("-1"), Some(-1 as $T));
665+
assert_eq!(from_str::<$T>("-3"), Some(-3 as $T));
666+
assert_eq!(from_str::<$T>("-10"), Some(-10 as $T));
667+
assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32));
668+
assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T));
684669

685-
assert!(from_str(" ").is_none());
686-
assert!(from_str("x").is_none());
670+
assert!(from_str::<$T>(" ").is_none());
671+
assert!(from_str::<$T>("x").is_none());
687672
}
688673

689674
#[test]
@@ -751,36 +736,36 @@ mod tests {
751736
#[test]
752737
fn test_int_from_str_overflow() {
753738
let mut i8_val: i8 = 127_i8;
754-
assert_eq!(i8::from_str("127"), Some(i8_val));
755-
assert!(i8::from_str("128").is_none());
739+
assert_eq!(from_str::<i8>("127"), Some(i8_val));
740+
assert!(from_str::<i8>("128").is_none());
756741
757742
i8_val += 1 as i8;
758-
assert_eq!(i8::from_str("-128"), Some(i8_val));
759-
assert!(i8::from_str("-129").is_none());
743+
assert_eq!(from_str::<i8>("-128"), Some(i8_val));
744+
assert!(from_str::<i8>("-129").is_none());
760745
761746
let mut i16_val: i16 = 32_767_i16;
762-
assert_eq!(i16::from_str("32767"), Some(i16_val));
763-
assert!(i16::from_str("32768").is_none());
747+
assert_eq!(from_str::<i16>("32767"), Some(i16_val));
748+
assert!(from_str::<i16>("32768").is_none());
764749
765750
i16_val += 1 as i16;
766-
assert_eq!(i16::from_str("-32768"), Some(i16_val));
767-
assert!(i16::from_str("-32769").is_none());
751+
assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
752+
assert!(from_str::<i16>("-32769").is_none());
768753
769754
let mut i32_val: i32 = 2_147_483_647_i32;
770-
assert_eq!(i32::from_str("2147483647"), Some(i32_val));
771-
assert!(i32::from_str("2147483648").is_none());
755+
assert_eq!(from_str::<i32>("2147483647"), Some(i32_val));
756+
assert!(from_str::<i32>("2147483648").is_none());
772757
773758
i32_val += 1 as i32;
774-
assert_eq!(i32::from_str("-2147483648"), Some(i32_val));
775-
assert!(i32::from_str("-2147483649").is_none());
759+
assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
760+
assert!(from_str::<i32>("-2147483649").is_none());
776761
777762
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
778-
assert_eq!(i64::from_str("9223372036854775807"), Some(i64_val));
779-
assert!(i64::from_str("9223372036854775808").is_none());
763+
assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val));
764+
assert!(from_str::<i64>("9223372036854775808").is_none());
780765
781766
i64_val += 1 as i64;
782-
assert_eq!(i64::from_str("-9223372036854775808"), Some(i64_val));
783-
assert!(i64::from_str("-9223372036854775809").is_none());
767+
assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
768+
assert!(from_str::<i64>("-9223372036854775809").is_none());
784769
}
785770

786771
#[test]

src/libstd/num/num.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ pub trait FromStrRadix {
439439
fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
440440
}
441441

442+
/// A utility function that just calls FromStrRadix::from_str_radix
443+
pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
444+
FromStrRadix::from_str_radix(str, radix)
445+
}
446+
442447
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
443448
///
444449
/// Returns `radix^pow` as `T`.

0 commit comments

Comments
 (0)