Skip to main content

datum/graph/
ports.rs

1use super::*;
2
3#[derive(PartialEq, Eq, Hash)]
4pub struct Inlet<T: 'static> {
5    id: PortId,
6    name: Arc<str>,
7    _marker: PhantomData<fn() -> T>,
8}
9
10impl<T: 'static> Inlet<T> {
11    #[must_use]
12    pub fn new(name: impl Into<String>) -> Self {
13        Self::with_id(next_port_id(), name)
14    }
15
16    pub(super) fn with_id(id: PortId, name: impl Into<String>) -> Self {
17        Self::with_name(id, Arc::from(name.into()))
18    }
19
20    pub(super) fn with_arc_name(id: PortId, name: Arc<str>) -> Self {
21        Self::with_name(id, name)
22    }
23
24    fn with_name(id: PortId, name: Arc<str>) -> Self {
25        Self {
26            id,
27            name,
28            _marker: PhantomData,
29        }
30    }
31
32    #[must_use]
33    pub const fn id(&self) -> PortId {
34        self.id
35    }
36
37    #[must_use]
38    pub fn name(&self) -> &str {
39        &self.name
40    }
41
42    #[must_use]
43    pub fn erase(&self) -> AnyInlet {
44        AnyInlet {
45            id: self.id,
46            name: Arc::clone(&self.name),
47            type_id: TypeId::of::<T>(),
48            type_name: type_name::<T>(),
49        }
50    }
51}
52
53impl<T: 'static> Clone for Inlet<T> {
54    fn clone(&self) -> Self {
55        Self {
56            id: self.id,
57            name: Arc::clone(&self.name),
58            _marker: PhantomData,
59        }
60    }
61}
62
63impl<T: 'static> fmt::Debug for Inlet<T> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.debug_struct("Inlet")
66            .field("id", &self.id)
67            .field("name", &self.name)
68            .field("type", &type_name::<T>())
69            .finish()
70    }
71}
72
73#[derive(PartialEq, Eq, Hash)]
74pub struct Outlet<T: 'static> {
75    id: PortId,
76    name: Arc<str>,
77    _marker: PhantomData<fn() -> T>,
78}
79
80impl<T: 'static> Outlet<T> {
81    #[must_use]
82    pub fn new(name: impl Into<String>) -> Self {
83        Self::with_id(next_port_id(), name)
84    }
85
86    pub(super) fn with_id(id: PortId, name: impl Into<String>) -> Self {
87        Self::with_name(id, Arc::from(name.into()))
88    }
89
90    pub(super) fn with_arc_name(id: PortId, name: Arc<str>) -> Self {
91        Self::with_name(id, name)
92    }
93
94    fn with_name(id: PortId, name: Arc<str>) -> Self {
95        Self {
96            id,
97            name,
98            _marker: PhantomData,
99        }
100    }
101
102    #[must_use]
103    pub const fn id(&self) -> PortId {
104        self.id
105    }
106
107    #[must_use]
108    pub fn name(&self) -> &str {
109        &self.name
110    }
111
112    #[must_use]
113    pub fn erase(&self) -> AnyOutlet {
114        AnyOutlet {
115            id: self.id,
116            name: Arc::clone(&self.name),
117            type_id: TypeId::of::<T>(),
118            type_name: type_name::<T>(),
119        }
120    }
121}
122
123impl<T: 'static> Clone for Outlet<T> {
124    fn clone(&self) -> Self {
125        Self {
126            id: self.id,
127            name: Arc::clone(&self.name),
128            _marker: PhantomData,
129        }
130    }
131}
132
133impl<T: 'static> fmt::Debug for Outlet<T> {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        f.debug_struct("Outlet")
136            .field("id", &self.id)
137            .field("name", &self.name)
138            .field("type", &type_name::<T>())
139            .finish()
140    }
141}
142
143#[derive(Clone, Debug, PartialEq, Eq, Hash)]
144pub struct AnyInlet {
145    id: PortId,
146    pub(super) name: Arc<str>,
147    type_id: TypeId,
148    type_name: &'static str,
149}
150
151impl AnyInlet {
152    #[must_use]
153    pub const fn id(&self) -> PortId {
154        self.id
155    }
156
157    #[must_use]
158    pub fn name(&self) -> &str {
159        &self.name
160    }
161
162    #[must_use]
163    pub const fn type_id(&self) -> TypeId {
164        self.type_id
165    }
166
167    #[must_use]
168    pub const fn type_name(&self) -> &'static str {
169        self.type_name
170    }
171}
172
173#[derive(Clone, Debug, PartialEq, Eq, Hash)]
174pub struct AnyOutlet {
175    id: PortId,
176    pub(super) name: Arc<str>,
177    type_id: TypeId,
178    type_name: &'static str,
179}
180
181impl AnyOutlet {
182    #[must_use]
183    pub const fn id(&self) -> PortId {
184        self.id
185    }
186
187    #[must_use]
188    pub fn name(&self) -> &str {
189        &self.name
190    }
191
192    #[must_use]
193    pub const fn type_id(&self) -> TypeId {
194        self.type_id
195    }
196
197    #[must_use]
198    pub const fn type_name(&self) -> &'static str {
199        self.type_name
200    }
201}
202
203pub trait PortRef {
204    fn id(&self) -> PortId;
205    fn name(&self) -> &str;
206    fn kind(&self) -> PortKind;
207}
208
209impl<T: 'static> PortRef for Inlet<T> {
210    fn id(&self) -> PortId {
211        self.id
212    }
213
214    fn name(&self) -> &str {
215        &self.name
216    }
217
218    fn kind(&self) -> PortKind {
219        PortKind::Inlet
220    }
221}
222
223impl<T: 'static> PortRef for Outlet<T> {
224    fn id(&self) -> PortId {
225        self.id
226    }
227
228    fn name(&self) -> &str {
229        &self.name
230    }
231
232    fn kind(&self) -> PortKind {
233        PortKind::Outlet
234    }
235}