autd3_core/link/
async.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::{
    geometry::Geometry,
    link::{LinkError, RxMessage, TxMessage},
};

pub use internal::AsyncLink;

#[cfg(feature = "async-trait")]
mod internal {

    use super::*;

    /// A trait that provides the interface with the device.
    #[async_trait::async_trait]
    pub trait AsyncLink: Send {
        /// Opens the link.
        async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError>;

        /// Closes the link.
        async fn close(&mut self) -> Result<(), LinkError>;

        #[doc(hidden)]
        async fn update(&mut self, _: &Geometry) -> Result<(), LinkError> {
            Ok(())
        }

        /// Sends a message to the device.
        async fn send(&mut self, tx: &[TxMessage]) -> Result<bool, LinkError>;

        /// Receives a message from the device.
        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, LinkError>;

        /// Checks if the link is open.
        #[must_use]
        fn is_open(&self) -> bool;
    }

    #[async_trait::async_trait]
    impl AsyncLink for Box<dyn AsyncLink> {
        async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
            self.as_mut().open(geometry).await
        }

        async fn close(&mut self) -> Result<(), LinkError> {
            self.as_mut().close().await
        }

        async fn update(&mut self, geometry: &Geometry) -> Result<(), LinkError> {
            self.as_mut().update(geometry).await
        }

        async fn send(&mut self, tx: &[TxMessage]) -> Result<bool, LinkError> {
            self.as_mut().send(tx).await
        }

        async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, LinkError> {
            self.as_mut().receive(rx).await
        }

        fn is_open(&self) -> bool {
            self.as_ref().is_open()
        }
    }
}

#[cfg(not(feature = "async-trait"))]
mod internal {
    use super::*;

    /// A trait that provides the interface with the device.
    pub trait AsyncLink: Send {
        /// Opens the link.
        fn open(
            &mut self,
            geometry: &Geometry,
        ) -> impl std::future::Future<Output = Result<(), LinkError>>;

        /// Closes the link.
        fn close(&mut self) -> impl std::future::Future<Output = Result<(), LinkError>>;

        #[doc(hidden)]
        fn update(
            &mut self,
            _: &Geometry,
        ) -> impl std::future::Future<Output = Result<(), LinkError>> {
            async { Ok(()) }
        }

        /// Sends a message to the device.
        fn send(
            &mut self,
            tx: &[TxMessage],
        ) -> impl std::future::Future<Output = Result<bool, LinkError>>;

        /// Receives a message from the device.
        fn receive(
            &mut self,
            rx: &mut [RxMessage],
        ) -> impl std::future::Future<Output = Result<bool, LinkError>>;

        /// Checks if the link is open.
        #[must_use]
        fn is_open(&self) -> bool;
    }
}