async_flow/io/port.rs
1// This is free and unencumbered software released into the public domain.
2
3use crate::PortDirection;
4
5use super::PortState;
6use dogma::{MaybeLabeled, MaybeNamed};
7
8/// The common interface for ports, whether for input or output.
9pub trait Port<T>: MaybeNamed + MaybeLabeled {
10 /// Closes this port without dropping it, returning immediately.
11 ///
12 /// If the port had an open connection, it will be disconnected.
13 /// If the port was already closed, no further action is taken.
14 /// There is no facility to reopen a port once it has been closed.
15 fn close(&mut self);
16
17 /// The dataflow direction of this port.
18 fn direction(&self) -> PortDirection;
19
20 /// The current state of this port.
21 fn state(&self) -> PortState;
22
23 /// Checks whether this port is an input port.
24 fn is_input(&self) -> bool {
25 self.direction().is_input()
26 }
27
28 /// Checks whether this port is an output port.
29 fn is_output(&self) -> bool {
30 self.direction().is_output()
31 }
32
33 /// Checks whether this port is currently closed.
34 fn is_closed(&self) -> bool {
35 self.state().is_closed()
36 }
37
38 /// Checks whether this port is currently open.
39 fn is_open(&self) -> bool {
40 self.state().is_open()
41 }
42
43 /// Checks whether this port is currently connected.
44 fn is_connected(&self) -> bool {
45 self.state().is_connected()
46 }
47
48 /// Returns the remaining buffer capacity of the connection.
49 fn capacity(&self) -> Option<usize> {
50 None // unknown
51 }
52
53 /// Returns the maximum buffer capacity of the connection.
54 fn max_capacity(&self) -> Option<usize> {
55 None // unknown
56 }
57}
58
59impl<T> core::fmt::Debug for &dyn Port<T> {
60 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
61 f.debug_struct("Port")
62 .field("name", &self.name())
63 .field("state", &self.state())
64 .finish()
65 }
66}