autd3_core/link/
async.rs

1use alloc::vec::Vec;
2
3use crate::{
4    geometry::Geometry,
5    link::{LinkError, RxMessage, TxMessage},
6};
7
8/// A trait that provides the interface with the device.
9pub trait AsyncLink: Send {
10    /// Opens the link.
11    fn open(
12        &mut self,
13        geometry: &Geometry,
14    ) -> impl core::future::Future<Output = Result<(), LinkError>>;
15
16    /// Closes the link.
17    fn close(&mut self) -> impl core::future::Future<Output = Result<(), LinkError>>;
18
19    #[doc(hidden)]
20    fn update(
21        &mut self,
22        _: &Geometry,
23    ) -> impl core::future::Future<Output = Result<(), LinkError>> {
24        async { Ok(()) }
25    }
26
27    /// Allocate a sending buffer for the link.
28    fn alloc_tx_buffer(
29        &mut self,
30    ) -> impl core::future::Future<Output = Result<Vec<TxMessage>, LinkError>>;
31
32    /// Sends a message to the device.
33    fn send(
34        &mut self,
35        tx: Vec<TxMessage>,
36    ) -> impl core::future::Future<Output = Result<(), LinkError>>;
37
38    /// Receives a message from the device.
39    fn receive(
40        &mut self,
41        rx: &mut [RxMessage],
42    ) -> impl core::future::Future<Output = Result<(), LinkError>>;
43
44    /// Checks if the link is open.
45    #[must_use]
46    fn is_open(&self) -> bool;
47
48    /// Ensures that the link is open, returning an error if it is not.
49    fn ensure_is_open(&self) -> Result<(), LinkError> {
50        if self.is_open() {
51            Ok(())
52        } else {
53            Err(LinkError::closed())
54        }
55    }
56}