]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/cci_nested_lib.rs
a9be1e62195ff9665904b52f60f03b769e77220b
[rust.git] / src / test / auxiliary / cci_nested_lib.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[feature(managed_boxes)];
12
13 use std::cell::RefCell;
14
15 pub struct Entry<A,B> {
16     key: A,
17     value: B
18 }
19
20 pub struct alist<A,B> {
21     eq_fn: extern "Rust" fn(A,A) -> bool,
22     data: @RefCell<Vec<Entry<A,B>> >,
23 }
24
25 pub fn alist_add<A:'static,B:'static>(lst: &alist<A,B>, k: A, v: B) {
26     let mut data = lst.data.borrow_mut();
27     data.get().push(Entry{key:k, value:v});
28 }
29
30 pub fn alist_get<A:Clone + 'static,
31                  B:Clone + 'static>(
32                  lst: &alist<A,B>,
33                  k: A)
34                  -> B {
35     let eq_fn = lst.eq_fn;
36     let data = lst.data.borrow();
37     for entry in data.get().iter() {
38         if eq_fn(entry.key.clone(), k.clone()) {
39             return entry.value.clone();
40         }
41     }
42     fail!();
43 }
44
45 #[inline]
46 pub fn new_int_alist<B:'static>() -> alist<int, B> {
47     fn eq_int(a: int, b: int) -> bool { a == b }
48     return alist {
49         eq_fn: eq_int,
50         data: @RefCell::new(Vec::new()),
51     };
52 }
53
54 #[inline]
55 pub fn new_int_alist_2<B:'static>() -> alist<int, B> {
56     #[inline]
57     fn eq_int(a: int, b: int) -> bool { a == b }
58     return alist {
59         eq_fn: eq_int,
60         data: @RefCell::new(Vec::new()),
61     };
62 }