1use elsa::FrozenVec;
2
3fn main() {
4 let arena = Arena::new();
5 let lonely = arena.add_thing("lonely", vec![]);
6 let best_friend = arena.add_thing("best friend", vec![lonely]);
7 let threes_a_crowd = arena.add_thing("threes a crowd", vec![lonely, best_friend]);
8 let rando = arena.add_thing("rando", vec![]);
9 let _facebook = arena.add_thing("facebook", vec![rando, threes_a_crowd, lonely, best_friend]);
10
11 assert!(cmp_ref(lonely, best_friend.friends[0]));
12 assert!(cmp_ref(best_friend, threes_a_crowd.friends[1]));
13 arena.dump();
14}
15
16struct Arena<'arena> {
17 things: FrozenVec<Box<Thing<'arena>>>,
18}
19
20struct Thing<'arena> {
21 pub friends: Vec<ThingRef<'arena>>,
22 pub name: &'static str,
23}
24
25type ThingRef<'arena> = &'arena Thing<'arena>;
26
27impl<'arena> Arena<'arena> {
28 fn new() -> Arena<'arena> {
29 Arena {
30 things: FrozenVec::new(),
31 }
32 }
33
34 fn add_thing(
35 &'arena self,
36 name: &'static str,
37 friends: Vec<ThingRef<'arena>>,
38 ) -> ThingRef<'arena> {
39 let idx = self.things.len();
40 self.things.push(Box::new(Thing { name, friends }));
41 &self.things[idx]
42 }
43
44 fn dump(&'arena self) {
45 for thing in &self.things {
46 println!("friends of {}:", thing.name);
47 for friend in &thing.friends {
48 println!("\t{}", friend.name);
49 }
50 }
51 }
52}
53
54fn cmp_ref<T>(x: &T, y: &T) -> bool {
55 std::ptr::eq(x, y)
56}