audio_streams/
async_api.rs1use std::io::Result;
16#[cfg(any(target_os = "android", target_os = "linux"))]
17use std::os::unix::io::RawFd as RawDescriptor;
18#[cfg(any(target_os = "android", target_os = "linux"))]
19use std::os::unix::net::UnixStream;
20#[cfg(windows)]
21use std::os::windows::io::RawHandle;
22use std::time::Duration;
23
24use async_trait::async_trait;
25
26#[async_trait(?Send)]
27pub trait ReadAsync {
28 async fn read_to_vec<'a>(
33 &'a self,
34 file_offset: Option<u64>,
35 vec: Vec<u8>,
36 ) -> Result<(usize, Vec<u8>)>;
37}
38
39#[async_trait(?Send)]
40pub trait WriteAsync {
41 async fn write_from_vec<'a>(
46 &'a self,
47 file_offset: Option<u64>,
48 vec: Vec<u8>,
49 ) -> Result<(usize, Vec<u8>)>;
50}
51
52#[async_trait(?Send)]
55pub trait EventAsyncWrapper {
56 async fn wait(&self) -> Result<u64>;
57}
58
59pub trait ReadWriteAsync: ReadAsync + WriteAsync {}
60
61pub type AsyncStream = Box<dyn ReadWriteAsync + Send>;
62
63#[async_trait(?Send)]
65pub trait AudioStreamsExecutor {
66 #[cfg(any(target_os = "android", target_os = "linux"))]
68 fn async_unix_stream(&self, f: UnixStream) -> Result<AsyncStream>;
69
70 #[cfg(windows)]
77 unsafe fn async_event(&self, event: RawHandle) -> Result<Box<dyn EventAsyncWrapper>>;
78
79 async fn delay(&self, dur: Duration) -> Result<()>;
81
82 #[cfg(any(target_os = "android", target_os = "linux"))]
84 async fn wait_fd_readable(&self, _fd: RawDescriptor) -> Result<()> {
85 Ok(())
86 }
87}
88
89#[cfg(test)]
90pub mod test {
91 use super::*;
92
93 pub struct TestExecutor {}
95
96 #[async_trait(?Send)]
97 impl AudioStreamsExecutor for TestExecutor {
98 #[cfg(any(target_os = "android", target_os = "linux"))]
99 fn async_unix_stream(&self, _f: UnixStream) -> Result<AsyncStream> {
100 panic!("Not Implemented");
101 }
102
103 async fn delay(&self, dur: Duration) -> Result<()> {
104 std::thread::sleep(dur);
106 return Ok(());
107 }
108
109 #[cfg(windows)]
110 unsafe fn async_event(&self, _event: RawHandle) -> Result<Box<dyn EventAsyncWrapper>> {
111 unimplemented!("async_event is not yet implemented on windows");
112 }
113 }
114}