From 1fe4bd0f433553eacf8d81cdbe16ac6fe6f16c54 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Fri, 16 Dec 2011 16:31:35 +0100 Subject: [PATCH 1/3] std: added either::flip, to_result and result::to_either --- src/libcore/either.rs | 25 +++++++++++++++++++++++++ src/libcore/result.rs | 13 ++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/libcore/either.rs b/src/libcore/either.rs index 89d47b20746a0..60ad12d2e026a 100644 --- a/src/libcore/either.rs +++ b/src/libcore/either.rs @@ -78,6 +78,31 @@ fn partition(eithers: [t]) ret {lefts: lefts, rights: rights}; } +/* +Function: flip + +Flips between left and right of a given either +*/ +pure fn flip(eith: t) -> t { + alt eith { + right(r) { left(r) } + left(l) { right(l) } + } +} + +/* +Function: to_result + +Converts either::t to a result::t, making the "right" choice +an ok result, and the "left" choice a fail +*/ +pure fn to_result(eith: t) -> result::t { + alt eith { + right(r) { result::ok(r) } + left(l) { result::err(l) } + } +} + // // Local Variables: // mode: rust diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 550f53470bbe8..84fbc3b7f90e0 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -42,7 +42,7 @@ fn get(res: t) -> T { ok(t) { t } err(_) { // FIXME: Serialize the error value - // and include it in the fail message + // and include it in the fail message (maybe just note it) fail "get called on error result"; } } @@ -71,7 +71,7 @@ Function: success Returns true if the result is */ -fn success(res: t) -> bool { +pure fn success(res: t) -> bool { alt res { ok(_) { true } err(_) { false } @@ -83,10 +83,17 @@ Function: failure Returns true if the result is */ -fn failure(res: t) -> bool { +pure fn failure(res: t) -> bool { !success(res) } +pure fn to_either(res: t) -> either::t { + alt res { + ok(res) { either::right(res) } + err(fail_) { either::left(fail_) } + } +} + /* Function: chain From bfbaadc694141935660fb73bbc1800389ede18e9 Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Fri, 16 Dec 2011 17:38:49 +0100 Subject: [PATCH 2/3] core: marked fns as pure where possible --- src/libcore/bool.rs | 2 +- src/libcore/box.rs | 2 +- src/libcore/char.rs | 4 ++-- src/libcore/option.rs | 4 ++-- src/libcore/str.rs | 8 ++++---- src/libcore/sys.rs | 2 +- src/libcore/vec.rs | 6 +++--- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs index 0fc869d1b41a9..af4b752f4dd1a 100644 --- a/src/libcore/bool.rs +++ b/src/libcore/bool.rs @@ -123,7 +123,7 @@ Returns: An u8 whose first bit is set if `if_true(v)` holds */ -fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } } +pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } } // Local Variables: // mode: rust; diff --git a/src/libcore/box.rs b/src/libcore/box.rs index 6682fe17284ac..23b5889bc6a4e 100644 --- a/src/libcore/box.rs +++ b/src/libcore/box.rs @@ -10,7 +10,7 @@ Function: ptr_eq Determine if two shared boxes point to the same object */ -fn ptr_eq(a: @T, b: @T) -> bool { +pure fn ptr_eq(a: @T, b: @T) -> bool { // FIXME: ptr::addr_of unsafe { let a_ptr: uint = unsafe::reinterpret_cast(a); diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 5ff7681ae8ab4..2648c8481dfa1 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -122,7 +122,7 @@ pure fn to_digit(c: char) -> u8 unsafe { Convert a char to the corresponding digit. Returns none when the character is not a valid hexadecimal digit. */ -fn maybe_digit(c: char) -> option::t { +pure fn maybe_digit(c: char) -> option::t { alt c { '0' to '9' { option::some(c as u8 - ('0' as u8)) } 'a' to 'z' { option::some(c as u8 + 10u8 - ('a' as u8)) } @@ -143,7 +143,7 @@ fn maybe_digit(c: char) -> option::t { Returns: -1 if ab */ -fn cmp(a: char, b: char) -> int { +pure fn cmp(a: char, b: char) -> int { ret if b > a { -1 } else if b < a { 1 } else { 0 } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 403cb47f47e64..39e68a01519bf 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -30,7 +30,7 @@ Failure: Fails if the value equals `none`. */ -fn get(opt: t) -> T { +pure fn get(opt: t) -> T { alt opt { some(x) { ret x; } none. { fail "option none"; } } } @@ -61,7 +61,7 @@ Function: from_maybe Returns the contained value or a default */ -fn from_maybe(def: T, opt: t) -> T { +pure fn from_maybe(def: T, opt: t) -> T { alt opt { some(x) { x } none. { def } } } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 8a532fcc75323..802297223e9bc 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -26,14 +26,14 @@ Function: eq Bytewise string equality */ -fn eq(&&a: str, &&b: str) -> bool { a == b } +pure fn eq(&&a: str, &&b: str) -> bool { a == b } /* Function: lteq Bytewise less than or equal */ -fn lteq(&&a: str, &&b: str) -> bool { a <= b } +pure fn lteq(&&a: str, &&b: str) -> bool { a <= b } /* Function: hash @@ -131,7 +131,7 @@ Function: byte_len Returns the length in bytes of a string */ -fn byte_len(s: str) -> uint unsafe { +pure fn byte_len(s: str) -> uint unsafe { let v: [u8] = unsafe::reinterpret_cast(s); let vlen = vec::len(v); unsafe::leak(v); @@ -261,7 +261,7 @@ Function: utf8_char_width FIXME: What does this function do? */ -fn utf8_char_width(b: u8) -> uint { +pure fn utf8_char_width(b: u8) -> uint { let byte: uint = b as uint; if byte < 128u { ret 1u; } if byte < 192u { diff --git a/src/libcore/sys.rs b/src/libcore/sys.rs index 3b4a3b8c64378..9c8b097211173 100644 --- a/src/libcore/sys.rs +++ b/src/libcore/sys.rs @@ -42,7 +42,7 @@ Function: last_os_error Get a string representing the platform-dependent last error */ fn last_os_error() -> str { - ret rustrt::last_os_error(); + rustrt::last_os_error() } /* diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index d8f89b3b27bc9..d056eb8d04dcc 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -181,7 +181,7 @@ Returns the first element of a vector Predicates: (v) */ -fn head(v: [const T]) : is_not_empty(v) -> T { ret v[0]; } +pure fn head(v: [const T]) : is_not_empty(v) -> T { ret v[0]; } /* Function: tail @@ -221,7 +221,7 @@ Returns: An option containing the last element of `v` if `v` is not empty, or none if `v` is empty. */ -fn last(v: [const T]) -> option::t { +pure fn last(v: [const T]) -> option::t { if len(v) == 0u { ret none; } ret some(v[len(v) - 1u]); } @@ -234,7 +234,7 @@ Returns the last element of a non-empty vector `v` Predicates: (v) */ -fn last_total(v: [const T]) : is_not_empty(v) -> T { +pure fn last_total(v: [const T]) : is_not_empty(v) -> T { ret v[len(v) - 1u]; } From 7d786318a1b0f9315164716b46306d68e3b9560f Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Fri, 16 Dec 2011 18:18:34 +0100 Subject: [PATCH 3/3] std: declared fns as pure where sensible --- src/libstd/bitv.rs | 2 +- src/libstd/list.rs | 6 +++--- src/libstd/posix_fs.rs | 1 + src/libstd/rope.rs | 2 +- src/libstd/unicode.rs | 6 +++--- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/libstd/bitv.rs b/src/libstd/bitv.rs index 4b7267030856b..a8e6d98b3cf2b 100644 --- a/src/libstd/bitv.rs +++ b/src/libstd/bitv.rs @@ -128,7 +128,7 @@ Function: get Retreive the value at index `i` */ -fn get(v: t, i: uint) -> bool { +pure fn get(v: t, i: uint) -> bool { assert (i < v.nbits); let bits = uint_bits; let w = i / bits; diff --git a/src/libstd/list.rs b/src/libstd/list.rs index d0c7582426a48..67025cc90ac80 100644 --- a/src/libstd/list.rs +++ b/src/libstd/list.rs @@ -112,7 +112,7 @@ Function: tail Returns all but the first element of a list */ -fn tail(ls: list) -> list { +pure fn tail(ls: list) -> list { alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } } } @@ -121,7 +121,7 @@ Function: head Returns the first element of a list */ -fn head(ls: list) -> T { +pure fn head(ls: list) -> T { alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } } } @@ -130,7 +130,7 @@ Function: append Appends one list to another */ -fn append(l: list, m: list) -> list { +pure fn append(l: list, m: list) -> list { alt l { nil. { ret m; } cons(x, xs) { let rest = append(*xs, m); ret cons(x, @rest); } diff --git a/src/libstd/posix_fs.rs b/src/libstd/posix_fs.rs index befa4c9829c61..e774650f980b5 100644 --- a/src/libstd/posix_fs.rs +++ b/src/libstd/posix_fs.rs @@ -30,6 +30,7 @@ fn list_dir(path: str) -> [str] { } +// FIXME make pure when str::char_at is fn path_is_absolute(p: str) -> bool { ret str::char_at(p, 0u) == '/'; } const path_sep: char = '/'; diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs index 2872aa6c4f617..bd07b6a8bc7e6 100644 --- a/src/libstd/rope.rs +++ b/src/libstd/rope.rs @@ -310,7 +310,7 @@ Returns: A negative value if `left < right`, 0 if eq(left, right) or a positive value if `left > right` - */ +*/ fn cmp(left: rope, right: rope) -> int { alt((left, right)) { (node::empty., node::empty.) { ret 0; } diff --git a/src/libstd/unicode.rs b/src/libstd/unicode.rs index 5038721d26e54..a3d11cbc55b23 100644 --- a/src/libstd/unicode.rs +++ b/src/libstd/unicode.rs @@ -151,16 +151,16 @@ mod icu { #[link_name = "icuuc"] #[abi = "cdecl"] native mod libicu { - fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool; + pure fn u_hasBinaryProperty(c: UChar32, which: UProperty) -> UBool; } } -fn is_XID_start(c: char) -> bool { +pure fn is_XID_start(c: char) -> bool { ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START) == icu::TRUE; } -fn is_XID_continue(c: char) -> bool { +pure fn is_XID_continue(c: char) -> bool { ret icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START) == icu::TRUE; }