Skip to main content

dager/
outport.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
5
6use std::any::TypeId;
7
8use crate::DErr;
9use crate::collections::OutCol;
10use crate::edge::Edge;
11
12
13///An out signature must be able to save which idx of an node's output ports are connected.
14pub trait OutSignature{
15    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr>;
16    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>;
17}
18
19/*
20impl<A, B> OutSignature for (OutCol<A>, OutCol<B>) where A: Send + 'static, B: Send + 'static{
21    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr> {
22	match edge.start_idx{
23	    0 => {
24		if TypeId::of::<A>() == edge.type_id{
25		    return Err(DErr::TypeMissmatch);
26		}
27		self.0.edge = Some(edge)
28	    },
29	    1 => {
30		if TypeId::of::<B>() == edge.type_id{
31		    return Err(DErr::TypeMissmatch);
32		}
33		self.1.edge = Some(edge)
34	    },
35	    _ => return Err(DErr::NoSuchPort),
36	}
37
38	Ok(())
39    }
40}
41*/
42
43impl<A> OutSignature for OutCol<A> where A: Send + 'static{
44    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr> {
45	if edge.start_idx == 0{
46	    if TypeId::of::<A>() != edge.type_id{
47		return Err(DErr::TypeMissmatch);
48	    }
49	    self.edge = Some(edge);
50	}else{
51	    return Err(DErr::NoSuchPort);
52	}
53	Ok(())
54    }
55
56    
57    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>{
58	if port_idx == 0{
59	    if let Some(old_edge) = self.edge.take(){
60		Ok(old_edge)
61	    }else{
62		Err(DErr::NoEdgeOnIdx)
63	    }
64	}else{
65	    Err(DErr::NoSuchPort)
66	}
67    }
68}
69
70impl OutSignature for (){
71    fn set_edge(&mut self, _edge: Edge) -> Result<(), DErr> {
72	Err(DErr::NoSuchPort)
73    }
74    
75    fn remove_edge(&mut self, _port_idx: usize) -> Result<Edge, DErr>{
76	Err(DErr::NoSuchPort)
77    }
78}
79
80
81///Implemets the OutSignature trait for any tupel of the for (A, B, C, ...) denoted as impl_outsig!(A:0, B:1, C:2, ...);
82#[macro_export]
83macro_rules! impl_outsig {
84    ($($Gen:ident : $Idx:tt), +) => {
85	impl<$($Gen),+> OutSignature for ($(OutCol<$Gen>),+) where $($Gen : Send + 'static),+{
86	    fn set_edge(&mut self, edge: Edge) -> Result<(), DErr>{
87		match edge.start_idx{
88		    $($Idx => {
89			if TypeId::of::<$Gen>() != edge.type_id{
90			    return Err(DErr::TypeMissmatch);
91			}
92			self.$Idx.edge = Some(edge)
93		    }),+
94		    _ => return Err(DErr::NoSuchPort),
95		}
96
97		Ok(())
98	    }
99	    
100	    fn remove_edge(&mut self, port_idx: usize) -> Result<Edge, DErr>{
101		match port_idx{
102		    $($Idx => {
103			if let Some(old_edge) = self.$Idx.edge.take(){
104			    Ok(old_edge)
105			}else{
106			    Err(DErr::NoEdgeOnIdx)
107			}
108		    }), +
109			_ => Err(DErr::NoSuchPort)
110		}
111		
112	    }
113	}
114    };
115}
116
117
118impl_outsig!(A:0, B:1);
119impl_outsig!(A:0, B:1, C:2);
120impl_outsig!(A:0, B:1, C:2, D:3);
121impl_outsig!(A:0, B:1, C:2, D:3, E:4);
122impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5);
123impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6);
124impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7);
125impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8);
126impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9);
127impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10);
128impl_outsig!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I:8, J:9, K:10, L:11);
129impl_outsig!(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);
130impl_outsig!(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);
131impl_outsig!(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);
132impl_outsig!(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);
133impl_outsig!(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);
134impl_outsig!(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);
135impl_outsig!(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);
136impl_outsig!(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);
137impl_outsig!(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);
138impl_outsig!(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);
139impl_outsig!(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);
140impl_outsig!(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);
141impl_outsig!(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);
142impl_outsig!(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);