tauri_plugin_android_fs/api/
writable_stream.rs1use sync_async::sync_async;
2use crate::*;
3
4
5#[sync_async(
17 use(if_sync) super::impls::SyncWritableStreamImpls as WritableStreamImpls;
18 use(if_async) super::impls::AsyncWritableStreamImpls as WritableStreamImpls;
19 use(if_sync) super::api_sync::WritableStream;
20 use(if_async) super::api_async::WritableStream;
21)]
22pub struct WritableStream<R: tauri::Runtime> {
23 #[cfg(target_os = "android")]
24 pub(crate) impls: WritableStreamImpls<R>,
25
26 #[cfg(not(target_os = "android"))]
27 #[allow(unused)]
28 pub(crate) impls: std::marker::PhantomData<fn() -> R>
29}
30
31#[sync_async(
32 use(if_async) super::api_async::{AndroidFs, FileOpener, FilePicker, PrivateStorage, PublicStorage};
33 use(if_sync) super::api_sync::{AndroidFs, FileOpener, FilePicker, PrivateStorage, PublicStorage};
34)]
35impl<R: tauri::Runtime> WritableStream<R> {
36
37 #[always_sync]
39 pub fn into_sync(self) -> SyncWritableStream<R> {
40 #[cfg(not(target_os = "android"))] {
41 panic!("expected on Android")
44 }
45 #[cfg(target_os = "android")] {
46 SyncWritableStream { impls: self.impls.into_sync() }
47 }
48 }
49
50 #[always_sync]
52 pub fn into_async(self) -> AsyncWritableStream<R> {
53 #[cfg(not(target_os = "android"))] {
54 panic!("expected on Android")
57 }
58 #[cfg(target_os = "android")] {
59 AsyncWritableStream { impls: self.impls.into_async() }
60 }
61 }
62
63 #[maybe_async]
76 pub fn reflect(self) -> Result<()> {
77 #[cfg(not(target_os = "android"))] {
78 Err(Error::NOT_ANDROID)
79 }
80 #[cfg(target_os = "android")] {
81 self.impls.reflect().await
82 }
83 }
84
85 #[maybe_async]
91 pub fn sync_all(&self) -> std::io::Result<()> {
92 #[cfg(not(target_os = "android"))] {
93 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
94 }
95 #[cfg(target_os = "android")] {
96 self.impls.sync_all().await
97 }
98 }
99
100 #[maybe_async]
106 pub fn sync_data(&self) -> std::io::Result<()> {
107 #[cfg(not(target_os = "android"))] {
108 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
109 }
110 #[cfg(target_os = "android")] {
111 self.impls.sync_data().await
112 }
113 }
114}
115
116macro_rules! impl_write {
117 ($target:ident) => {
118
119 impl<R: tauri::Runtime> std::io::Write for $target<R> {
120
121 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
122 #[cfg(not(target_os = "android"))] {
123 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
124 }
125 #[cfg(target_os = "android")] {
126 self.impls.write(buf)
127 }
128 }
129
130 fn flush(&mut self) -> std::io::Result<()> {
131 #[cfg(not(target_os = "android"))] {
132 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
133 }
134 #[cfg(target_os = "android")] {
135 self.impls.flush()
136 }
137 }
138
139 fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()> {
140 #[cfg(not(target_os = "android"))] {
141 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
142 }
143 #[cfg(target_os = "android")] {
144 self.impls.write_all(buf)
145 }
146 }
147
148 fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
149 #[cfg(not(target_os = "android"))] {
150 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
151 }
152 #[cfg(target_os = "android")] {
153 self.impls.write_vectored(bufs)
154 }
155 }
156
157 fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> std::io::Result<()> {
158 #[cfg(not(target_os = "android"))] {
159 Err(std::io::Error::new(std::io::ErrorKind::Other, Error::NOT_ANDROID))
160 }
161 #[cfg(target_os = "android")] {
162 self.impls.write_fmt(fmt)
163 }
164 }
165 }
166 };
167}
168
169impl_write!(AsyncWritableStream);
170impl_write!(SyncWritableStream);