Demode iter-trait

This commit is contained in:
Tim Chevalier 2012-09-25 17:39:22 -07:00
parent 473a866733
commit e19e628b19
21 changed files with 70 additions and 68 deletions

View File

@ -2,17 +2,19 @@
// workaround our lack of traits and lack of macros. See core.{rc,rs} for
// how this file is used.
#[warn(deprecated_mode)];
use cmp::{Eq, Ord};
use inst::{IMPL_T, EACH, SIZE_HINT};
export extensions;
impl<A> IMPL_T<A>: iter::BaseIter<A> {
pure fn each(blk: fn(v: &A) -> bool) { EACH(self, blk) }
pure fn size_hint() -> Option<uint> { SIZE_HINT(self) }
pure fn each(blk: fn(v: &A) -> bool) { EACH(&self, blk) }
pure fn size_hint() -> Option<uint> { SIZE_HINT(&self) }
}
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(self, blk) }
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
@ -24,8 +26,8 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
}
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
pure fn contains(x: A) -> bool { iter::contains(self, x) }
pure fn count(x: A) -> uint { iter::count(self, x) }
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
pure fn count(x: &A) -> uint { iter::count(self, x) }
}
impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {

View File

@ -8,7 +8,7 @@ type IMPL_T<A> = dlist::DList<A>;
* e.g. breadth-first search with in-place enqueues), but removing the current
* node is forbidden.
*/
pure fn EACH<A>(self: IMPL_T<A>, f: fn(v: &A) -> bool) {
pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
let mut link = self.peek_n();
while option::is_some(&link) {
let nobe = option::get(&link);
@ -29,6 +29,6 @@ pure fn EACH<A>(self: IMPL_T<A>, f: fn(v: &A) -> bool) {
}
}
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> Option<uint> {
pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}

View File

@ -6,7 +6,7 @@ type IMPL_T<A> = dvec::DVec<A>;
*
* Attempts to access this dvec during iteration will fail.
*/
pure fn EACH<A>(self: IMPL_T<A>, f: fn(v: &A) -> bool) {
pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
unsafe {
do self.swap |v| {
v.each(f);
@ -15,6 +15,6 @@ pure fn EACH<A>(self: IMPL_T<A>, f: fn(v: &A) -> bool) {
}
}
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> Option<uint> {
pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}

View File

@ -1,16 +1,16 @@
#[allow(non_camel_case_types)]
type IMPL_T<A> = Option<A>;
pure fn EACH<A>(self: IMPL_T<A>, f: fn(v: &A) -> bool) {
match self {
pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
match *self {
None => (),
Some(ref a) => { f(a); }
}
}
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> Option<uint> {
match self {
None => Some(0u),
Some(_) => Some(1u)
pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
match *self {
None => Some(0),
Some(_) => Some(1)
}
}

View File

@ -23,8 +23,8 @@ trait ExtendedIter<A> {
}
trait EqIter<A:Eq> {
pure fn contains(x: A) -> bool;
pure fn count(x: A) -> uint;
pure fn contains(x: &A) -> bool;
pure fn count(x: &A) -> uint;
}
trait Times {
@ -66,11 +66,11 @@ trait Buildable<A> {
builder: fn(push: pure fn(+v: A))) -> self;
}
pure fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, v: &A) -> bool) {
let mut i = 0u;
pure fn eachi<A,IA:BaseIter<A>>(self: &IA, blk: fn(uint, v: &A) -> bool) {
let mut i = 0;
for self.each |a| {
if !blk(i, a) { break; }
i += 1u;
i += 1;
}
}
@ -130,17 +130,17 @@ pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: IA) -> ~[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 {
pure fn contains<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> bool {
for self.each |a| {
if *a == x { return true; }
if *a == *x { return true; }
}
return false;
}
pure fn count<A:Eq,IA:BaseIter<A>>(self: IA, x: A) -> uint {
do foldl(self, 0u) |count, value| {
if value == x {
count + 1u
pure fn count<A:Eq,IA:BaseIter<A>>(self: IA, x: &A) -> uint {
do foldl(self, 0) |count, value| {
if value == *x {
count + 1
} else {
count
}

View File

@ -377,7 +377,7 @@ fn unshift_char(s: &mut ~str, ch: char) {
pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
if chars_to_trim.is_empty() { return from_slice(s); }
match find(s, |c| !chars_to_trim.contains(c)) {
match find(s, |c| !chars_to_trim.contains(&c)) {
None => ~"",
Some(first) => unsafe { raw::slice_bytes(s, first, s.len()) }
}
@ -395,7 +395,7 @@ pure fn trim_left_chars(s: &str, chars_to_trim: &[char]) -> ~str {
pure fn trim_right_chars(s: &str, chars_to_trim: &[char]) -> ~str {
if chars_to_trim.is_empty() { return str::from_slice(s); }
match rfind(s, |c| !chars_to_trim.contains(c)) {
match rfind(s, |c| !chars_to_trim.contains(&c)) {
None => ~"",
Some(last) => {
let {next, _} = char_range_at(s, last);

View File

@ -1873,7 +1873,7 @@ impl<A> &[A]: iter::BaseIter<A> {
}
impl<A> &[A]: iter::ExtendedIter<A> {
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(self, blk) }
pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(&self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
@ -1885,8 +1885,8 @@ impl<A> &[A]: iter::ExtendedIter<A> {
}
impl<A: Eq> &[A]: iter::EqIter<A> {
pure fn contains(x: A) -> bool { iter::contains(self, x) }
pure fn count(x: A) -> uint { iter::count(self, x) }
pure fn contains(x: &A) -> bool { iter::contains(self, x) }
pure fn count(x: &A) -> uint { iter::count(self, x) }
}
impl<A: Copy> &[A]: iter::CopyableIter<A> {

View File

@ -524,7 +524,7 @@ fn get_authority(rawurl: &str) ->
let host_is_end_plus_one: &fn() -> bool = || {
end+1 == len
&& !['?', '#', '/'].contains(rawurl[end] as char)
&& !['?', '#', '/'].contains(&(rawurl[end] as char))
};
// finish up

View File

@ -140,12 +140,12 @@ fn run_compiler(args: ~[~str], demitter: diagnostic::emitter) {
let lint_flags = vec::append(getopts::opt_strs(matches, ~"W"),
getopts::opt_strs(matches, ~"warn"));
if lint_flags.contains(~"help") {
if lint_flags.contains(&~"help") {
describe_warnings();
return;
}
if getopts::opt_strs(matches, ~"Z").contains(~"help") {
if getopts::opt_strs(matches, ~"Z").contains(&~"help") {
describe_debug_flags();
return;
}

View File

@ -204,7 +204,7 @@ impl check_loan_ctxt {
let did = ast_util::def_id_of_def(def);
let is_fn_arg =
did.crate == ast::local_crate &&
(*self.fn_args).contains(did.node);
(*self.fn_args).contains(&(did.node));
if is_fn_arg { return; } // case (a) above
}
ast::expr_fn_block(*) | ast::expr_fn(*) |
@ -251,7 +251,7 @@ impl check_loan_ctxt {
let def = self.tcx().def_map.get(expr.id);
let did = ast_util::def_id_of_def(def);
did.crate == ast::local_crate &&
(*self.fn_args).contains(did.node)
(*self.fn_args).contains(&(did.node))
}
ast::expr_fn_block(*) | ast::expr_fn(*) => {
self.is_stack_closure(expr.id)

View File

@ -275,7 +275,7 @@ fn missing_ctor(tcx: ty::ctxt, m: matrix, left_ty: ty::t) -> Option<ctor> {
let variants = ty::enum_variants(tcx, eid);
if found.len() != (*variants).len() {
for vec::each(*variants) |v| {
if !found.contains(variant(v.id)) {
if !found.contains(&(variant(v.id))) {
return Some(variant(v.id));
}
}

View File

@ -169,7 +169,7 @@ fn check_item_recursion(sess: session, ast_map: ast_map::map,
visitor.visit_item(it, env, visitor);
fn visit_item(it: @item, &&env: env, v: visit::vt<env>) {
if (*env.idstack).contains(it.id) {
if (*env.idstack).contains(&(it.id)) {
env.sess.span_fatal(env.root_it.span, ~"recursive constant");
}
(*env.idstack).push(it.id);

View File

@ -199,13 +199,13 @@ fn check_fn(fk: visit::fn_kind, decl: fn_decl, body: blk, sp: span,
let id = ast_util::def_id_of_def(fv.def).node;
// skip over free variables that appear in the cap clause
if captured_vars.contains(id) { loop; }
if captured_vars.contains(&id) { loop; }
// if this is the last use of the variable, then it will be
// a move and not a copy
let is_move = {
match cx.last_use_map.find(fn_id) {
Some(vars) => (*vars).contains(id),
Some(vars) => (*vars).contains(&id),
None => false
}
};
@ -588,7 +588,7 @@ fn check_cast_for_escaping_regions(
do ty::walk_ty(source_ty) |ty| {
match ty::get(ty).sty {
ty::ty_param(source_param) => {
if target_params.contains(source_param) {
if target_params.contains(&source_param) {
/* case (2) */
} else {
check_owned(cx.tcx, ty, source.span); /* case (3) */

View File

@ -56,7 +56,7 @@ fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
if method.vis == private &&
(impl_id.crate != local_crate ||
!privileged_items
.contains(impl_id.node)) {
.contains(&(impl_id.node))) {
tcx.sess.span_err(span,
fmt!("method `%s` is \
private",
@ -95,9 +95,9 @@ fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
}
match methods[method_num] {
provided(method)
if method.vis == private &&
!privileged_items
.contains(trait_id.node) => {
if method.vis == private &&
!privileged_items
.contains(&(trait_id.node)) => {
tcx.sess.span_err(span,
fmt!("method
`%s` \
@ -157,7 +157,7 @@ fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
match ty::get(ty::expr_ty(tcx, base)).sty {
ty_class(id, _)
if id.crate != local_crate ||
!privileged_items.contains(id.node) => {
!privileged_items.contains(&(id.node)) => {
match method_map.find(expr.id) {
None => {
debug!("(privacy checking) checking \
@ -178,7 +178,7 @@ fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
match ty::get(ty::expr_ty(tcx, expr)).sty {
ty_class(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(id.node) {
!privileged_items.contains(&(id.node)) {
for fields.each |field| {
debug!("(privacy checking) checking \
field in struct literal");
@ -205,7 +205,7 @@ fn check_crate(tcx: ty::ctxt, method_map: &method_map, crate: @ast::crate) {
match ty::get(ty::pat_ty(tcx, pattern)).sty {
ty_class(id, _) => {
if id.crate != local_crate ||
!privileged_items.contains(id.node) {
!privileged_items.contains(&(id.node)) {
for fields.each |field| {
debug!("(privacy checking) checking \
struct pattern");

View File

@ -485,7 +485,7 @@ impl determine_rp_ctxt {
}
};
let dep = {ambient_variance: self.ambient_variance, id: self.item_id};
if !vec.contains(dep) { vec.push(dep); }
if !vec.contains(&dep) { vec.push(dep); }
}
// Determines whether a reference to a region that appears in the

View File

@ -813,7 +813,7 @@ fn trans_local_var(bcx: block, ref_id: ast::node_id, def: ast::def) -> Datum {
nid: ast::node_id) -> Datum {
let is_last_use = match bcx.ccx().maps.last_use_map.find(ref_id) {
None => false,
Some(vars) => (*vars).contains(nid)
Some(vars) => (*vars).contains(&nid)
};
let source = if is_last_use {FromLastUseLvalue} else {FromLvalue};

View File

@ -182,7 +182,7 @@ impl LookupContext {
ty_enum(did, _) => {
// Watch out for newtype'd enums like "enum t = @T".
// See discussion in typeck::check::do_autoderef().
if enum_dids.contains(did) {
if enum_dids.contains(&did) {
return None;
}
enum_dids.push(did);

View File

@ -5,7 +5,7 @@ use config::Config;
fn main(args: ~[~str]) {
if args.contains(~"-h") || args.contains(~"--help") {
if args.contains(&~"-h") || args.contains(&~"--help") {
config::usage();
return;
}

View File

@ -9,7 +9,7 @@ fn foo_func<A, B: thing<A>>(x: B) -> Option<A> { x.foo() }
fn main() {
for iter::eachi(Some({a: 0})) |i, a| {
for iter::eachi(&(Some({a: 0}))) |i, a| {
#debug["%u %d", i, a.a];
}

View File

@ -1,10 +1,10 @@
fn main() {
assert []/_.contains(22u) == false;
assert [1u, 3u]/_.contains(22u) == false;
assert [22u, 1u, 3u]/_.contains(22u) == true;
assert [1u, 22u, 3u]/_.contains(22u) == true;
assert [1u, 3u, 22u]/_.contains(22u) == true;
assert None.contains(22u) == false;
assert Some(1u).contains(22u) == false;
assert Some(22u).contains(22u) == true;
assert []/_.contains(&22u) == false;
assert [1u, 3u]/_.contains(&22u) == false;
assert [22u, 1u, 3u]/_.contains(&22u) == true;
assert [1u, 22u, 3u]/_.contains(&22u) == true;
assert [1u, 3u, 22u]/_.contains(&22u) == true;
assert None.contains(&22u) == false;
assert Some(1u).contains(&22u) == false;
assert Some(22u).contains(&22u) == true;
}

View File

@ -1,9 +1,9 @@
fn main() {
assert []/_.count(22u) == 0u;
assert [1u, 3u]/_.count(22u) == 0u;
assert [22u, 1u, 3u]/_.count(22u) == 1u;
assert [22u, 1u, 22u]/_.count(22u) == 2u;
assert None.count(22u) == 0u;
assert Some(1u).count(22u) == 0u;
assert Some(22u).count(22u) == 1u;
assert []/_.count(&22u) == 0u;
assert [1u, 3u]/_.count(&22u) == 0u;
assert [22u, 1u, 3u]/_.count(&22u) == 1u;
assert [22u, 1u, 22u]/_.count(&22u) == 2u;
assert None.count(&22u) == 0u;
assert Some(1u).count(&22u) == 0u;
assert Some(22u).count(&22u) == 1u;
}