1use crate::*;
2use crate::plugboard::Plugboard;
3use core::fmt;
4use std::sync::Arc;
5
6#[derive(Clone)]
8pub struct Line {
9 pub(crate) plugboard: Arc<Plugboard>,
10}
11
12impl Line {
13 pub fn device_id(&self) -> DeviceID {
15 DeviceID::new(&*self.plugboard as *const _ as usize)
16 }
17
18 pub fn send(self, message: Message) -> Result<(), Message> {
21 self.plugboard.send(message)
22 }
23
24 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 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}