gmt_dos_actors/actor/
check.rs

1use interface::{Update, Who};
2
3use crate::{
4    framework::model::{Check, CheckError},
5    ActorError,
6};
7
8use super::{Actor, PlainActor};
9
10type Result<T> = std::result::Result<T, CheckError>;
11
12impl<C, const NI: usize, const NO: usize> Check for Actor<C, NI, NO>
13where
14    C: 'static + Update,
15{
16    fn check_inputs(&self) -> Result<()> {
17        match self.inputs {
18            Some(_) if NI == 0 => Err(ActorError::SomeInputsZeroRate(Who::who(self))),
19            None if NI > 0 => Err(ActorError::NoInputsPositiveRate(Who::who(self))),
20            _ => Ok(()),
21        }?;
22        Ok(())
23    }
24    fn check_outputs(&self) -> Result<()> {
25        match self.outputs {
26            Some(_) if NO == 0 => Err(ActorError::SomeOutputsZeroRate(Who::who(self))),
27            None if NO > 0 => Err(ActorError::NoOutputsPositiveRate(Who::who(self))),
28            _ => Ok(()),
29        }?;
30        Ok(())
31    }
32    fn n_inputs(&self) -> usize {
33        self.inputs.as_ref().map_or(0, |i| i.len())
34    }
35    fn n_outputs(&self) -> usize {
36        self.outputs
37            .as_ref()
38            .map_or(0, |o| o.iter().map(|o| o.len()).sum())
39    }
40    fn inputs_hashes(&self) -> Vec<u64> {
41        self.inputs.as_ref().map_or(Vec::new(), |inputs| {
42            inputs.iter().map(|input| input.get_hash()).collect()
43        })
44    }
45    fn outputs_hashes(&self) -> Vec<u64> {
46        self.outputs.as_ref().map_or(Vec::new(), |outputs| {
47            outputs
48                .iter()
49                .flat_map(|output| vec![output.get_hash(); output.len()])
50                .collect()
51        })
52    }
53    fn _as_plain(&self) -> PlainActor {
54        self.into()
55    }
56}