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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage, SystemData};
use std::{ops::Deref, sync::Arc};

pub trait Link: Sized + Send + Sync + 'static {
    type Error;

    type CreateData<'a>: SystemData<'a>;
    fn create(this: &LinkHandle<Self>, data: &mut Self::CreateData<'_>) -> Result<(), Self::Error>;

    type PersistData<'a>: SystemData<'a>;
    fn persist(this: &LinkHandle<Self>, data: &mut Self::PersistData<'_>) -> bool;

    type DeleteData<'a>: SystemData<'a>;
    fn delete(this: &LinkHandle<Self>, data: &mut Self::DeleteData<'_>);
}

pub trait Role {
    type Link: Link;
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Is<R: Role> {
    #[serde(bound(serialize = "R::Link: Serialize"))]
    #[serde(bound(deserialize = "R::Link: Deserialize<'de>"))]
    link: LinkHandle<R::Link>,
}

impl<R: Role> Is<R> {
    pub fn delete(&self, data: &mut <R::Link as Link>::DeleteData<'_>) {
        Link::delete(&self.link, data)
    }
}

impl<R: Role> Clone for Is<R> {
    fn clone(&self) -> Self {
        Self {
            link: self.link.clone(),
        }
    }
}

impl<R: Role> Deref for Is<R> {
    type Target = R::Link;

    fn deref(&self) -> &Self::Target { &self.link }
}

impl<R: Role + 'static> Component for Is<R>
where
    R::Link: Send + Sync + 'static,
{
    type Storage = DerefFlaggedStorage<Self, specs::VecStorage<Self>>;
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LinkHandle<L: Link> {
    link: Arc<L>,
}

impl<L: Link> Clone for LinkHandle<L> {
    fn clone(&self) -> Self {
        Self {
            link: Arc::clone(&self.link),
        }
    }
}

impl<L: Link> LinkHandle<L> {
    pub fn from_link(link: L) -> Self {
        Self {
            link: Arc::new(link),
        }
    }

    pub fn make_role<R: Role<Link = L>>(&self) -> Is<R> { Is { link: self.clone() } }
}

impl<L: Link> Deref for LinkHandle<L> {
    type Target = L;

    fn deref(&self) -> &Self::Target { &self.link }
}