async_backplane/
line.rs

1use crate::*;
2use crate::plugboard::Plugboard;
3use core::fmt;
4use std::sync::Arc;
5
6/// A reference to a `Device` that allows us to link with it.
7#[derive(Clone)]
8pub struct Line {
9    pub(crate) plugboard: Arc<Plugboard>,
10}
11
12impl Line {
13    /// Get the ID of the Device this line is connected to.
14    pub fn device_id(&self) -> DeviceID {
15        DeviceID::new(&*self.plugboard as *const _ as usize)
16    }
17
18    /// Send a message to the Device. Returns the original message on
19    /// failure (if the Device has disconnected).
20    pub fn send(self, message: Message) -> Result<(), Message> {
21        self.plugboard.send(message)
22    }
23
24    /// Links with a Device through its Line. Panics if you try to link to yourself.
25    pub fn link_line(&self, other: Line, mode: LinkMode) -> Result<(), LinkError>{
26        if self.device_id() == other.device_id() { panic!("Do not link to yourself."); }
27        if mode.monitor() { other.plugboard.plug(self.clone(), LinkError::LinkDown)?; }
28        if mode.notify() { self.plugboard.plug(other, LinkError::DeviceDown)?; }
29        Ok(())
30    }
31
32    /// Links with another Line.
33    pub fn unlink_line(&self, other: &Line, mode: LinkMode) {
34        #[allow(unused_must_use)]
35        if self.device_id() != other.device_id() {
36            if mode.monitor() { other.plugboard.unplug(self.device_id(), LinkError::LinkDown); }
37            if mode.notify() { self.plugboard.unplug(other.device_id(), LinkError::DeviceDown); }
38        }
39    }
40}
41
42impl Eq for Line {}
43
44impl Unpin for Line {}
45
46impl PartialEq for Line {
47    fn eq(&self, other: &Line) -> bool {
48        Arc::ptr_eq(&self.plugboard, &other.plugboard)
49    }
50}
51
52impl fmt::Debug for Line {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.write_fmt(format_args!("Line<{:x}>", self.device_id().inner))
55    }
56}