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        /// Sends a message to the device.
28        async fn send(&mut self, tx: &[TxMessage]) -> Result<(), LinkError>;
29
30        /// Receives a message from the device.
31        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError>;
32
33        /// Checks if the link is open.
34        #[must_use]
35        fn is_open(&self) -> bool;
36    }
37
38    #[async_trait::async_trait]
39    impl AsyncLink for Box<dyn AsyncLink> {
40        async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
41            self.as_mut().open(geometry).await
42        }
43
44        async fn close(&mut self) -> Result<(), LinkError> {
45            self.as_mut().close().await
46        }
47
48        async fn update(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
49            self.as_mut().update(geometry).await
50        }
51
52        async fn send(&mut self, tx: &[TxMessage]) -> Result<(), LinkError> {
53            self.as_mut().send(tx).await
54        }
55
56        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError> {
57            self.as_mut().receive(rx).await
58        }
59
60        fn is_open(&self) -> bool {
61            self.as_ref().is_open()
62        }
63    }
64}
65
66#[cfg(not(feature = "async-trait"))]
67mod internal {
68    use super::*;
69
70    /// A trait that provides the interface with the device.
71    pub trait AsyncLink: Send {
72        /// Opens the link.
73        fn open(
74            &mut self,
75            geometry: &Geometry,
76        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
77
78        /// Closes the link.
79        fn close(&mut self) -> impl std::future::Future<Output = Result<(), LinkError>>;
80
81        #[doc(hidden)]
82        fn update(
83            &mut self,
84            _: &Geometry,
85        ) -> impl std::future::Future<Output = Result<(), LinkError>> {
86            async { Ok(()) }
87        }
88
89        /// Sends a message to the device.
90        fn send(
91            &mut self,
92            tx: &[TxMessage],
93        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
94
95        /// Receives a message from the device.
96        fn receive(
97            &mut self,
98            rx: &mut [RxMessage],
99        ) -> impl std::future::Future<Output = Result<(), LinkError>>;
100
101        /// Checks if the link is open.
102        #[must_use]
103        fn is_open(&self) -> bool;
104    }
105}