gmt_dos_actors/
aggregation.rs

1//! # Actors aggregations
2//!
3//! Algebraic rules to add [Model] and [Actor] to create a new model
4
5use std::ops::{Add, AddAssign};
6
7use interface::Update;
8
9use crate::{
10    actor::Actor,
11    model,
12    model::{Model, Unknown},
13};
14
15/// Aggregation of models into a new model
16impl Add for Model<Unknown> {
17    type Output = Model<Unknown>;
18
19    fn add(self, rhs: Self) -> Self::Output {
20        match (self.actors, rhs.actors) {
21            (None, None) => Model::new(vec![]),
22            (None, Some(b)) => Model::new(b),
23            (Some(a), None) => Model::new(a),
24            (Some(mut a), Some(mut b)) => {
25                a.append(&mut b);
26                Model::new(a)
27            }
28        }
29    }
30}
31
32/// Aggregation of a model and an actor into a new model
33impl<C, const NI: usize, const NO: usize> Add<Actor<C, NI, NO>> for Model<Unknown>
34where
35    C: Update + 'static,
36{
37    type Output = Model<Unknown>;
38
39    fn add(self, rhs: Actor<C, NI, NO>) -> Self::Output {
40        self + model!(rhs)
41    }
42}
43
44/// Aggregation of an actor and a model into a new model
45impl<C, const NI: usize, const NO: usize> Add<Model<Unknown>> for Actor<C, NI, NO>
46where
47    C: Update + 'static,
48{
49    type Output = Model<Unknown>;
50
51    fn add(self, rhs: Model<Unknown>) -> Self::Output {
52        model!(self) + rhs
53    }
54}
55
56/// Aggregation of actors into a model
57impl<A, const A_NI: usize, const A_NO: usize, B, const B_NI: usize, const B_NO: usize>
58    Add<Actor<B, B_NI, B_NO>> for Actor<A, A_NI, A_NO>
59where
60    A: Update + 'static,
61    B: Update + 'static,
62{
63    type Output = Model<Unknown>;
64
65    fn add(self, rhs: Actor<B, B_NI, B_NO>) -> Self::Output {
66        model!(self) + model!(rhs)
67    }
68}
69/* /// Aggregation of subsystems into a model
70impl<Right, Left> Add<SubSystem<Right>> for SubSystem<Left>
71where
72    Right: Gateways + BuildSystem<Right> + GetField + 'static,
73    Model<model::Unknown>: From<Right>,
74    Left: Gateways + BuildSystem<Left> + GetField + 'static,
75    Model<model::Unknown>: From<Left>,
76{
77    type Output = Model<Unknown>;
78
79    fn add(self, rhs: SubSystem<Right>) -> Self::Output {
80        model!(self, rhs)
81    }
82} */
83
84impl<C, const NI: usize, const NO: usize> AddAssign<Actor<C, NI, NO>> for Model<Unknown>
85where
86    C: Update + 'static,
87{
88    fn add_assign(&mut self, rhs: Actor<C, NI, NO>) {
89        self.actors.get_or_insert(vec![]).push(Box::new(rhs));
90    }
91}