async_flow/io/
port_state.rs

1// This is free and unencumbered software released into the public domain.
2
3/// A port's possible states (either unconnected, connected, disconnected, or closed).
4#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
7pub enum PortState {
8    #[default]
9    Unconnected,
10
11    Connected,
12
13    Disconnected,
14
15    Closed,
16}
17
18impl PortState {
19    /// Checks whether the port state is currently unconnected.
20    pub fn is_unconnected(&self) -> bool {
21        *self == Self::Unconnected
22    }
23
24    /// Checks whether the port state is currently connected.
25    pub fn is_connected(&self) -> bool {
26        *self == Self::Connected
27    }
28
29    /// Checks whether the port state is currently disconnected.
30    pub fn is_disconnected(&self) -> bool {
31        *self == Self::Disconnected
32    }
33
34    /// Checks whether the port state is currently closed.
35    pub fn is_closed(&self) -> bool {
36        *self == Self::Closed
37    }
38
39    pub fn as_str(&self) -> &str {
40        match self {
41            Self::Unconnected => "unconnected",
42            Self::Connected => "connected",
43            Self::Disconnected => "disconnected",
44            Self::Closed => "closed",
45        }
46    }
47}
48
49impl AsRef<str> for PortState {
50    fn as_ref(&self) -> &str {
51        self.as_str()
52    }
53}