mavlink_core/connection/file/
async.rs1use std::io;
4use std::path::PathBuf;
5
6use futures::lock::{Mutex, MutexGuard};
7use tokio::fs::File;
8use tokio::io::Sink;
9
10use crate::AsyncMavlinkReader;
11use crate::Dialect;
12use crate::connection::r#async::{
13 AsyncConnectable, AsyncDialectConnection, AsyncTransport, Transport,
14};
15use crate::connection::file::config::FileConfig;
16
17pub async fn open(file_path: &PathBuf) -> io::Result<AsyncFileConnection> {
18 Ok(AsyncFileConnection {
19 file: Mutex::new(AsyncMavlinkReader::new(File::open(file_path).await?)),
20 })
21}
22
23pub struct AsyncFileConnection {
24 file: Mutex<AsyncMavlinkReader<File>>,
25}
26
27impl AsyncTransport for AsyncFileConnection {
28 type Reader = File;
29 type Writer = Sink;
30
31 fn reader(&self) -> &Mutex<AsyncMavlinkReader<Self::Reader>> {
32 &self.file
33 }
34
35 async fn lock_writer(&self) -> Option<MutexGuard<'_, Self::Writer>> {
36 None
37 }
38
39 fn retry_receive(&self, error: &crate::error::MessageReadError) -> bool {
40 !matches!(error, crate::error::MessageReadError::Io(error) if error.kind() == io::ErrorKind::UnexpectedEof)
41 }
42}
43
44impl AsyncConnectable for FileConfig {
45 async fn connect_async_with_dialect<D>(
46 &self,
47 dialect: D,
48 ) -> io::Result<AsyncDialectConnection<D>>
49 where
50 D: Dialect + Send + Sync,
51 D::Message: Send + Sync,
52 {
53 Ok(AsyncDialectConnection::new(
54 Transport::File(open(&self.address).await?),
55 dialect,
56 ))
57 }
58}