Skip to content

Commit dbfa1b5

Browse files
committed
Merge pull request #1317 from boggle/fix1315
fix to #1315 + small additions to std::either and result
2 parents 1bf078f + 7d78631 commit dbfa1b5

File tree

14 files changed

+58
-25
lines changed

14 files changed

+58
-25
lines changed

src/libcore/bool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Returns:
123123
124124
An u8 whose first bit is set if `if_true(v)` holds
125125
*/
126-
fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
126+
pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
127127

128128
// Local Variables:
129129
// mode: rust;

src/libcore/box.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Function: ptr_eq
1010
1111
Determine if two shared boxes point to the same object
1212
*/
13-
fn ptr_eq<T>(a: @T, b: @T) -> bool {
13+
pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
1414
// FIXME: ptr::addr_of
1515
unsafe {
1616
let a_ptr: uint = unsafe::reinterpret_cast(a);

src/libcore/char.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pure fn to_digit(c: char) -> u8 unsafe {
122122
Convert a char to the corresponding digit. Returns none when the
123123
character is not a valid hexadecimal digit.
124124
*/
125-
fn maybe_digit(c: char) -> option::t<u8> {
125+
pure fn maybe_digit(c: char) -> option::t<u8> {
126126
alt c {
127127
'0' to '9' { option::some(c as u8 - ('0' as u8)) }
128128
'a' to 'z' { option::some(c as u8 + 10u8 - ('a' as u8)) }
@@ -143,7 +143,7 @@ fn maybe_digit(c: char) -> option::t<u8> {
143143
Returns:
144144
-1 if a<b, 0 if a==b, +1 if a>b
145145
*/
146-
fn cmp(a: char, b: char) -> int {
146+
pure fn cmp(a: char, b: char) -> int {
147147
ret if b > a { -1 }
148148
else if b < a { 1 }
149149
else { 0 }

src/libcore/either.rs

+25
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ fn partition<copy T, copy U>(eithers: [t<T, U>])
7878
ret {lefts: lefts, rights: rights};
7979
}
8080

81+
/*
82+
Function: flip
83+
84+
Flips between left and right of a given either
85+
*/
86+
pure fn flip<copy T, copy U>(eith: t<T, U>) -> t<U, T> {
87+
alt eith {
88+
right(r) { left(r) }
89+
left(l) { right(l) }
90+
}
91+
}
92+
93+
/*
94+
Function: to_result
95+
96+
Converts either::t to a result::t, making the "right" choice
97+
an ok result, and the "left" choice a fail
98+
*/
99+
pure fn to_result<copy T, copy U>(eith: t<T, U>) -> result::t<U, T> {
100+
alt eith {
101+
right(r) { result::ok(r) }
102+
left(l) { result::err(l) }
103+
}
104+
}
105+
81106
//
82107
// Local Variables:
83108
// mode: rust

src/libcore/option.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Failure:
3030
3131
Fails if the value equals `none`.
3232
*/
33-
fn get<copy T>(opt: t<T>) -> T {
33+
pure fn get<copy T>(opt: t<T>) -> T {
3434
alt opt { some(x) { ret x; } none. { fail "option none"; } }
3535
}
3636

@@ -61,7 +61,7 @@ Function: from_maybe
6161
6262
Returns the contained value or a default
6363
*/
64-
fn from_maybe<T>(def: T, opt: t<T>) -> T {
64+
pure fn from_maybe<T>(def: T, opt: t<T>) -> T {
6565
alt opt { some(x) { x } none. { def } }
6666
}
6767

src/libcore/result.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn get<T, U>(res: t<T, U>) -> T {
4242
ok(t) { t }
4343
err(_) {
4444
// FIXME: Serialize the error value
45-
// and include it in the fail message
45+
// and include it in the fail message (maybe just note it)
4646
fail "get called on error result";
4747
}
4848
}
@@ -71,7 +71,7 @@ Function: success
7171
7272
Returns true if the result is <ok>
7373
*/
74-
fn success<T, U>(res: t<T, U>) -> bool {
74+
pure fn success<T, U>(res: t<T, U>) -> bool {
7575
alt res {
7676
ok(_) { true }
7777
err(_) { false }
@@ -83,10 +83,17 @@ Function: failure
8383
8484
Returns true if the result is <error>
8585
*/
86-
fn failure<T, U>(res: t<T, U>) -> bool {
86+
pure fn failure<T, U>(res: t<T, U>) -> bool {
8787
!success(res)
8888
}
8989

90+
pure fn to_either<copy T, copy U>(res: t<U, T>) -> either::t<T, U> {
91+
alt res {
92+
ok(res) { either::right(res) }
93+
err(fail_) { either::left(fail_) }
94+
}
95+
}
96+
9097
/*
9198
Function: chain
9299

src/libcore/str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Function: eq
2626
2727
Bytewise string equality
2828
*/
29-
fn eq(&&a: str, &&b: str) -> bool { a == b }
29+
pure fn eq(&&a: str, &&b: str) -> bool { a == b }
3030

3131
/*
3232
Function: lteq
3333
3434
Bytewise less than or equal
3535
*/
36-
fn lteq(&&a: str, &&b: str) -> bool { a <= b }
36+
pure fn lteq(&&a: str, &&b: str) -> bool { a <= b }
3737

3838
/*
3939
Function: hash
@@ -131,7 +131,7 @@ Function: byte_len
131131
132132
Returns the length in bytes of a string
133133
*/
134-
fn byte_len(s: str) -> uint unsafe {
134+
pure fn byte_len(s: str) -> uint unsafe {
135135
let v: [u8] = unsafe::reinterpret_cast(s);
136136
let vlen = vec::len(v);
137137
unsafe::leak(v);
@@ -261,7 +261,7 @@ Function: utf8_char_width
261261
262262
FIXME: What does this function do?
263263
*/
264-
fn utf8_char_width(b: u8) -> uint {
264+
pure fn utf8_char_width(b: u8) -> uint {
265265
let byte: uint = b as uint;
266266
if byte < 128u { ret 1u; }
267267
if byte < 192u {

src/libcore/sys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Function: last_os_error
4242
Get a string representing the platform-dependent last error
4343
*/
4444
fn last_os_error() -> str {
45-
ret rustrt::last_os_error();
45+
rustrt::last_os_error()
4646
}
4747

4848
/*

src/libcore/vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Returns the first element of a vector
181181
Predicates:
182182
<is_not_empty> (v)
183183
*/
184-
fn head<copy T>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; }
184+
pure fn head<copy T>(v: [const T]) : is_not_empty(v) -> T { ret v[0]; }
185185

186186
/*
187187
Function: tail
@@ -221,7 +221,7 @@ Returns:
221221
An option containing the last element of `v` if `v` is not empty, or
222222
none if `v` is empty.
223223
*/
224-
fn last<copy T>(v: [const T]) -> option::t<T> {
224+
pure fn last<copy T>(v: [const T]) -> option::t<T> {
225225
if len(v) == 0u { ret none; }
226226
ret some(v[len(v) - 1u]);
227227
}
@@ -234,7 +234,7 @@ Returns the last element of a non-empty vector `v`
234234
Predicates:
235235
<is_not_empty> (v)
236236
*/
237-
fn last_total<copy T>(v: [const T]) : is_not_empty(v) -> T {
237+
pure fn last_total<copy T>(v: [const T]) : is_not_empty(v) -> T {
238238
ret v[len(v) - 1u];
239239
}
240240

src/libstd/bitv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Function: get
128128
129129
Retreive the value at index `i`
130130
*/
131-
fn get(v: t, i: uint) -> bool {
131+
pure fn get(v: t, i: uint) -> bool {
132132
assert (i < v.nbits);
133133
let bits = uint_bits;
134134
let w = i / bits;

src/libstd/list.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Function: tail
112112
113113
Returns all but the first element of a list
114114
*/
115-
fn tail<copy T>(ls: list<T>) -> list<T> {
115+
pure fn tail<copy T>(ls: list<T>) -> list<T> {
116116
alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } }
117117
}
118118

@@ -121,7 +121,7 @@ Function: head
121121
122122
Returns the first element of a list
123123
*/
124-
fn head<copy T>(ls: list<T>) -> T {
124+
pure fn head<copy T>(ls: list<T>) -> T {
125125
alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } }
126126
}
127127

@@ -130,7 +130,7 @@ Function: append
130130
131131
Appends one list to another
132132
*/
133-
fn append<copy T>(l: list<T>, m: list<T>) -> list<T> {
133+
pure fn append<copy T>(l: list<T>, m: list<T>) -> list<T> {
134134
alt l {
135135
nil. { ret m; }
136136
cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); }

src/libstd/posix_fs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fn list_dir(path: str) -> [str] {
3030

3131
}
3232

33+
// FIXME make pure when str::char_at is
3334
fn path_is_absolute(p: str) -> bool { ret str::char_at(p, 0u) == '/'; }
3435

3536
const path_sep: char = '/';

src/libstd/rope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Returns:
310310
311311
A negative value if `left < right`, 0 if eq(left, right) or a positive
312312
value if `left > right`
313-
*/
313+
*/
314314
fn cmp(left: rope, right: rope) -> int {
315315
alt((left, right)) {
316316
(node::empty., node::empty.) { ret 0; }

src/libstd/unicode.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ mod icu {
151151
#[link_name = "icuuc"]
152152
#[abi = "cdecl"]
153153
native mod libicu {
154-
fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool;
154+
pure fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool;
155155
}
156156
}
157157

158-
fn is_XID_start(c: char) -> bool {
158+
pure fn is_XID_start(c: char) -> bool {
159159
ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
160160
== icu::TRUE;
161161
}
162162

163-
fn is_XID_continue(c: char) -> bool {
163+
pure fn is_XID_continue(c: char) -> bool {
164164
ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
165165
== icu::TRUE;
166166
}

0 commit comments

Comments
 (0)