Skip to main content

dager/
inport.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::any::Any;
6use std::any::TypeId;
7
8use crate::DErr;
9use crate::collections::InCol;
10use crate::edge::Edge;
11
12pub trait InSignature{
13    fn has_edge(&self, idx: usize) -> bool;
14    fn set(&mut self, idx: usize, value: Box<dyn Any>) -> Result<(), DErr>;
15    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr>;
16    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>;
17    fn all_set(&self) -> bool;
18}
19
20
21impl InSignature for () {
22    fn has_edge(&self, _idx: usize) -> bool{
23	false
24    }
25    fn set(&mut self, _idx: usize, _value: Box<dyn Any>) -> Result<(), DErr>{
26	Err(DErr::NoSuchPort)
27    }
28    fn set_edge(&mut self, _edge: Edge) -> Result<(), DErr>{
29	Err(DErr::NoSuchPort)	
30    }
31    fn remove_edge(&mut self, _port_idx: usize) -> Result<Edge, DErr>{
32	Err(DErr::NoSuchPort)
33    }
34    //Checks if 
35    fn all_set(&self) -> bool{
36	true
37    }
38}
39
40impl<A> InSignature for InCol<A> where A: Send + 'static {
41    fn has_edge(&self, idx: usize) -> bool{
42	if idx == 0{
43	    self.edge.is_some()
44	}else{
45	   false
46	}
47    }
48    fn set(&mut self, idx: usize, value: Box<dyn Any>) -> Result<(), DErr>{
49	if idx == 0{
50	    match value.downcast::<A>(){
51		Ok(val) => self.value = Some(*val),
52		Err(_) => return Err(DErr::TypeMissmatch),
53	    }
54	    Ok(())
55	}else{
56	    Err(DErr::NoSuchPort)
57	}
58    }
59    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr>{
60	if edge.end_idx == 0{
61	    if TypeId::of::<A>() != edge.type_id{
62		return Err(DErr::TypeMissmatch);
63	    }
64	    self.edge = Some(edge);
65	    Ok(())
66	}else{
67	    Err(DErr::NoSuchPort)
68	}
69    }
70    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>{
71	if port_idx == 0{
72	    if let Some(old_partner) = self.edge.take(){
73		Ok(old_partner)
74	    }else{
75		Err(DErr::NoEdgeOnIdx)
76	    }
77	}else{
78	    Err(DErr::NoSuchPort)
79	}
80    }
81    //Checks if 
82    fn all_set(&self) -> bool{
83	self.value.is_some()
84    }
85}
86
87
88/*
89impl<A, B> InSignature for (InCol<A>, InCol<B>) where A: Send + 'static, B: Send + 'static{
90    fn has_edge(&self, idx: usize) -> bool {
91	match idx{
92	    0 => self.0.edge.is_some(),
93	    1 => self.1.edge.is_some(),
94	    _ => false
95	}
96    }
97    
98    fn all_set(&self) -> bool {
99	self.0.value.is_some() && self.1.value.is_some()
100    }
101
102    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr> {
103	match edge.end_idx{
104	    0 => {
105		if TypeId::of::<A>() != edge.type_id{
106		    return Err(DErr::TypeMissmatch);
107		}
108		self.0.edge = Some(edge);
109	    },
110	    1 => {
111		if TypeId::of::<B>() != edge.type_id{
112		    return Err(DErr::TypeMissmatch);
113		}
114		self.1.edge = Some(edge);
115	    },
116	    _ => return Err(DErr::NoSuchPort),
117	}
118	Ok(())
119    }
120
121    fn set(&mut self, idx: usize, value: Box<dyn Any>) -> Result<(), DErr>{
122	match idx{
123	    0 => {
124		match value.downcast::<A>(){
125		    Ok(val) => self.0.value = Some(*val),
126		    Err(_) => return Err(DErr::TypeMissmatch),
127		}
128		Ok(())
129	    },
130	    1 => {
131		match value.downcast::<B>(){
132		    Ok(val) => self.1.value = Some(*val),
133		    Err(_) => return Err(DErr::TypeMissmatch),
134		}
135		Ok(())
136	    },
137	    _ => Err(DErr::NoSuchPort)
138	}
139    }
140}
141*/
142///Implemets the InSignature trait for any tupel of the for (A, B, C, ...) denoted as impl_insig!(A:0, B:1, C:2, ...);
143#[macro_export]
144macro_rules! impl_insig {
145    ($($Gen:ident : $Idx:tt), +) => {
146	impl<$($Gen),+> InSignature for ($(InCol<$Gen>),+) where $($Gen : Send + 'static),+{
147	    fn has_edge(&self, idx: usize) -> bool{
148		match idx{
149		    $($Idx => self.$Idx.edge.is_some(),)+
150		    _ => false
151		}
152	    }
153
154	    fn all_set(&self) -> bool{
155		$(self.$Idx.value.is_some() &&)+ true
156	    }
157	    
158	    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr>{
159		match edge.end_idx{
160		    $($Idx => {
161			if TypeId::of::<$Gen>() != edge.type_id{
162			    return Err(DErr::TypeMissmatch);
163			}
164			self.$Idx.edge = Some(edge)
165		    }),+
166		    _ => return Err(DErr::NoSuchPort),
167		}
168
169		Ok(())
170	    }
171
172	    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>{
173		match port_idx{
174		    $($Idx => {
175			if let Some(old_partner) = self.$Idx.edge.take(){
176			    Ok(old_partner)
177			}else{
178			    Err(DErr::NoEdgeOnIdx)
179			}
180		    }),+
181		    _ => Err(DErr::NoSuchPort)
182		}
183	    }
184	    
185	    fn set(&mut self, idx: usize, value: Box<dyn Any>) -> Result<(), DErr>{		
186		match idx{
187		    $($Idx => {
188			match value.downcast::<$Gen>(){
189			    Ok(val) => self.$Idx.value = Some(*val),
190			    Err(_) => return Err(DErr::TypeMissmatch),
191			}
192			Ok(())
193		    }),+
194		    _ => Err(DErr::NoSuchPort)
195		}
196	    }
197	}
198    };
199}
200
201
202impl_insig!(A:0, B:1);
203impl_insig!(A:0, B:1, C:2);
204impl_insig!(A:0, B:1, C:2, D:3);
205impl_insig!(A:0, B:1, C:2, D:3, E:4);
206impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5);
207impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6);
208impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7);
209impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8);
210impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9);
211impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10);
212impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11);
213impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12);
214impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13);
215impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14);
216impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15);
217impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16);
218impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17);
219impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18);
220impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19);
221impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20);
222impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21);
223impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, W:22);
224impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, W:22, X:23);
225impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, W:22, X:23, Y:24);
226impl_insig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, W:22, X:23, Y:24, Z:25);