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    /// Allocate a sending buffer for the link.
19    fn alloc_tx_buffer(&mut self) -> Result<Vec<TxMessage>, LinkError>;
20
21    /// Sends a message to the device.
22    fn send(&mut self, tx: Vec<TxMessage>) -> Result<(), LinkError>;
23
24    /// Receives a message from the device.
25    fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError>;
26
27    /// Checks if the link is open.
28    #[must_use]
29    fn is_open(&self) -> bool;
30
31    /// Ensures that the link is open, returning an error if it is not.
32    fn ensure_is_open(&self) -> Result<(), LinkError> {
33        if self.is_open() {
34            Ok(())
35        } else {
36            Err(LinkError::closed())
37        }
38    }
39}
40
41impl Link for Box<dyn Link> {
42    fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
43        self.as_mut().open(geometry)
44    }
45
46    fn close(&mut self) -> Result<(), LinkError> {
47        self.as_mut().close()
48    }
49
50    fn update(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
51        self.as_mut().update(geometry)
52    }
53
54    fn alloc_tx_buffer(&mut self) -> Result<Vec<TxMessage>, LinkError> {
55        self.as_mut().alloc_tx_buffer()
56    }
57
58    fn send(&mut self, tx: Vec<TxMessage>) -> Result<(), LinkError> {
59        self.as_mut().send(tx)
60    }
61
62    fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError> {
63        self.as_mut().receive(rx)
64    }
65
66    fn is_open(&self) -> bool {
67        self.as_ref().is_open()
68    }
69}