async_backplane/
lib.rs

1pub mod prelude;
2pub mod panic;
3
4mod crash;
5pub use crash::Crash;
6
7mod device_id;
8pub use device_id::DeviceID;
9
10mod device;
11pub use device::Device;
12
13mod fault;
14pub use fault::Fault;
15
16mod line;
17pub use line::Line;
18
19mod watched;
20pub use watched::Watched;
21
22mod plugboard;
23mod linemap;
24
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26/// There was a problem Linking
27pub enum LinkError {
28    /// We can't because we already disconnected.
29    DeviceDown,
30    /// We can't because the other Device already disconnected.
31    LinkDown,
32}
33
34#[derive(Clone, Copy)]
35#[repr(u32)]
36/// How deeply should we link?
37pub enum LinkMode {
38    /// Receive a notification when the other Device disconnects.
39    Monitor = 0b01,
40    /// Send a notification when we disconnect.
41    Notify  = 0b10,
42    /// Monitor + Notify.
43    Peer    = 0b11,
44}
45
46impl LinkMode {
47    /// true if we should be notified when the other Device disconnects.
48    pub fn monitor(&self) -> bool {
49        LinkMode::Monitor as u32 == ((*self) as u32 & LinkMode::Monitor as u32)
50    }
51    /// true if we should notify the other Device when we disconnect.
52    pub fn notify(&self) -> bool {
53        LinkMode::Notify as u32 == ((*self) as u32 & LinkMode::Notify as u32)
54    }
55    /// true if both sides will notify the other on disconnect.
56    pub fn peer(&self) -> bool {
57        LinkMode::Peer as u32 == ((*self) as u32 & LinkMode::Peer as u32)
58    }
59}
60
61#[derive(Clone, Copy, Debug, Eq, PartialEq)]
62/// A message exchanged between devices.
63pub enum Message {
64    /// A Device we are monitoring has disconnected.
65    Disconnected(DeviceID, Option<Fault>),
66    /// Request to stop running.
67    Shutdown(DeviceID),
68}
69
70use Message::{Disconnected, Shutdown};
71
72impl Message {
73
74    /// Returns the DeviceID of the sender.
75    pub fn sender(&self) -> DeviceID {
76        match self {
77            Disconnected(did, _) => *did,
78            Shutdown(did) => *did,
79        }
80    }
81
82    /// Unwraps the Disconnect notification or panics.
83    pub fn unwrap_disconnected(&self) -> (DeviceID, Option<Fault>) {
84        if let Disconnected(did, fault) = self { (*did, *fault) }
85        else { panic!("Message was not Disconnected") }
86    }
87
88    /// Unwraps the Shutdown request or panics.
89    pub fn unwrap_shutdown(&self) -> DeviceID {
90        if let Shutdown(did) = self { *did }
91        else { panic!("Message was not Shutdown") }
92    }
93}