Skip to content

Commit dda69ab

Browse files
committed
Add impls for primitive types in libstd
These impls are mostly currently blank, but they will get filled out in the future with implementations as we remove satellite traits in favor of inherent methods on primitives.
1 parent 3173630 commit dda69ab

21 files changed

+197
-20
lines changed

src/libextra/uuid.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Examples of string representations:
5858
use std::str;
5959
use std::vec;
6060
use std::num::FromStrRadix;
61-
use std::char::Char;
6261
use std::container::Container;
6362
use std::to_str::ToStr;
6463
use std::rand;

src/libstd/bool.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ pub fn all_values(blk: |v: bool|) {
6666
// Methods on `bool`
6767
/////////////////////////////////////////////////////////////////////////////
6868

69-
/// Extension methods on a `bool`
70-
pub trait Bool {
69+
#[cfg(not(stage0), not(test))]
70+
#[lang = "bool_impl"]
71+
/// The boolean primitive for Rust.
72+
///
73+
/// This primitive can have two values, `true` and `false`, and is type type
74+
/// that is used to conditions in `if` statements and `while` loops.
75+
impl bool {
7176
/// Conjunction of two boolean values.
7277
///
7378
/// # Examples
@@ -78,7 +83,7 @@ pub trait Bool {
7883
/// assert_eq!(false.and(true), false);
7984
/// assert_eq!(false.and(false), false);
8085
/// ```
81-
fn and(self, b: bool) -> bool;
86+
pub fn and(self, b: bool) -> bool { self && b }
8287

8388
/// Disjunction of two boolean values.
8489
///
@@ -90,7 +95,8 @@ pub trait Bool {
9095
/// assert_eq!(false.or(true), true);
9196
/// assert_eq!(false.or(false), false);
9297
/// ```
93-
fn or(self, b: bool) -> bool;
98+
#[inline]
99+
pub fn or(self, b: bool) -> bool { self || b }
94100

95101
/// An 'exclusive or' of two boolean values.
96102
///
@@ -104,7 +110,8 @@ pub trait Bool {
104110
/// assert_eq!(false.xor(true), true);
105111
/// assert_eq!(false.xor(false), false);
106112
/// ```
107-
fn xor(self, b: bool) -> bool;
113+
#[inline]
114+
pub fn xor(self, b: bool) -> bool { self ^ b }
108115

109116
/// Implication between two boolean values.
110117
///
@@ -120,7 +127,8 @@ pub trait Bool {
120127
/// assert_eq!(false.implies(true), true);
121128
/// assert_eq!(false.implies(false), true);
122129
/// ```
123-
fn implies(self, b: bool) -> bool;
130+
#[inline]
131+
pub fn implies(self, b: bool) -> bool { !self || b }
124132

125133
/// Convert a `bool` to a `u8`.
126134
///
@@ -130,9 +138,24 @@ pub trait Bool {
130138
/// assert_eq!(true.to_bit::<u8>(), 1u8);
131139
/// assert_eq!(false.to_bit::<u8>(), 0u8);
132140
/// ```
141+
#[inline]
142+
pub fn to_bit<N: FromPrimitive>(self) -> N {
143+
if self { FromPrimitive::from_u8(1).unwrap() }
144+
else { FromPrimitive::from_u8(0).unwrap() }
145+
}
146+
}
147+
148+
#[cfg(stage0)]
149+
#[allow(missing_doc)]
150+
pub trait Bool {
151+
fn and(self, b: bool) -> bool;
152+
fn or(self, b: bool) -> bool;
153+
fn xor(self, b: bool) -> bool;
154+
fn implies(self, b: bool) -> bool;
133155
fn to_bit<N: FromPrimitive>(self) -> N;
134156
}
135157

158+
#[cfg(stage0)]
136159
impl Bool for bool {
137160
#[inline]
138161
fn and(self, b: bool) -> bool { self && b }

src/libstd/char.rs

+55-5
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,61 @@ impl ToStr for char {
350350
}
351351
}
352352

353+
#[cfg(not(stage0), not(test))]
354+
#[lang = "char_impl"]
353355
#[allow(missing_doc)]
356+
/// The character primitive type in Rust.
357+
///
358+
/// A `char` represents a unicode codepoint, and is represented as a 32-bit
359+
/// value on all architectures. This type is not equivalent with the C `char`
360+
/// type because it is used to represent more than just ASCII.
361+
impl char {
362+
pub fn is_alphabetic(&self) -> bool { is_alphabetic(*self) }
363+
pub fn is_XID_start(&self) -> bool { is_XID_start(*self) }
364+
pub fn is_XID_continue(&self) -> bool { is_XID_continue(*self) }
365+
pub fn is_lowercase(&self) -> bool { is_lowercase(*self) }
366+
pub fn is_uppercase(&self) -> bool { is_uppercase(*self) }
367+
pub fn is_whitespace(&self) -> bool { is_whitespace(*self) }
368+
pub fn is_alphanumeric(&self) -> bool { is_alphanumeric(*self) }
369+
pub fn is_control(&self) -> bool { is_control(*self) }
370+
pub fn is_digit(&self) -> bool { is_digit(*self) }
371+
pub fn is_digit_radix(&self, radix: uint) -> bool { is_digit_radix(*self, radix) }
372+
pub fn to_digit(&self, radix: uint) -> Option<uint> { to_digit(*self, radix) }
373+
pub fn from_digit(num: uint, radix: uint) -> Option<char> { from_digit(num, radix) }
374+
pub fn escape_unicode(&self, f: |char|) { escape_unicode(*self, f) }
375+
pub fn escape_default(&self, f: |char|) { escape_default(*self, f) }
376+
pub fn len_utf8_bytes(&self) -> uint { len_utf8_bytes(*self) }
377+
378+
/// Encodes this character as utf-8 into the provided byte-buffer. The
379+
/// buffer must be at least 4 bytes long or a runtime failure will occur.
380+
///
381+
/// This will then return the number of characters written to the slice.
382+
pub fn encode_utf8<'a>(&self, dst: &'a mut [u8]) -> uint {
383+
let code = *self as uint;
384+
if code < MAX_ONE_B {
385+
dst[0] = code as u8;
386+
return 1;
387+
} else if code < MAX_TWO_B {
388+
dst[0] = (code >> 6u & 31u | TAG_TWO_B) as u8;
389+
dst[1] = (code & 63u | TAG_CONT) as u8;
390+
return 2;
391+
} else if code < MAX_THREE_B {
392+
dst[0] = (code >> 12u & 15u | TAG_THREE_B) as u8;
393+
dst[1] = (code >> 6u & 63u | TAG_CONT) as u8;
394+
dst[2] = (code & 63u | TAG_CONT) as u8;
395+
return 3;
396+
} else {
397+
dst[0] = (code >> 18u & 7u | TAG_FOUR_B) as u8;
398+
dst[1] = (code >> 12u & 63u | TAG_CONT) as u8;
399+
dst[2] = (code >> 6u & 63u | TAG_CONT) as u8;
400+
dst[3] = (code & 63u | TAG_CONT) as u8;
401+
return 4;
402+
}
403+
}
404+
}
405+
406+
#[allow(missing_doc)]
407+
#[cfg(stage0)]
354408
pub trait Char {
355409
fn is_alphabetic(&self) -> bool;
356410
fn is_XID_start(&self) -> bool;
@@ -367,14 +421,10 @@ pub trait Char {
367421
fn escape_unicode(&self, f: |char|);
368422
fn escape_default(&self, f: |char|);
369423
fn len_utf8_bytes(&self) -> uint;
370-
371-
/// Encodes this character as utf-8 into the provided byte-buffer. The
372-
/// buffer must be at least 4 bytes long or a runtime failure will occur.
373-
///
374-
/// This will then return the number of characters written to the slice.
375424
fn encode_utf8(&self, dst: &mut [u8]) -> uint;
376425
}
377426

427+
#[cfg(stage0)]
378428
impl Char for char {
379429
fn is_alphabetic(&self) -> bool { is_alphabetic(*self) }
380430

src/libstd/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ will look like `"\\{"`.
471471
use prelude::*;
472472

473473
use cast;
474-
use char::Char;
474+
#[cfg(stage0)] use char::Char;
475475
use io::MemWriter;
476476
use io;
477477
use str;

src/libstd/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ Out of scope
289289
#[allow(missing_doc)];
290290

291291
use cast;
292-
use char::Char;
292+
#[cfg(stage0)] use char::Char;
293293
use condition::Guard;
294294
use container::Container;
295295
use int;

src/libstd/num/f32.rs

+7
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ pub mod consts {
169169
pub static LN_10: f32 = 2.30258509299404568401799145468436421_f32;
170170
}
171171

172+
#[cfg(not(stage0), not(test))]
173+
#[lang = "f32_impl"]
174+
/// The `f32` primitive is an 32-bit floating point type. This is generally
175+
/// equivalent to the C `float` type.
176+
impl f32 {
177+
}
178+
172179
impl Num for f32 {}
173180

174181
#[cfg(not(test))]

src/libstd/num/f64.rs

+7
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ pub mod consts {
176176
pub static LN_10: f64 = 2.30258509299404568401799145468436421_f64;
177177
}
178178

179+
#[cfg(not(stage0), not(test))]
180+
#[lang = "f64_impl"]
181+
/// The `f64` primitive is an 32-bit floating point type. This is generally
182+
/// equivalent to the C `double` type.
183+
impl f64 {
184+
}
185+
179186
impl Num for f64 {}
180187

181188
#[cfg(not(test))]

src/libstd/num/i16.rs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ use unstable::intrinsics;
2424

2525
int_module!(i16, 16)
2626

27+
#[cfg(not(stage0), not(test))]
28+
#[lang = "i16_impl"]
29+
/// The `i16` primitive is a signed 16-bit integer type.
30+
impl i16 {
31+
}
32+
2733
impl Bitwise for i16 {
2834
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
2935
#[inline]

src/libstd/num/i32.rs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ use unstable::intrinsics;
2424

2525
int_module!(i32, 32)
2626

27+
#[cfg(not(stage0), not(test))]
28+
#[lang = "i32_impl"]
29+
/// The `i32` primitive is a signed 32-bit integer type.
30+
impl i32 {
31+
}
32+
2733
impl Bitwise for i32 {
2834
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
2935
#[inline]

src/libstd/num/i64.rs

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ use unstable::intrinsics;
2626

2727
int_module!(i64, 64)
2828

29+
#[cfg(not(stage0), not(test))]
30+
#[lang = "i64_impl"]
31+
/// The `i64` primitive is a signed 64-bit integer type.
32+
impl i64 {
33+
}
34+
2935
impl Bitwise for i64 {
3036
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
3137
#[inline]

src/libstd/num/i8.rs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ use unstable::intrinsics;
2424

2525
int_module!(i8, 8)
2626

27+
#[cfg(not(stage0), not(test))]
28+
#[lang = "i8_impl"]
29+
/// The `i8` primitive is a signed 8-bit integer type.
30+
impl i8 {
31+
}
32+
2733
impl Bitwise for i8 {
2834
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
2935
#[inline]

src/libstd/num/int.rs

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ use unstable::intrinsics;
2525
#[cfg(target_word_size = "32")] int_module!(int, 32)
2626
#[cfg(target_word_size = "64")] int_module!(int, 64)
2727

28+
#[cfg(not(stage0), not(test))]
29+
#[lang = "int_impl"]
30+
/// The `int` primitive type is an architecture-sized signed integer.
31+
///
32+
/// The size of a `int` is equivalent to the size of a `pointer` on the
33+
/// particular architecture in question.
34+
impl int {
35+
}
36+
2837
#[cfg(target_word_size = "32")]
2938
impl Bitwise for int {
3039
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.

src/libstd/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub trait Unsigned: Num {}
147147
/// ten.times(|| { accum += 1; })
148148
/// ```
149149
///
150+
#[cfg(stage0)]
150151
pub trait Times {
151152
fn times(&self, it: ||);
152153
}

src/libstd/num/u16.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use unstable::intrinsics;
2525

2626
uint_module!(u16, i16, 16)
2727

28+
#[cfg(not(stage0), not(test))]
29+
#[lang = "u16_impl"]
30+
/// The `u16` primitive is an unsigned 8-bit integer type.
31+
impl u16 {
32+
}
33+
2834
impl CheckedAdd for u16 {
2935
#[inline]
3036
fn checked_add(&self, v: &u16) -> Option<u16> {

src/libstd/num/u32.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use unstable::intrinsics;
2525

2626
uint_module!(u32, i32, 32)
2727

28+
#[cfg(not(stage0), not(test))]
29+
#[lang = "u32_impl"]
30+
/// The `u32` primitive is an unsigned 32-bit integer type.
31+
impl u32 {
32+
}
33+
2834
impl CheckedAdd for u32 {
2935
#[inline]
3036
fn checked_add(&self, v: &u32) -> Option<u32> {

src/libstd/num/u64.rs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ use unstable::intrinsics;
2727

2828
uint_module!(u64, i64, 64)
2929

30+
#[cfg(not(stage0), not(test))]
31+
#[lang = "u64_impl"]
32+
/// The `u64` primitive is an unsigned 64-bit integer type.
33+
impl u64 {
34+
}
35+
3036
impl CheckedAdd for u64 {
3137
#[inline]
3238
fn checked_add(&self, v: &u64) -> Option<u64> {

src/libstd/num/u8.rs

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ use unstable::intrinsics;
2525

2626
uint_module!(u8, i8, 8)
2727

28+
#[cfg(not(stage0), not(test))]
29+
#[lang = "u8_impl"]
30+
/// The `u8` primitive is an unsigned 8-bit integer type.
31+
impl u8 {
32+
}
33+
2834
impl CheckedAdd for u8 {
2935
#[inline]
3036
fn checked_add(&self, v: &u8) -> Option<u8> {

src/libstd/num/uint.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,40 @@ use num::{Bitwise, Bounded};
2020
use num::{CheckedAdd, CheckedSub, CheckedMul};
2121
use num::{CheckedDiv, Zero, One, strconv};
2222
use num::{ToStrRadix, FromStrRadix};
23-
use num;
23+
#[cfg(stage0)] use num;
2424
use option::{Option, Some, None};
2525
use str;
2626
use unstable::intrinsics;
2727

2828
uint_module!(uint, int, ::int::bits)
2929

30+
#[cfg(not(stage0), not(test))]
31+
#[lang = "uint_impl"]
32+
/// The `uint` primitive type is an architecture-sized unsigned integer.
33+
///
34+
/// The size of a `uint` is equivalent to the size of a `pointer` on the
35+
/// particular architecture in question.
36+
impl uint {
37+
///
38+
/// A convenience form for basic repetition. Given a uint `x`,
39+
/// `x.times(|| { ... })` executes the given block x times.
40+
///
41+
/// Equivalent to `for uint::range(0, x) |_| { ... }`.
42+
///
43+
/// Not defined on all integer types to permit unambiguous
44+
/// use with integer literals of inferred integer-type as
45+
/// the self-value (eg. `100.times(|| { ... })`).
46+
///
47+
#[inline]
48+
pub fn times(&self, it: ||) {
49+
let mut i = *self;
50+
while i > 0 {
51+
it();
52+
i -= 1;
53+
}
54+
}
55+
}
56+
3057
///
3158
/// Divide two numbers, return the result, rounded up.
3259
///
@@ -80,8 +107,8 @@ pub fn div_round(x: uint, y: uint) -> uint {
80107
///
81108
pub fn div_floor(x: uint, y: uint) -> uint { return x / y; }
82109

110+
#[cfg(stage0)]
83111
impl num::Times for uint {
84-
#[inline]
85112
///
86113
/// A convenience form for basic repetition. Given a uint `x`,
87114
/// `x.times(|| { ... })` executes the given block x times.
@@ -92,6 +119,7 @@ impl num::Times for uint {
92119
/// use with integer literals of inferred integer-type as
93120
/// the self-value (eg. `100.times(|| { ... })`).
94121
///
122+
#[inline]
95123
fn times(&self, it: ||) {
96124
let mut i = *self;
97125
while i > 0 {

0 commit comments

Comments
 (0)