summary refs log tree commit diff
path: root/src/test/run-pass/static-method-test.rs
blob: 57d6558dd7733c1707d9ee6d8a4b91af5549f8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// xfail-fast
#[legacy_modes];

// A trait for objects that can be used to do an if-then-else
// (No actual need for this to be static, but it is a simple test.)
trait bool_like {
    static fn select<A>(b: self, +x1: A, +x2: A) -> A;
}

fn andand<T: bool_like Copy>(x1: T, x2: T) -> T {
    select(x1, x2, x1)
}

impl bool: bool_like {
    static fn select<A>(&&b: bool, +x1: A, +x2: A) -> A {
        if b { x1 } else { x2 }
    }
}

impl int: bool_like {
    static fn select<A>(&&b: int, +x1: A, +x2: A) -> A {
        if b != 0 { x1 } else { x2 }
    }
}

// A trait for sequences that can be constructed imperatively.
trait buildable<A> {
     static pure fn build_sized(size: uint,
                                builder: fn(push: pure fn(+v: A))) -> self;
}


impl<A> @[A]: buildable<A> {
    #[inline(always)]
     static pure fn build_sized(size: uint,
                                builder: fn(push: pure fn(+v: A))) -> @[A] {
         at_vec::build_sized(size, builder)
     }
}
impl<A> ~[A]: buildable<A> {
    #[inline(always)]
     static pure fn build_sized(size: uint,
                                builder: fn(push: pure fn(+v: A))) -> ~[A] {
         vec::build_sized(size, builder)
     }
}

#[inline(always)]
pure fn build<A, B: buildable<A>>(builder: fn(push: pure fn(+v: A))) -> B {
    build_sized(4, builder)
}

/// 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 {
    do build |push| {
        for v.each() |elem| {
            push(f(*elem));
        }
    }
}

fn seq_range<BT: buildable<int>>(lo: uint, hi: uint) -> BT {
    do build_sized(hi-lo) |push| {
        for uint::range(lo, hi) |i| {
            push(i as int);
        }
    }
}

fn main() {
    let v: @[int] = seq_range(0, 10);
    assert v == @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

    let v: @[int] = map(&[1,2,3], |x| 1+x);
    assert v == @[2, 3, 4];
    let v: ~[int] = map(&[1,2,3], |x| 1+x);
    assert v == ~[2, 3, 4];

    assert select(true, 9, 14) == 9;
    assert !andand(true, false);
    assert andand(7, 12) == 12;
    assert andand(0, 12) == 0;
}