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