summaryrefslogtreecommitdiffstats
path: root/tests/ui/regions/regions-refcell.rs
blob: 39ad0c53f1e98181529474ebd4693e2d82f7ff47 (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
// run-pass
// This is a regression test for something that only came up while
// attempting to bootstrap librustc with new destructor lifetime
// semantics.


use std::collections::HashMap;
use std::cell::RefCell;

// This version does not yet work (associated type issues)...
#[cfg(cannot_use_this_yet)]
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
    let one = [1];
    assert_eq!(map.borrow().get("one"), Some(&one[..]));
}

#[cfg(cannot_use_this_yet_either)]
// ... and this version does not work (the lifetime of `one` is
// supposed to match the lifetime `'a`) ...
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
    let one = [1];
    assert_eq!(map.borrow().get("one"), Some(&&one[..]));
}

#[cfg(all(not(cannot_use_this_yet),not(cannot_use_this_yet_either)))]
fn foo<'a>(map: RefCell<HashMap<&'static str, &'a [u8]>>) {
    // ...so instead we walk through the trivial slice and make sure
    // it contains the element we expect.

    for (i, &x) in map.borrow().get("one").unwrap().iter().enumerate() {
        assert_eq!((i, x), (0, 1));
    }
}

fn main() {
    let zer = [0];
    let one = [1];
    let two = [2];
    let mut map = HashMap::new();
    map.insert("zero", &zer[..]);
    map.insert("one",  &one[..]);
    map.insert("two",  &two[..]);
    let map = RefCell::new(map);
    foo(map);
}