autd3_core/link/
async.rs

1use crate::{
2    geometry::Geometry,
3    link::{LinkError, RxMessage, TxMessage},
4};
5
6pub use internal::AsyncLink;
7
8#[cfg(feature = "async-trait")]
9mod internal {
10
11    use super::*;
12
13    /// A trait that provides the interface with the device.
14    #[async_trait::async_trait]
15    pub trait AsyncLink: Send {
16        /// Opens the link.
17        async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError>;
18
19        /// Closes the link.
20        async fn close(&mut self) -> Result<(), LinkError>;
21
22        #[doc(hidden)]
23        async fn update(&mut self, _: &Geometry) -> Result<(), LinkError> {
24            Ok(())
25        }
26
27        /// Allocate a sending buffer for the link.
28        async fn alloc_tx_buffer(&mut self) -> Result<Vec<TxMessage>, LinkError>;
29
30        /// Sends a message to the device.
31        async fn send(&mut self, tx: Vec<TxMessage>) -> Result<(), LinkError>;
32
33        /// Receives a message from the device.
34        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError>;
35
36        /// Checks if the link is open.
37        #[must_use]
38        fn is_open(&self) -> bool;
39    }
40
41    #[async_trait::async_trait]
42    impl AsyncLink for Box<dyn AsyncLink> {
43        async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
44            self.as_mut().open(geometry).await
45        }
46
47        async fn close(&mut self) -> Result<(), LinkError> {
48            self.as_mut().close().await
49        }
50
51        async fn update(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
52            self.as_mut().update(geometry).await
53        }
54
55        async fn alloc_tx_buffer(&mut self) -> Result<Vec<TxMessage>, LinkError> {
56            self.as_mut().alloc_tx_buffer().await
57        }
58
59        async fn send(&mut self, tx: Vec<TxMessage>) -> Result<(), LinkError> {
60            self.as_mut().send(tx).await
61        }
62
63        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError> {
64            self.as_mut().receive(rx).await
65        }
66
67        fn is_open(&self) -> bool {
68            self.as_ref().is_open()
69        }
70    }
71}
72
73#[cfg(not(feature = "async-trait"))]
74mod internal {
75    use super::*;
76
77    /// A trait that provides the interface with the device.
78    pub trait AsyncLink: Send {
79        /// Opens the link.
80        fn open(
81            &mut self,
82            geometry: &Geometry,
83        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
84
85        /// Closes the link.
86        fn close(&mut self) -> impl std::future::Future<Output = Result<(), LinkError>>;
87
88        #[doc(hidden)]
89        fn update(
90            &mut self,
91            _: &Geometry,
92        ) -> impl std::future::Future<Output = Result<(), LinkError>> {
93            async { Ok(()) }
94        }
95
96        /// Allocate a sending buffer for the link.
97        fn alloc_tx_buffer(
98            &mut self,
99        ) -> impl std::future::Future<Output = Result<Vec<TxMessage>, LinkError>>;
100
101        /// Sends a message to the device.
102        fn send(
103            &mut self,
104            tx: Vec<TxMessage>,
105        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
106
107        /// Receives a message from the device.
108        fn receive(
109            &mut self,
110            rx: &mut [RxMessage],
111        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
112
113        /// Checks if the link is open.
114        #[must_use]
115        fn is_open(&self) -> bool;
116    }
117}