protoflow_core/
output_ports.rs

1// This is free and unencumbered software released into the public domain.
2
3//! Output port arrays.
4
5use crate::{
6    prelude::{fmt, slice, AsRef, Deref, Index},
7    Message, MessageSender, OutputPort, PortResult, System, Transport,
8};
9
10#[derive(Clone)]
11pub struct OutputPorts<T: Message, const N: usize> {
12    pub(crate) array: [OutputPort<T>; N],
13}
14
15impl<T: Message, const N: usize> OutputPorts<T, N> {
16    pub const LEN: usize = N;
17
18    pub fn new<X: Transport + Default>(system: &System<X>) -> Self {
19        Self {
20            array: [(); N].map(|_| OutputPort::new(system)),
21        }
22    }
23
24    pub const fn is_empty(&self) -> bool {
25        self.len() == 0
26    }
27
28    pub const fn len(&self) -> usize {
29        Self::LEN
30    }
31
32    pub const fn capacity(&self) -> usize {
33        Self::LEN as _
34    }
35
36    #[must_use]
37    pub fn get<I>(&self, index: usize) -> Option<&OutputPort<T>> {
38        self.array.get(index)
39    }
40
41    pub fn iter(&self) -> slice::Iter<OutputPort<T>> {
42        self.into_iter()
43    }
44
45    pub const fn as_slice(&self) -> &[OutputPort<T>] {
46        self.array.as_slice()
47    }
48}
49
50impl<T: Message, const N: usize> MessageSender<T> for OutputPorts<T, N> {
51    fn send<'a>(&self, _message: impl Into<&'a T>) -> PortResult<()>
52    where
53        T: 'a,
54    {
55        todo!("OutputPorts::send") // TODO
56    }
57}
58
59impl<T: Message, const N: usize> AsRef<[OutputPort<T>]> for OutputPorts<T, N> {
60    fn as_ref(&self) -> &[OutputPort<T>] {
61        self
62    }
63}
64
65impl<T: Message, const N: usize> Deref for OutputPorts<T, N> {
66    type Target = [OutputPort<T>];
67
68    fn deref(&self) -> &Self::Target {
69        self.array.as_slice()
70    }
71}
72
73impl<T: Message, const N: usize> Index<usize> for OutputPorts<T, N> {
74    type Output = OutputPort<T>;
75
76    fn index(&self, index: usize) -> &Self::Output {
77        &self.array[index]
78    }
79}
80
81impl<'a, T: Message + 'a, const N: usize> IntoIterator for &'a OutputPorts<T, N> {
82    type Item = &'a OutputPort<T>;
83    type IntoIter = slice::Iter<'a, OutputPort<T>>;
84
85    fn into_iter(self) -> Self::IntoIter {
86        self.iter()
87    }
88}
89
90impl<T: Message, const N: usize> fmt::Debug for OutputPorts<T, N> {
91    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92        f.debug_list().entries(&self.array).finish()
93    }
94}