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