windows_future/
async_ready.rs1use super::*;
2use std::sync::atomic::{AtomicBool, Ordering};
3
4struct ReadyState<T: Async> {
5 set_completed: AtomicBool,
6 result: Result<T::Output>,
7}
8
9impl<T: Async> ReadyState<T> {
10 fn new(result: Result<T::Output>) -> Self {
11 Self {
12 set_completed: AtomicBool::new(false),
13 result,
14 }
15 }
16
17 fn status(&self) -> AsyncStatus {
18 if self.result.is_ok() {
19 AsyncStatus::Completed
20 } else {
21 AsyncStatus::Error
22 }
23 }
24
25 fn invoke_completed(&self, sender: &T, handler: Ref<'_, T::CompletedHandler>) -> Result<()> {
28 if !self.set_completed.swap(true, Ordering::SeqCst) {
29 sender.invoke_completed(handler.ok()?, self.status());
30 Ok(())
31 } else {
32 Err(Error::from_hresult(HRESULT(0x80000018u32 as i32))) }
34 }
35
36 fn error_code(&self) -> Result<HRESULT> {
39 Ok(match &self.result {
40 Ok(_) => HRESULT(0),
41 Err(error) => error.code(),
42 })
43 }
44}
45
46#[implement(IAsyncAction, IAsyncInfo)]
47struct ReadyAction(ReadyState<IAsyncAction>);
48
49#[implement(IAsyncOperation<T>, IAsyncInfo)]
50struct ReadyOperation<T>(ReadyState<IAsyncOperation<T>>)
51where
52 T: RuntimeType + 'static;
53
54#[implement(IAsyncActionWithProgress<P>, IAsyncInfo)]
55struct ReadyActionWithProgress<P>(ReadyState<IAsyncActionWithProgress<P>>)
56where
57 P: RuntimeType + 'static;
58
59#[implement(IAsyncOperationWithProgress<T, P>, IAsyncInfo)]
60struct ReadyOperationWithProgress<T, P>(ReadyState<IAsyncOperationWithProgress<T, P>>)
61where
62 T: RuntimeType + 'static,
63 P: RuntimeType + 'static;
64
65impl IAsyncInfo_Impl for ReadyAction_Impl {
66 fn Id(&self) -> Result<u32> {
67 Ok(1)
68 }
69 fn Status(&self) -> Result<AsyncStatus> {
70 Ok(self.0.status())
71 }
72 fn ErrorCode(&self) -> Result<HRESULT> {
73 self.0.error_code()
74 }
75 fn Cancel(&self) -> Result<()> {
76 Ok(())
77 }
78 fn Close(&self) -> Result<()> {
79 Ok(())
80 }
81}
82
83impl<T: RuntimeType> IAsyncInfo_Impl for ReadyOperation_Impl<T> {
84 fn Id(&self) -> Result<u32> {
85 Ok(1)
86 }
87 fn Status(&self) -> Result<AsyncStatus> {
88 Ok(self.0.status())
89 }
90 fn ErrorCode(&self) -> Result<HRESULT> {
91 self.0.error_code()
92 }
93 fn Cancel(&self) -> Result<()> {
94 Ok(())
95 }
96 fn Close(&self) -> Result<()> {
97 Ok(())
98 }
99}
100
101impl<P: RuntimeType> IAsyncInfo_Impl for ReadyActionWithProgress_Impl<P> {
102 fn Id(&self) -> Result<u32> {
103 Ok(1)
104 }
105 fn Status(&self) -> Result<AsyncStatus> {
106 Ok(self.0.status())
107 }
108 fn ErrorCode(&self) -> Result<HRESULT> {
109 self.0.error_code()
110 }
111 fn Cancel(&self) -> Result<()> {
112 Ok(())
113 }
114 fn Close(&self) -> Result<()> {
115 Ok(())
116 }
117}
118
119impl<T: RuntimeType, P: RuntimeType> IAsyncInfo_Impl for ReadyOperationWithProgress_Impl<T, P> {
120 fn Id(&self) -> Result<u32> {
121 Ok(1)
122 }
123 fn Status(&self) -> Result<AsyncStatus> {
124 Ok(self.0.status())
125 }
126 fn ErrorCode(&self) -> Result<HRESULT> {
127 self.0.error_code()
128 }
129 fn Cancel(&self) -> Result<()> {
130 Ok(())
131 }
132 fn Close(&self) -> Result<()> {
133 Ok(())
134 }
135}
136
137impl IAsyncAction_Impl for ReadyAction_Impl {
138 fn SetCompleted(&self, handler: Ref<'_, AsyncActionCompletedHandler>) -> Result<()> {
139 self.0.invoke_completed(&self.as_interface(), handler)
140 }
141 fn Completed(&self) -> Result<AsyncActionCompletedHandler> {
142 Err(Error::empty())
143 }
144 fn GetResults(&self) -> Result<()> {
145 self.0.result.clone()
146 }
147}
148
149impl<T: RuntimeType> IAsyncOperation_Impl<T> for ReadyOperation_Impl<T> {
150 fn SetCompleted(&self, handler: Ref<'_, AsyncOperationCompletedHandler<T>>) -> Result<()> {
151 self.0.invoke_completed(&self.as_interface(), handler)
152 }
153 fn Completed(&self) -> Result<AsyncOperationCompletedHandler<T>> {
154 Err(Error::empty())
155 }
156 fn GetResults(&self) -> Result<T> {
157 self.0.result.clone()
158 }
159}
160
161impl<P: RuntimeType> IAsyncActionWithProgress_Impl<P> for ReadyActionWithProgress_Impl<P> {
162 fn SetCompleted(
163 &self,
164 handler: Ref<'_, AsyncActionWithProgressCompletedHandler<P>>,
165 ) -> Result<()> {
166 self.0.invoke_completed(&self.as_interface(), handler)
167 }
168 fn Completed(&self) -> Result<AsyncActionWithProgressCompletedHandler<P>> {
169 Err(Error::empty())
170 }
171 fn GetResults(&self) -> Result<()> {
172 self.0.result.clone()
173 }
174 fn SetProgress(&self, _: Ref<'_, AsyncActionProgressHandler<P>>) -> Result<()> {
175 Ok(())
176 }
177 fn Progress(&self) -> Result<AsyncActionProgressHandler<P>> {
178 Err(Error::empty())
179 }
180}
181
182impl<T: RuntimeType, P: RuntimeType> IAsyncOperationWithProgress_Impl<T, P>
183 for ReadyOperationWithProgress_Impl<T, P>
184{
185 fn SetCompleted(
186 &self,
187 handler: Ref<'_, AsyncOperationWithProgressCompletedHandler<T, P>>,
188 ) -> Result<()> {
189 self.0.invoke_completed(&self.as_interface(), handler)
190 }
191 fn Completed(&self) -> Result<AsyncOperationWithProgressCompletedHandler<T, P>> {
192 Err(Error::empty())
193 }
194 fn GetResults(&self) -> Result<T> {
195 self.0.result.clone()
196 }
197 fn SetProgress(&self, _: Ref<'_, AsyncOperationProgressHandler<T, P>>) -> Result<()> {
198 Ok(())
199 }
200 fn Progress(&self) -> Result<AsyncOperationProgressHandler<T, P>> {
201 Err(Error::empty())
202 }
203}
204
205impl IAsyncAction {
206 pub fn ready(result: Result<()>) -> Self {
208 ReadyAction(ReadyState::new(result)).into()
209 }
210}
211
212impl<T: RuntimeType> IAsyncOperation<T> {
213 pub fn ready(result: Result<T>) -> Self {
215 ReadyOperation(ReadyState::new(result)).into()
216 }
217}
218
219impl<P: RuntimeType> IAsyncActionWithProgress<P> {
220 pub fn ready(result: Result<()>) -> Self {
222 ReadyActionWithProgress(ReadyState::new(result)).into()
223 }
224}
225
226impl<T: RuntimeType, P: RuntimeType> IAsyncOperationWithProgress<T, P> {
227 pub fn ready(result: Result<T>) -> Self {
229 ReadyOperationWithProgress(ReadyState::new(result)).into()
230 }
231}