De-export iter and result. Part of #3583.
This commit is contained in:
parent
24fbf88230
commit
39f114d171
3 changed files with 62 additions and 53 deletions
|
@ -185,7 +185,6 @@ mod cmp;
|
||||||
mod num;
|
mod num;
|
||||||
mod hash;
|
mod hash;
|
||||||
mod either;
|
mod either;
|
||||||
#[legacy_exports]
|
|
||||||
mod iter;
|
mod iter;
|
||||||
mod logging;
|
mod logging;
|
||||||
mod option;
|
mod option;
|
||||||
|
@ -194,7 +193,6 @@ mod option_iter {
|
||||||
#[path = "option.rs"]
|
#[path = "option.rs"]
|
||||||
mod inst;
|
mod inst;
|
||||||
}
|
}
|
||||||
#[legacy_exports]
|
|
||||||
mod result;
|
mod result;
|
||||||
mod to_str;
|
mod to_str;
|
||||||
mod to_bytes;
|
mod to_bytes;
|
||||||
|
|
|
@ -7,14 +7,14 @@ The iteration traits and common implementation
|
||||||
use cmp::{Eq, Ord};
|
use cmp::{Eq, Ord};
|
||||||
|
|
||||||
/// A function used to initialize the elements of a sequence
|
/// A function used to initialize the elements of a sequence
|
||||||
type InitOp<T> = &fn(uint) -> T;
|
pub type InitOp<T> = &fn(uint) -> T;
|
||||||
|
|
||||||
trait BaseIter<A> {
|
pub trait BaseIter<A> {
|
||||||
pure fn each(blk: fn(v: &A) -> bool);
|
pure fn each(blk: fn(v: &A) -> bool);
|
||||||
pure fn size_hint() -> Option<uint>;
|
pure fn size_hint() -> Option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait ExtendedIter<A> {
|
pub trait ExtendedIter<A> {
|
||||||
pure fn eachi(blk: fn(uint, v: &A) -> bool);
|
pure fn eachi(blk: fn(uint, v: &A) -> bool);
|
||||||
pure fn all(blk: fn(&A) -> bool) -> bool;
|
pure fn all(blk: fn(&A) -> bool) -> bool;
|
||||||
pure fn any(blk: fn(&A) -> bool) -> bool;
|
pure fn any(blk: fn(&A) -> bool) -> bool;
|
||||||
|
@ -22,33 +22,34 @@ trait ExtendedIter<A> {
|
||||||
pure fn position(f: fn(&A) -> bool) -> Option<uint>;
|
pure fn position(f: fn(&A) -> bool) -> Option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait EqIter<A:Eq> {
|
pub trait EqIter<A:Eq> {
|
||||||
pure fn contains(x: &A) -> bool;
|
pure fn contains(x: &A) -> bool;
|
||||||
pure fn count(x: &A) -> uint;
|
pure fn count(x: &A) -> uint;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Times {
|
pub trait Times {
|
||||||
pure fn times(it: fn() -> bool);
|
pure fn times(it: fn() -> bool);
|
||||||
}
|
}
|
||||||
trait TimesIx{
|
|
||||||
|
pub trait TimesIx{
|
||||||
pure fn timesi(it: fn(uint) -> bool);
|
pure fn timesi(it: fn(uint) -> bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CopyableIter<A:Copy> {
|
pub trait CopyableIter<A:Copy> {
|
||||||
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A];
|
pure fn filter_to_vec(pred: fn(+a: A) -> bool) -> ~[A];
|
||||||
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B];
|
pure fn map_to_vec<B>(op: fn(+v: A) -> B) -> ~[B];
|
||||||
pure fn to_vec() -> ~[A];
|
pure fn to_vec() -> ~[A];
|
||||||
pure fn find(p: fn(+a: A) -> bool) -> Option<A>;
|
pure fn find(p: fn(+a: A) -> bool) -> Option<A>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CopyableOrderedIter<A:Copy Ord> {
|
pub trait CopyableOrderedIter<A:Copy Ord> {
|
||||||
pure fn min() -> A;
|
pure fn min() -> A;
|
||||||
pure fn max() -> A;
|
pure fn max() -> A;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A trait for sequences that can be by imperatively pushing elements
|
// A trait for sequences that can be by imperatively pushing elements
|
||||||
// onto them.
|
// onto them.
|
||||||
trait Buildable<A> {
|
pub trait Buildable<A> {
|
||||||
/**
|
/**
|
||||||
* Builds a buildable sequence by calling a provided function with
|
* Builds a buildable sequence by calling a provided function with
|
||||||
* an argument function that pushes an element onto the back of
|
* an argument function that pushes an element onto the back of
|
||||||
|
@ -66,7 +67,8 @@ trait Buildable<A> {
|
||||||
builder: fn(push: pure fn(+v: A))) -> self;
|
builder: fn(push: pure fn(+v: A))) -> self;
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn eachi<A,IA:BaseIter<A>>(self: &IA, blk: fn(uint, v: &A) -> bool) {
|
pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
|
||||||
|
blk: fn(uint, v: &A) -> bool) {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if !blk(i, a) { break; }
|
if !blk(i, a) { break; }
|
||||||
|
@ -74,22 +76,24 @@ pure fn eachi<A,IA:BaseIter<A>>(self: &IA, blk: fn(uint, v: &A) -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn all<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
|
pub pure fn all<A,IA:BaseIter<A>>(self: &IA,
|
||||||
|
blk: fn(&A) -> bool) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if !blk(a) { return false; }
|
if !blk(a) { return false; }
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn any<A,IA:BaseIter<A>>(self: &IA, blk: fn(&A) -> bool) -> bool {
|
pub pure fn any<A,IA:BaseIter<A>>(self: &IA,
|
||||||
|
blk: fn(&A) -> bool) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if blk(a) { return true; }
|
if blk(a) { return true; }
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: &IA,
|
pub pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(
|
||||||
prd: fn(+a: A) -> bool) -> ~[A] {
|
self: &IA, prd: fn(+a: A) -> bool) -> ~[A] {
|
||||||
do vec::build_sized_opt(self.size_hint()) |push| {
|
do vec::build_sized_opt(self.size_hint()) |push| {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if prd(*a) { push(*a); }
|
if prd(*a) { push(*a); }
|
||||||
|
@ -97,7 +101,8 @@ pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: &IA,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA, op: fn(+v: A) -> B)
|
pub pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA,
|
||||||
|
op: fn(+v: A) -> B)
|
||||||
-> ~[B] {
|
-> ~[B] {
|
||||||
do vec::build_sized_opt(self.size_hint()) |push| {
|
do vec::build_sized_opt(self.size_hint()) |push| {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
|
@ -106,7 +111,7 @@ pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: &IA, op: fn(+v: A) -> B)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
pub pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
||||||
self: &IA, op: fn(+a: A) -> IB) -> ~[B] {
|
self: &IA, op: fn(+a: A) -> IB) -> ~[B] {
|
||||||
|
|
||||||
do vec::build |push| {
|
do vec::build |push| {
|
||||||
|
@ -118,7 +123,8 @@ pure fn flat_map_to_vec<A:Copy,B:Copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
|
pub pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B,
|
||||||
|
blk: fn(&B, &A) -> B)
|
||||||
-> B {
|
-> B {
|
||||||
let mut b <- b0;
|
let mut b <- b0;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
|
@ -127,18 +133,18 @@ pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, +b0: B, blk: fn(&B, &A) -> B)
|
||||||
move b
|
move b
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
|
pub pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
|
||||||
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy (*r), ~[*a]))
|
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy (*r), ~[*a]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
|
pub pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if *a == *x { return true; }
|
if *a == *x { return true; }
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
pub pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
||||||
do foldl(self, 0) |count, value| {
|
do foldl(self, 0) |count, value| {
|
||||||
if *value == *x {
|
if *value == *x {
|
||||||
*count + 1
|
*count + 1
|
||||||
|
@ -148,7 +154,7 @@ pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
|
pub pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
|
||||||
-> Option<uint>
|
-> Option<uint>
|
||||||
{
|
{
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
|
@ -163,7 +169,7 @@ pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
|
||||||
// iter interface, such as would provide "reach" in addition to "each". as is,
|
// iter interface, such as would provide "reach" in addition to "each". as is,
|
||||||
// it would have to be implemented with foldr, which is too inefficient.
|
// it would have to be implemented with foldr, which is too inefficient.
|
||||||
|
|
||||||
pure fn repeat(times: uint, blk: fn() -> bool) {
|
pub pure fn repeat(times: uint, blk: fn() -> bool) {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < times {
|
while i < times {
|
||||||
if !blk() { break }
|
if !blk() { break }
|
||||||
|
@ -171,7 +177,7 @@ pure fn repeat(times: uint, blk: fn() -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
pub pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
&Some(ref a_) if *a_ < *b => {
|
&Some(ref a_) if *a_ < *b => {
|
||||||
|
@ -185,7 +191,7 @@ pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
&Some(ref a_) if *a_ > *b => {
|
&Some(ref a_) if *a_ > *b => {
|
||||||
|
@ -199,7 +205,7 @@ pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
|
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
|
||||||
p: fn(+a: A) -> bool) -> Option<A> {
|
p: fn(+a: A) -> bool) -> Option<A> {
|
||||||
for self.each |i| {
|
for self.each |i| {
|
||||||
if p(*i) { return Some(*i) }
|
if p(*i) { return Some(*i) }
|
||||||
|
@ -220,7 +226,8 @@ pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
|
||||||
* onto the sequence being constructed.
|
* onto the sequence being constructed.
|
||||||
*/
|
*/
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A))) -> B {
|
pub pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A)))
|
||||||
|
-> B {
|
||||||
build_sized(4, builder)
|
build_sized(4, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,7 +245,7 @@ pure fn build<A,B: Buildable<A>>(builder: fn(push: pure fn(+v: A))) -> B {
|
||||||
* onto the sequence being constructed.
|
* onto the sequence being constructed.
|
||||||
*/
|
*/
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn build_sized_opt<A,B: Buildable<A>>(
|
pub pure fn build_sized_opt<A,B: Buildable<A>>(
|
||||||
size: Option<uint>,
|
size: Option<uint>,
|
||||||
builder: fn(push: pure fn(+v: A))) -> B {
|
builder: fn(push: pure fn(+v: A))) -> B {
|
||||||
|
|
||||||
|
@ -248,7 +255,8 @@ pure fn build_sized_opt<A,B: Buildable<A>>(
|
||||||
// Functions that combine iteration and building
|
// Functions that combine iteration and building
|
||||||
|
|
||||||
/// Apply a function to each element of an iterable and return the results
|
/// Apply a function to each element of an iterable and return the results
|
||||||
fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U) -> BU {
|
pub fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U)
|
||||||
|
-> BU {
|
||||||
do build_sized_opt(v.size_hint()) |push| {
|
do build_sized_opt(v.size_hint()) |push| {
|
||||||
for v.each() |elem| {
|
for v.each() |elem| {
|
||||||
push(f(elem));
|
push(f(elem));
|
||||||
|
@ -262,7 +270,8 @@ fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U) -> BU {
|
||||||
* Creates a generic sequence of size `n_elts` and initializes the elements
|
* Creates a generic sequence of size `n_elts` and initializes the elements
|
||||||
* to the value returned by the function `op`.
|
* to the value returned by the function `op`.
|
||||||
*/
|
*/
|
||||||
pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint, op: InitOp<T>) -> BT {
|
pub pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint,
|
||||||
|
op: InitOp<T>) -> BT {
|
||||||
do build_sized(n_elts) |push| {
|
do build_sized(n_elts) |push| {
|
||||||
let mut i: uint = 0u;
|
let mut i: uint = 0u;
|
||||||
while i < n_elts { push(op(i)); i += 1u; }
|
while i < n_elts { push(op(i)); i += 1u; }
|
||||||
|
@ -275,7 +284,8 @@ pure fn from_fn<T,BT: Buildable<T>>(n_elts: uint, op: InitOp<T>) -> BT {
|
||||||
* Creates an immutable vector of size `n_elts` and initializes the elements
|
* Creates an immutable vector of size `n_elts` and initializes the elements
|
||||||
* to the value `t`.
|
* to the value `t`.
|
||||||
*/
|
*/
|
||||||
pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint, +t: T) -> BT {
|
pub pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint,
|
||||||
|
+t: T) -> BT {
|
||||||
do build_sized(n_elts) |push| {
|
do build_sized(n_elts) |push| {
|
||||||
let mut i: uint = 0;
|
let mut i: uint = 0;
|
||||||
while i < n_elts { push(t); i += 1; }
|
while i < n_elts { push(t); i += 1; }
|
||||||
|
@ -284,7 +294,7 @@ pure fn from_elem<T: Copy,BT: Buildable<T>>(n_elts: uint, +t: T) -> BT {
|
||||||
|
|
||||||
/// Appending two generic sequences
|
/// Appending two generic sequences
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
|
pub pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
|
||||||
lhs: &IT, rhs: &IT) -> BT {
|
lhs: &IT, rhs: &IT) -> BT {
|
||||||
let size_opt = lhs.size_hint().chain_ref(
|
let size_opt = lhs.size_hint().chain_ref(
|
||||||
|sz1| rhs.size_hint().map(|sz2| *sz1+*sz2));
|
|sz1| rhs.size_hint().map(|sz2| *sz1+*sz2));
|
||||||
|
@ -297,7 +307,7 @@ pure fn append<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
|
||||||
/// Copies a generic sequence, possibly converting it to a different
|
/// Copies a generic sequence, possibly converting it to a different
|
||||||
/// type of sequence.
|
/// type of sequence.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn copy_seq<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
|
pub pure fn copy_seq<T: Copy,IT: BaseIter<T>,BT: Buildable<T>>(
|
||||||
v: &IT) -> BT {
|
v: &IT) -> BT {
|
||||||
do build_sized_opt(v.size_hint()) |push| {
|
do build_sized_opt(v.size_hint()) |push| {
|
||||||
for v.each |x| { push(*x); }
|
for v.each |x| { push(*x); }
|
||||||
|
|
|
@ -8,7 +8,7 @@ use cmp::Eq;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
|
|
||||||
/// The result type
|
/// The result type
|
||||||
enum Result<T, U> {
|
pub enum Result<T, U> {
|
||||||
/// Contains the successful result value
|
/// Contains the successful result value
|
||||||
Ok(T),
|
Ok(T),
|
||||||
/// Contains the error value
|
/// Contains the error value
|
||||||
|
@ -22,7 +22,7 @@ enum Result<T, U> {
|
||||||
*
|
*
|
||||||
* If the result is an error
|
* If the result is an error
|
||||||
*/
|
*/
|
||||||
pure fn get<T: Copy, U>(res: &Result<T, U>) -> T {
|
pub pure fn get<T: Copy, U>(res: &Result<T, U>) -> T {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(copy t) => t,
|
Ok(copy t) => t,
|
||||||
Err(ref the_err) => unsafe {
|
Err(ref the_err) => unsafe {
|
||||||
|
@ -38,7 +38,7 @@ pure fn get<T: Copy, U>(res: &Result<T, U>) -> T {
|
||||||
*
|
*
|
||||||
* If the result is an error
|
* If the result is an error
|
||||||
*/
|
*/
|
||||||
pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T {
|
pub pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(ref t) => t,
|
Ok(ref t) => t,
|
||||||
Err(ref the_err) => unsafe {
|
Err(ref the_err) => unsafe {
|
||||||
|
@ -54,7 +54,7 @@ pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T {
|
||||||
*
|
*
|
||||||
* If the result is not an error
|
* If the result is not an error
|
||||||
*/
|
*/
|
||||||
pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U {
|
pub pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U {
|
||||||
match *res {
|
match *res {
|
||||||
Err(copy u) => u,
|
Err(copy u) => u,
|
||||||
Ok(_) => fail ~"get_err called on ok result"
|
Ok(_) => fail ~"get_err called on ok result"
|
||||||
|
@ -62,7 +62,7 @@ pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the result is `ok`
|
/// Returns true if the result is `ok`
|
||||||
pure fn is_ok<T, U>(res: &Result<T, U>) -> bool {
|
pub pure fn is_ok<T, U>(res: &Result<T, U>) -> bool {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(_) => false
|
Err(_) => false
|
||||||
|
@ -70,7 +70,7 @@ pure fn is_ok<T, U>(res: &Result<T, U>) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the result is `err`
|
/// Returns true if the result is `err`
|
||||||
pure fn is_err<T, U>(res: &Result<T, U>) -> bool {
|
pub pure fn is_err<T, U>(res: &Result<T, U>) -> bool {
|
||||||
!is_ok(res)
|
!is_ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,8 @@ pure fn is_err<T, U>(res: &Result<T, U>) -> bool {
|
||||||
* `ok` result variants are converted to `either::right` variants, `err`
|
* `ok` result variants are converted to `either::right` variants, `err`
|
||||||
* result variants are converted to `either::left`.
|
* result variants are converted to `either::left`.
|
||||||
*/
|
*/
|
||||||
pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>) -> Either<T, U> {
|
pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
|
||||||
|
-> Either<T, U> {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(copy res) => either::Right(res),
|
Ok(copy res) => either::Right(res),
|
||||||
Err(copy fail_) => either::Left(fail_)
|
Err(copy fail_) => either::Left(fail_)
|
||||||
|
@ -101,7 +102,7 @@ pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>) -> Either<T, U> {
|
||||||
* ok(parse_bytes(buf))
|
* ok(parse_bytes(buf))
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
fn chain<T, U: Copy, V: Copy>(+res: Result<T, V>, op: fn(+t: T)
|
pub fn chain<T, U: Copy, V: Copy>(+res: Result<T, V>, op: fn(+t: T)
|
||||||
-> Result<U, V>) -> Result<U, V> {
|
-> Result<U, V>) -> Result<U, V> {
|
||||||
// XXX: Should be writable with move + match
|
// XXX: Should be writable with move + match
|
||||||
if res.is_ok() {
|
if res.is_ok() {
|
||||||
|
@ -119,7 +120,7 @@ fn chain<T, U: Copy, V: Copy>(+res: Result<T, V>, op: fn(+t: T)
|
||||||
* immediately returned. This function can be used to pass through a
|
* immediately returned. This function can be used to pass through a
|
||||||
* successful result while handling an error.
|
* successful result while handling an error.
|
||||||
*/
|
*/
|
||||||
fn chain_err<T: Copy, U: Copy, V: Copy>(
|
pub fn chain_err<T: Copy, U: Copy, V: Copy>(
|
||||||
+res: Result<T, V>,
|
+res: Result<T, V>,
|
||||||
op: fn(+t: V) -> Result<T, U>)
|
op: fn(+t: V) -> Result<T, U>)
|
||||||
-> Result<T, U> {
|
-> Result<T, U> {
|
||||||
|
@ -143,7 +144,7 @@ fn chain_err<T: Copy, U: Copy, V: Copy>(
|
||||||
* print_buf(buf)
|
* print_buf(buf)
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
fn iter<T, E>(res: &Result<T, E>, f: fn((&T))) {
|
pub fn iter<T, E>(res: &Result<T, E>, f: fn((&T))) {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(ref t) => f(t),
|
Ok(ref t) => f(t),
|
||||||
Err(_) => ()
|
Err(_) => ()
|
||||||
|
@ -158,7 +159,7 @@ fn iter<T, E>(res: &Result<T, E>, f: fn((&T))) {
|
||||||
* This function can be used to pass through a successful result while
|
* This function can be used to pass through a successful result while
|
||||||
* handling an error.
|
* handling an error.
|
||||||
*/
|
*/
|
||||||
fn iter_err<T, E>(res: &Result<T, E>, f: fn((&E))) {
|
pub fn iter_err<T, E>(res: &Result<T, E>, f: fn((&E))) {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(ref e) => f(e)
|
Err(ref e) => f(e)
|
||||||
|
@ -179,7 +180,7 @@ fn iter_err<T, E>(res: &Result<T, E>, f: fn((&E))) {
|
||||||
* parse_bytes(buf)
|
* parse_bytes(buf)
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn((&T)) -> U)
|
pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn((&T)) -> U)
|
||||||
-> Result<U, E> {
|
-> Result<U, E> {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(ref t) => Ok(op(t)),
|
Ok(ref t) => Ok(op(t)),
|
||||||
|
@ -195,7 +196,7 @@ fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn((&T)) -> U)
|
||||||
* is immediately returned. This function can be used to pass through a
|
* is immediately returned. This function can be used to pass through a
|
||||||
* successful result while handling an error.
|
* successful result while handling an error.
|
||||||
*/
|
*/
|
||||||
fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn((&E)) -> F)
|
pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn((&E)) -> F)
|
||||||
-> Result<T, F> {
|
-> Result<T, F> {
|
||||||
match *res {
|
match *res {
|
||||||
Ok(copy t) => Ok(t),
|
Ok(copy t) => Ok(t),
|
||||||
|
@ -274,7 +275,7 @@ impl<T: Copy, E: Copy> Result<T, E> {
|
||||||
* assert incd == ~[2u, 3u, 4u];
|
* assert incd == ~[2u, 3u, 4u];
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
fn map_vec<T,U:Copy,V:Copy>(
|
pub fn map_vec<T,U:Copy,V:Copy>(
|
||||||
ts: &[T], op: fn((&T)) -> Result<V,U>) -> Result<~[V],U> {
|
ts: &[T], op: fn((&T)) -> Result<V,U>) -> Result<~[V],U> {
|
||||||
|
|
||||||
let mut vs: ~[V] = vec::with_capacity(vec::len(ts));
|
let mut vs: ~[V] = vec::with_capacity(vec::len(ts));
|
||||||
|
@ -287,7 +288,7 @@ fn map_vec<T,U:Copy,V:Copy>(
|
||||||
return Ok(move vs);
|
return Ok(move vs);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_opt<T,U:Copy,V:Copy>(
|
pub fn map_opt<T,U:Copy,V:Copy>(
|
||||||
o_t: &Option<T>, op: fn((&T)) -> Result<V,U>) -> Result<Option<V>,U> {
|
o_t: &Option<T>, op: fn((&T)) -> Result<V,U>) -> Result<Option<V>,U> {
|
||||||
|
|
||||||
match *o_t {
|
match *o_t {
|
||||||
|
@ -308,7 +309,7 @@ fn map_opt<T,U:Copy,V:Copy>(
|
||||||
* used in 'careful' code contexts where it is both appropriate and easy
|
* used in 'careful' code contexts where it is both appropriate and easy
|
||||||
* to accommodate an error like the vectors being of different lengths.
|
* to accommodate an error like the vectors being of different lengths.
|
||||||
*/
|
*/
|
||||||
fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
|
pub fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
|
||||||
op: fn((&S),(&T)) -> Result<V,U>) -> Result<~[V],U> {
|
op: fn((&S),(&T)) -> Result<V,U>) -> Result<~[V],U> {
|
||||||
|
|
||||||
assert vec::same_length(ss, ts);
|
assert vec::same_length(ss, ts);
|
||||||
|
@ -330,7 +331,7 @@ fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
|
||||||
* error. This could be implemented using `map2()` but it is more efficient
|
* error. This could be implemented using `map2()` but it is more efficient
|
||||||
* on its own as no result vector is built.
|
* on its own as no result vector is built.
|
||||||
*/
|
*/
|
||||||
fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T],
|
pub fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T],
|
||||||
op: fn((&S),(&T)) -> Result<(),U>) -> Result<(),U> {
|
op: fn((&S),(&T)) -> Result<(),U>) -> Result<(),U> {
|
||||||
|
|
||||||
assert vec::same_length(ss, ts);
|
assert vec::same_length(ss, ts);
|
||||||
|
@ -347,7 +348,7 @@ fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T],
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unwraps a result, assuming it is an `ok(T)`
|
/// Unwraps a result, assuming it is an `ok(T)`
|
||||||
fn unwrap<T, U>(+res: Result<T, U>) -> T {
|
pub fn unwrap<T, U>(+res: Result<T, U>) -> T {
|
||||||
match move res {
|
match move res {
|
||||||
Ok(move t) => move t,
|
Ok(move t) => move t,
|
||||||
Err(_) => fail ~"unwrap called on an err result"
|
Err(_) => fail ~"unwrap called on an err result"
|
||||||
|
@ -355,7 +356,7 @@ fn unwrap<T, U>(+res: Result<T, U>) -> T {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unwraps a result, assuming it is an `err(U)`
|
/// Unwraps a result, assuming it is an `err(U)`
|
||||||
fn unwrap_err<T, U>(+res: Result<T, U>) -> U {
|
pub fn unwrap_err<T, U>(+res: Result<T, U>) -> U {
|
||||||
match move res {
|
match move res {
|
||||||
Err(move u) => move u,
|
Err(move u) => move u,
|
||||||
Ok(_) => fail ~"unwrap called on an ok result"
|
Ok(_) => fail ~"unwrap called on an ok result"
|
||||||
|
|
Loading…
Add table
Reference in a new issue