Skip to content

Commit 4c30cb2

Browse files
author
Jakub Bukaj
committed
rollup merge of #18976: bjz/rfc369-numerics
2 parents 42c77f4 + d82a7ea commit 4c30cb2

Some content is hidden

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

52 files changed

+580
-757
lines changed

src/compiletest/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::from_str::FromStr;
1211
use std::fmt;
12+
use std::str::FromStr;
1313
use regex::Regex;
1414

1515
#[deriving(Clone, PartialEq)]

src/compiletest/compiletest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern crate regex;
2222
use std::os;
2323
use std::io;
2424
use std::io::fs;
25-
use std::from_str::FromStr;
25+
use std::str::FromStr;
2626
use getopts::{optopt, optflag, reqopt};
2727
use common::Config;
2828
use common::{Pretty, DebugInfoGdb, DebugInfoLldb, Codegen};

src/compiletest/header.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use common::Config;
1212
use common;
1313
use util;
1414

15-
use std::from_str::FromStr;
16-
1715
pub struct TestProps {
1816
// Lines that should be expected, in order, on standard out
1917
pub error_patterns: Vec<String> ,
@@ -353,8 +351,8 @@ pub fn gdb_version_to_int(version_string: &str) -> int {
353351
panic!("{}", error_string);
354352
}
355353

356-
let major: int = FromStr::from_str(components[0]).expect(error_string);
357-
let minor: int = FromStr::from_str(components[1]).expect(error_string);
354+
let major: int = from_str(components[0]).expect(error_string);
355+
let minor: int = from_str(components[1]).expect(error_string);
358356

359357
return major * 1000 + minor;
360358
}
@@ -364,6 +362,6 @@ pub fn lldb_version_to_int(version_string: &str) -> int {
364362
"Encountered LLDB version string with unexpected format: {}",
365363
version_string);
366364
let error_string = error_string.as_slice();
367-
let major: int = FromStr::from_str(version_string).expect(error_string);
365+
let major: int = from_str(version_string).expect(error_string);
368366
return major;
369367
}

src/doc/reference.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,11 +3108,10 @@ then the expression completes.
31083108
Some examples of call expressions:
31093109

31103110
```
3111-
# use std::from_str::FromStr;
31123111
# fn add(x: int, y: int) -> int { 0 }
31133112
31143113
let x: int = add(1, 2);
3115-
let pi: Option<f32> = FromStr::from_str("3.14");
3114+
let pi: Option<f32> = from_str("3.14");
31163115
```
31173116

31183117
### Lambda expressions

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ syn keyword rustTrait RawPtr
105105
syn keyword rustTrait Buffer Writer Reader Seek
106106
syn keyword rustTrait Str StrVector StrSlice
107107
syn keyword rustTrait IntoMaybeOwned StrAllocating UnicodeStrSlice
108-
syn keyword rustTrait ToString IntoStr
108+
syn keyword rustTrait ToString IntoString
109109
syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
110110
syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
111111
syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12

src/libcollections/str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits};
7373
pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items};
7474
pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items};
7575
pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange};
76+
pub use core::str::{FromStr, from_str};
7677
pub use core::str::{Str, StrPrelude};
7778
pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices};
7879

src/libcollections/string.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::raw::Slice as RawSlice;
2525
use hash;
2626
use slice::CloneSliceAllocPrelude;
2727
use str;
28-
use str::{CharRange, StrAllocating, MaybeOwned, Owned};
28+
use str::{CharRange, FromStr, StrAllocating, MaybeOwned, Owned};
2929
use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
3030
use vec::{DerefVec, Vec, as_vec};
3131

@@ -795,6 +795,33 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
795795
DerefString { x: as_vec(x.as_bytes()) }
796796
}
797797

