autd3_core/link/
sync.rs

1use crate::geometry::Geometry;
2
3use super::{RxMessage, TxMessage, error::LinkError};
4
5/// A trait that provides the interface with the device.
6pub trait Link: Send {
7    /// Opens the link.
8    fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError>;
9
10    /// Closes the link.
11    fn close(&mut self) -> Result<(), LinkError>;
12
13    #[doc(hidden)]
14    fn update(&mut self, _: &Geometry) -> Result<(), LinkError> {
15        Ok(())
16    }
17
18    /// Sends a message to the device.
19    fn send(&mut self, tx: &[TxMessage]) -> Result<(), LinkError>;
20
21    /// Receives a message from the device.
22    fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError>;
23
24    /// Checks if the link is open.
25    #[must_use]
26    fn is_open(&self) -> bool;
27}
28
29impl Link for Box<dyn Link> {
30    fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
31        self.as_mut().open(geometry)
32    }
33
34    fn close(&mut self) -> Result<(), LinkError> {
35        self.as_mut().close()
36    }
37
38    fn update(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
39        self.as_mut().update(geometry)
40    }
41
42    fn send(&mut self, tx: &[TxMessage]) -> Result<(), LinkError> {
43        self.as_mut().send(tx)
44    }
45
46    fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError> {
47        self.as_mut().receive(rx)
48    }
49
50    fn is_open(&self) -> bool {
51        self.as_ref().is_open()
52    }
53}