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
46
47
use crate::Store;

pub trait AsContext {
    type Data;

    fn as_context(&self) -> &Store<Self::Data>;
}

impl<T> AsContext for Store<T> {
    type Data = T;

    fn as_context(&self) -> &Store<Self::Data> {
        self
    }
}

impl<'a, T: AsContext> AsContext for &'a T {
    type Data = T::Data;

    fn as_context(&self) -> &Store<Self::Data> {
        T::as_context(*self)
    }
}

impl<'a, T: AsContext> AsContext for &'a mut T {
    type Data = T::Data;

    fn as_context(&self) -> &Store<Self::Data> {
        T::as_context(*self)
    }
}

pub trait AsContextMut: AsContext {
    fn as_context_mut(&mut self) -> &mut Store<Self::Data>;
}

impl<T> AsContextMut for Store<T> {
    fn as_context_mut(&mut self) -> &mut Store<Self::Data> {
        self
    }
}

impl<'a, T: AsContextMut> AsContextMut for &'a mut T {
    fn as_context_mut(&mut self) -> &mut Store<Self::Data> {
        T::as_context_mut(*self)
    }
}