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