1use super::*;
2use std::future::{Future, IntoFuture};
3use std::pin::Pin;
4use std::sync::{Arc, Mutex};
5use std::task::{Context, Poll, Waker};
6
7pub trait Async: Interface {
17 type Output: Clone;
19
20 type CompletedHandler: Interface;
22
23 fn set_completed<F: Fn() + Send + 'static>(&self, handler: F) -> Result<()>;
25
26 fn invoke_completed(&self, hander: &Self::CompletedHandler, status: AsyncStatus);
28
29 fn get_results(&self) -> Result<Self::Output>;
31}
32
33pub struct AsyncFuture<A: Async> {
37 inner: A,
40
41 status: IAsyncInfo,
43
44 waker: Option<Arc<Mutex<Waker>>>,
50}
51
52impl<A: Async> AsyncFuture<A> {
53 fn new(inner: A) -> Self {
54 Self {
55 status: inner.cast().unwrap(),
57 inner,
58 waker: None,
59 }
60 }
61}
62
63unsafe impl<A: Async> Send for AsyncFuture<A> {}
64unsafe impl<A: Async> Sync for AsyncFuture<A> {}
65impl<A: Async> Unpin for AsyncFuture<A> {}
66
67impl<A: Async> Future for AsyncFuture<A> {
68 type Output = Result<A::Output>;
69
70 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
71 if self.status.Status()? != AsyncStatus::Started {
75 return Poll::Ready(self.inner.get_results());
76 }
77
78 if let Some(shared_waker) = &self.waker {
79 let mut guard = shared_waker.lock().unwrap();
83 guard.clone_from(cx.waker());
84
85 if self.status.Status()? != AsyncStatus::Started {
89 return Poll::Ready(self.inner.get_results());
90 }
91 } else {
92 let shared_waker = Arc::new(Mutex::new(cx.waker().clone()));
95 self.waker = Some(shared_waker.clone());
96
97 self.inner.set_completed(move || {
101 shared_waker.lock().unwrap().wake_by_ref();
102 })?;
103 };
104
105 Poll::Pending
106 }
107}
108
109impl Async for IAsyncAction {
114 type Output = ();
115 type CompletedHandler = AsyncActionCompletedHandler;
116
117 fn set_completed<F: Fn() + Send + 'static>(&self, handler: F) -> Result<()> {
118 self.SetCompleted(&AsyncActionCompletedHandler::new(move |_, _| {
119 handler();
120 Ok(())
121 }))
122 }
123
124 fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) {
125 _ = handler.Invoke(self, status);
126 }
127
128 fn get_results(&self) -> Result<Self::Output> {
129 self.GetResults()
130 }
131}
132
133impl<T: RuntimeType> Async for IAsyncOperation<T> {
134 type Output = T;
135 type CompletedHandler = AsyncOperationCompletedHandler<T>;
136
137 fn set_completed<F: Fn() + Send + 'static>(&self, handler: F) -> Result<()> {
138 self.SetCompleted(&AsyncOperationCompletedHandler::new(move |_, _| {
139 handler();
140 Ok(())
141 }))
142 }
143
144 fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) {
145 _ = handler.Invoke(self, status);
146 }
147
148 fn get_results(&self) -> Result<Self::Output> {
149 self.GetResults()
150 }
151}
152
153impl<P: RuntimeType> Async for IAsyncActionWithProgress<P> {
154 type Output = ();
155 type CompletedHandler = AsyncActionWithProgressCompletedHandler<P>;
156
157 fn set_completed<F: Fn() + Send + 'static>(&self, handler: F) -> Result<()> {
158 self.SetCompleted(&AsyncActionWithProgressCompletedHandler::new(
159 move |_, _| {
160 handler();
161 Ok(())
162 },
163 ))
164 }
165
166 fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) {
167 _ = handler.Invoke(self, status);
168 }
169
170 fn get_results(&self) -> Result<Self::Output> {
171 self.GetResults()
172 }
173}
174
175impl<T: RuntimeType, P: RuntimeType> Async for IAsyncOperationWithProgress<T, P> {
176 type Output = T;
177 type CompletedHandler = AsyncOperationWithProgressCompletedHandler<T, P>;
178
179 fn set_completed<F: Fn() + Send + 'static>(&self, handler: F) -> Result<()> {
180 self.SetCompleted(&AsyncOperationWithProgressCompletedHandler::new(
181 move |_, _| {
182 handler();
183 Ok(())
184 },
185 ))
186 }
187
188 fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) {
189 _ = handler.Invoke(self, status);
190 }
191
192 fn get_results(&self) -> Result<Self::Output> {
193 self.GetResults()
194 }
195}
196
197impl IntoFuture for IAsyncAction {
202 type Output = Result<()>;
203 type IntoFuture = AsyncFuture<Self>;
204
205 fn into_future(self) -> Self::IntoFuture {
206 AsyncFuture::new(self)
207 }
208}
209
210impl<T: RuntimeType> IntoFuture for IAsyncOperation<T> {
211 type Output = Result<T>;
212 type IntoFuture = AsyncFuture<Self>;
213
214 fn into_future(self) -> Self::IntoFuture {
215 AsyncFuture::new(self)
216 }
217}
218
219impl<P: RuntimeType> IntoFuture for IAsyncActionWithProgress<P> {
220 type Output = Result<()>;
221 type IntoFuture = AsyncFuture<Self>;
222
223 fn into_future(self) -> Self::IntoFuture {
224 AsyncFuture::new(self)
225 }
226}
227
228impl<T: RuntimeType, P: RuntimeType> IntoFuture for IAsyncOperationWithProgress<T, P> {
229 type Output = Result<T>;
230 type IntoFuture = AsyncFuture<Self>;
231
232 fn into_future(self) -> Self::IntoFuture {
233 AsyncFuture::new(self)
234 }
235}