]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/regions-refcell.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass / regions-refcell.rs
1 // Copyright 2012-2014 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 // This is a regression test for something that only came up while
12 // attempting to bootstrap librustc with new destructor lifetime
13 // semantics.
14
15
16 use std::collections::HashMap;
17 use std::cell::RefCell;
18
19 // This version does not yet work (associated type issues)...
20 #[cfg(cannot_use_this_yet)]
21 fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
22 let one = [1];
23 assert_eq!(map.borrow().get("one"), Some(&one[..]));
24 }
25
26 #[cfg(cannot_use_this_yet_either)]
27 // ... and this version does not work (the lifetime of `one` is
28 // supposed to match the lifetime `'a`) ...
29 fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
30 let one = [1];
31 assert_eq!(map.borrow().get("one"), Some(&&one[..]));
32 }
33
34 #[cfg(all(not(cannot_use_this_yet),not(cannot_use_this_yet_either)))]
35 fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
36 // ...so instead we walk through the trivial slice and make sure
37 // it contains the element we expect.
38
39 for (i, &x) in map.borrow().get("one").unwrap().iter().enumerate() {
40 assert_eq!((i, x), (0, 1));
41 }
42 }
43
44 fn main() {
45 let zer = [0];
46 let one = [1];
47 let two = [2];
48 let mut map = HashMap::new();
49 map.insert("zero", &zer[..]);
50 map.insert("one", &one[..]);
51 map.insert("two", &two[..]);
52 let map = RefCell::new(map);
53 foo(map);
54 }