Skip to content

Commit aefc884

Browse files
committed
libcore: Add a from_str trait
1 parent 07ee299 commit aefc884

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export extfmt;
4949
export rt;
5050
export tuple;
5151
export to_str, to_bytes;
52+
export from_str;
5253
export util;
5354
export dvec, dvec_iter;
5455
export dlist, dlist_iter;
@@ -188,6 +189,7 @@ mod option_iter {
188189
mod result;
189190
mod to_str;
190191
mod to_bytes;
192+
mod from_str;
191193
mod util;
192194

193195
// Data structure modules

src/libcore/int-template.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import T = inst::T;
66
import cmp::{Eq, Ord};
7+
import from_str::FromStr;
78
import num::from_int;
89

910
export min_value, max_value;
@@ -162,6 +163,10 @@ fn parse_buf(buf: &[u8], radix: uint) -> Option<T> {
162163
/// Parse a string to an int
163164
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) }
164165

166+
impl T : FromStr {
167+
static fn from_str(s: &str) -> Option<T> { from_str(s) }
168+
}
169+
165170
/// Convert to a string in a given base
166171
fn to_str(n: T, radix: uint) -> ~str {
167172
do to_str_bytes(n, radix) |slice| {

src/libcore/uint-template.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import T = inst::T;
66
import cmp::{Eq, Ord};
7+
import from_str::FromStr;
78

89
export min_value, max_value;
910
export min, max;
@@ -145,6 +146,10 @@ fn parse_buf(buf: &[const u8], radix: uint) -> Option<T> {
145146
/// Parse a string to an int
146147
fn from_str(s: &str) -> Option<T> { parse_buf(str::to_bytes(s), 10u) }
147148

149+
impl T : FromStr {
150+
static fn from_str(s: &str) -> Option<T> { from_str(s) }
151+
}
152+
148153
/// Parse a string as an unsigned integer.
149154
fn from_str_radix(buf: &str, radix: u64) -> Option<u64> {
150155
if str::len(buf) == 0u { return None; }

0 commit comments

Comments
 (0)