]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/cci_nested_lib.rs
Rollup merge of #28991 - goyox86:goyox86/rustfmting-liblog-II, r=alexcrichton
[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 #![allow(unknown_features)]
12 #![feature(box_syntax)]
13
14 use std::cell::RefCell;
15
16 pub struct Entry<A,B> {
17     key: A,
18     value: B
19 }
20
21 pub struct alist<A,B> {
22     eq_fn: extern "Rust" fn(A,A) -> bool,
23     data: Box<RefCell<Vec<Entry<A,B>>>>,
24 }
25
26 pub fn alist_add<A:'static,B:'static>(lst: &alist<A,B>, k: A, v: B) {
27     let mut data = lst.data.borrow_mut();
28     (*data).push(Entry{key:k, value:v});
29 }
30
31 pub fn alist_get<A:Clone + 'static,
32                  B:Clone + 'static>(
33                  lst: &alist<A,B>,
34                  k: A)
35                  -> B {
36     let eq_fn = lst.eq_fn;
37     let data = lst.data.borrow();
38     for entry in &(*data) {
39         if eq_fn(entry.key.clone(), k.clone()) {
40             return entry.value.clone();
41         }
42     }
43     panic!();
44 }
45
46 #[inline]
47 pub fn new_int_alist<B:'static>() -> alist<isize, B> {
48     fn eq_int(a: isize, b: isize) -> bool { a == b }
49     return alist {
50         eq_fn: eq_int,
51         data: box RefCell::new(Vec::new()),
52     };
53 }
54
55 #[inline]
56 pub fn new_int_alist_2<B:'static>() -> alist<isize, B> {
57     #[inline]
58     fn eq_int(a: isize, b: isize) -> bool { a == b }
59     return alist {
60         eq_fn: eq_int,
61         data: box RefCell::new(Vec::new()),
62     };
63 }