Remove deprecated modes from list.rs (and temporarily delete list::push)

This commit is contained in:
Kevin Cantu 2012-08-29 16:28:04 -07:00
parent 34bf84649e
commit 7d57b4864a
3 changed files with 27 additions and 14 deletions

View file

@ -1,4 +1,6 @@
//! A standard linked list
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
import core::cmp::Eq;
import core::option;
@ -28,9 +30,9 @@ fn from_vec<T: copy>(v: &[T]) -> @list<T> {
* * z - The initial value
* * f - The function to apply
*/
fn foldl<T: copy, U>(z: T, ls: @list<U>, f: fn(T, U) -> T) -> T {
fn foldl<T: copy, U>(+z: T, ls: @list<U>, f: fn((&T), (&U)) -> T) -> T {
let mut accum: T = z;
do iter(ls) |elt| { accum = f(accum, elt);}
do iter(ls) |elt| { accum = f(&accum, &elt);}
accum
}
@ -41,12 +43,12 @@ fn foldl<T: copy, U>(z: T, ls: @list<U>, f: fn(T, U) -> T) -> T {
* When function `f` returns true then an option containing the element
* is returned. If `f` matches no elements then none is returned.
*/
fn find<T: copy>(ls: @list<T>, f: fn(T) -> bool) -> Option<T> {
fn find<T: copy>(ls: @list<T>, f: fn((&T)) -> bool) -> Option<T> {
let mut ls = ls;
loop {
ls = match *ls {
cons(hd, tl) => {
if f(hd) { return Some(hd); }
if f(&hd) { return Some(hd); }
tl
}
nil => return None
@ -55,7 +57,7 @@ fn find<T: copy>(ls: @list<T>, f: fn(T) -> bool) -> Option<T> {
}
/// Returns true if a list contains an element with the given value
fn has<T: copy Eq>(ls: @list<T>, elt: T) -> bool {
fn has<T: copy Eq>(ls: @list<T>, +elt: T) -> bool {
for each(ls) |e| {
if e == elt { return true; }
}
@ -110,10 +112,13 @@ pure fn append<T: copy>(l: @list<T>, m: @list<T>) -> @list<T> {
}
}
/// Push an element to the front of a list
fn push<T: copy>(&l: list<T>, v: T) {
l = cons(v, @l);
/*
/// Push one element into the front of a list, returning a new list
/// THIS VERSION DOESN'T ACTUALLY WORK
pure fn push<T: copy>(ll: &mut @list<T>, +vv: T) {
ll = &mut @cons(vv, *ll)
}
*/
/// Iterate over a list
fn iter<T>(l: @list<T>, f: fn(T)) {
@ -201,7 +206,7 @@ mod tests {
#[test]
fn test_foldl() {
fn add(&&a: uint, &&b: int) -> uint { return a + (b as uint); }
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
let l = from_vec(~[0, 1, 2, 3, 4]);
let empty = @list::nil::<int>;
assert (list::foldl(0u, l, add) == 10u);
@ -210,8 +215,8 @@ mod tests {
#[test]
fn test_foldl2() {
fn sub(&&a: int, &&b: int) -> int {
a - b
fn sub(a: &int, b: &int) -> int {
*a - *b
}
let l = from_vec(~[1, 2, 3, 4]);
assert (list::foldl(0, l, sub) == -10);
@ -219,14 +224,14 @@ mod tests {
#[test]
fn test_find_success() {
fn match_(&&i: int) -> bool { return i == 2; }
fn match_(i: &int) -> bool { return *i == 2; }
let l = from_vec(~[0, 1, 2]);
assert (list::find(l, match_) == option::Some(2));
}
#[test]
fn test_find_fail() {
fn match_(&&_i: int) -> bool { return false; }
fn match_(_i: &int) -> bool { return false; }
let l = from_vec(~[0, 1, 2]);
let empty = @list::nil::<int>;
assert (list::find(l, match_) == option::None::<int>);
@ -251,6 +256,11 @@ mod tests {
assert (list::len(empty) == 0u);
}
#[test]
fn test_append() {
assert from_vec(~[1,2,3,4])
== list::append(list::from_vec(~[1,2]), list::from_vec(~[3,4]));
}
}
// Local Variables:

View file

@ -1,3 +1,6 @@
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
import io::Writer;
import io::WriterUtil;
import serialization::serializer;

View file

@ -140,7 +140,7 @@ fn type_needs_inner(cx: ctx, use: uint, ty: ty::t,
ty::ty_fn(_) | ty::ty_ptr(_) | ty::ty_rptr(_, _)
| ty::ty_trait(_, _, _) => false,
ty::ty_enum(did, substs) => {
if option::is_none(list::find(enums_seen, |id| id == did)) {
if option::is_none(list::find(enums_seen, |id| *id == did)) {
let seen = @cons(did, enums_seen);
for vec::each(*ty::enum_variants(cx.ccx.tcx, did)) |v| {
for vec::each(v.args) |aty| {