798+
impl FromStr for String {
799+
#[inline]
800+
fn from_str(s: &str) -> Option<String> {
801+
Some(String::from_str(s))
802+
}
803+
}
804+
805+
/// Trait for converting a type to a string, consuming it in the process.
806+
pub trait IntoString {
807+
/// Consume and convert to a string.
808+
fn into_string(self) -> String;
809+
}
810+
811+
/// A generic trait for converting a value to a string
812+
pub trait ToString {
813+
/// Converts the value of `self` to an owned string
814+
fn to_string(&self) -> String;
815+
}
816+
817+
impl<T: fmt::Show> ToString for T {
818+
fn to_string(&self) -> String {
819+
let mut buf = Vec::<u8>::new();
820+
let _ = format_args!(|args| fmt::write(&mut buf, args), "{}", self);
821+
String::from_utf8(buf).unwrap()
822+
}
823+
}
824+
798825
/// Unsafe operations
799826
#[unstable = "waiting on raw module conventions"]
800827
pub mod raw {
@@ -860,7 +887,7 @@ mod tests {
860887

861888
use str;
862889
use str::{Str, StrPrelude, Owned};
863-
use super::{as_string, String};
890+
use super::{as_string, String, ToString};
864891
use vec::Vec;
865892
use slice::CloneSliceAllocPrelude;
866893

@@ -1164,6 +1191,28 @@ mod tests {
11641191
assert_eq!("oob", s[1..4]);
11651192
}
11661193

1194+
#[test]
1195+
fn test_simple_types() {
1196+
assert_eq!(1i.to_string(), "1".to_string());
1197+
assert_eq!((-1i).to_string(), "-1".to_string());
1198+
assert_eq!(200u.to_string(), "200".to_string());
1199+
assert_eq!(2u8.to_string(), "2".to_string());
1200+
assert_eq!(true.to_string(), "true".to_string());
1201+
assert_eq!(false.to_string(), "false".to_string());
1202+
assert_eq!(().to_string(), "()".to_string());
1203+
assert_eq!(("hi".to_string()).to_string(), "hi".to_string());
1204+
}
1205+
1206+
#[test]
1207+
fn test_vectors() {
1208+
let x: Vec<int> = vec![];
1209+
assert_eq!(x.to_string(), "[]".to_string());
1210+
assert_eq!((vec![1i]).to_string(), "[1]".to_string());
1211+
assert_eq!((vec![1i, 2, 3]).to_string(), "[1, 2, 3]".to_string());
1212+
assert!((vec![vec![], vec![1i], vec![1i, 1]]).to_string() ==
1213+
"[[], [1], [1, 1]]".to_string());
1214+
}
1215+
11671216
#[bench]
11681217
fn bench_with_capacity(b: &mut Bencher) {
11691218
b.iter(|| {

src/libcollections/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,13 @@ impl<T> Vec<T> {
16521652
}
16531653
}
16541654

1655+
impl<'a> fmt::FormatWriter for Vec<u8> {
1656+
fn write(&mut self, buf: &[u8]) -> fmt::Result {
1657+
self.push_all(buf);
1658+
Ok(())
1659+
}
1660+
}
1661+
16551662
#[cfg(test)]
16561663
mod tests {
16571664
extern crate test;

src/libcore/num/f32.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
use intrinsics;
1818
use mem;
19-
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20-
use num::Float;
19+
use num::{Float, FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20+
use num::from_str_radix;
2121
use option::Option;
2222

2323
pub const RADIX: uint = 2u;
@@ -424,3 +424,10 @@ impl Float for f32 {
424424
self * (value / 180.0f32)
425425
}
426426
}
427+
428+
#[inline]
429+
#[allow(missing_docs)]
430+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
431+
pub fn from_str_hex(src: &str) -> Option<f32> {
432+
from_str_radix(src, 16)
433+
}

src/libcore/num/f64.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
use intrinsics;
1818
use mem;
19-
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20-
use num::Float;
19+
use num::{Float, FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20+
use num::from_str_radix;
2121
use option::Option;
2222

2323
// FIXME(#5527): These constants should be deprecated once associated
@@ -430,3 +430,10 @@ impl Float for f64 {
430430
self * (value / 180.0)
431431
}
432432
}
433+
434+
#[inline]
435+
#[allow(missing_docs)]
436+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
437+
pub fn from_str_hex(src: &str) -> Option<f64> {
438+
from_str_radix(src, 16)
439+
}

0 commit comments

Comments
 (0)