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 #[async_trait::async_trait]
15 pub trait AsyncLink: Send {
16 async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError>;
18
19 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 async fn send(&mut self, tx: &[TxMessage]) -> Result<(), LinkError>;
29
30 async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<(), LinkError>;
32
33 #[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 pub trait AsyncLink: Send {
72 fn open(
74 &mut self,
75 geometry: &Geometry,
76 ) -> impl std::future::Future<Output = Result<(), LinkError>>;
77
78 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 fn send(
91 &mut self,
92 tx: &[TxMessage],
93 ) -> impl std::future::Future<Output = Result<(), LinkError>>;
94
95 fn receive(
97 &mut self,
98 rx: &mut [RxMessage],
99 ) -> impl std::future::Future<Output = Result<(), LinkError>>;
100
101 #[must_use]
103 fn is_open(&self) -> bool;
104 }
105}