use crate::{
geometry::Geometry,
link::{LinkError, RxMessage, TxMessage},
};
pub use internal::AsyncLink;
#[cfg(feature = "async-trait")]
mod internal {
use super::*;
#[async_trait::async_trait]
pub trait AsyncLink: Send {
async fn open(&mut self, geometry: &Geometry) -> Result<(), LinkError>;
async fn close(&mut self) -> Result<(), LinkError>;
#[doc(hidden)]
async fn update(&mut self, _: &Geometry) -> Result<(), LinkError> {
Ok(())
}
async fn send(&mut self, tx: &[TxMessage]) -> Result<bool, LinkError>;
async fn receive(&mut self, rx: &mut [RxMessage]) -> Result<bool, LinkError>;
#[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::*;
pub trait AsyncLink: Send {
fn open(
&mut self,
geometry: &Geometry,
) -> impl std::future::Future<Output = Result<(), LinkError>>;
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(()) }
}
fn send(
&mut self,
tx: &[TxMessage],
) -> impl std::future::Future<Output = Result<bool, LinkError>>;
fn receive(
&mut self,
rx: &mut [RxMessage],
) -> impl std::future::Future<Output = Result<bool, LinkError>>;
#[must_use]
fn is_open(&self) -> bool;
}
}