|
| 1 | +use super::*; |
| 2 | + |
| 3 | +// An `Async` represents a WinRT async execution object or type. There are precisely four such types: |
| 4 | +// - IAsyncAction |
| 5 | +// - IAsyncActionWithProgress |
| 6 | +// - IAsyncOperation |
| 7 | +// - IAsyncOperationWithProgress |
| 8 | +// |
| 9 | +// All four implementations are provided here and there is thus no need to implement this trait. |
| 10 | +// This trait provides an abstraction over the relevant differences so that the various async |
| 11 | +// capabilities in this crate can be reused for all implementations. |
| 12 | +pub trait Async: Interface { |
| 13 | + // The type of value produced on completion. |
| 14 | + type Output: Clone; |
| 15 | + |
| 16 | + // The type of the delegate use for completion notification. |
| 17 | + type CompletedHandler: Interface; |
| 18 | + |
| 19 | + // Sets the handler or callback to invoke when execution completes. This handler can only be set once. |
| 20 | + fn set_completed<F: Fn(&Self) + Send + 'static>(&self, handler: F) -> Result<()>; |
| 21 | + |
| 22 | + // Calls the given handler with the current object and status. |
| 23 | + #[cfg(feature = "std")] |
| 24 | + fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus); |
| 25 | + |
| 26 | + // Returns the value produced on completion. This should only be called when execution completes. |
| 27 | + fn get_results(&self) -> Result<Self::Output>; |
| 28 | + |
| 29 | + // Gets the current status of async execution. This calls `QueryInterface` so should be used sparingly. |
| 30 | + fn status(&self) -> Result<AsyncStatus>; |
| 31 | + |
| 32 | + // Waits for the async execution to finish and then returns the results. |
| 33 | + fn join(&self) -> Result<Self::Output> { |
| 34 | + if self.status()? == AsyncStatus::Started { |
| 35 | + let (_waiter, signaler) = Waiter::new()?; |
| 36 | + self.set_completed(move |_| { |
| 37 | + // This is safe because the waiter will only be dropped after being signaled. |
| 38 | + unsafe { |
| 39 | + signaler.signal(); |
| 40 | + } |
| 41 | + })?; |
| 42 | + } |
| 43 | + self.get_results() |
| 44 | + } |
| 45 | + |
| 46 | + // Calls `op(result)` when async execution completes. |
| 47 | + fn when<F>(&self, op: F) -> Result<()> |
| 48 | + where |
| 49 | + F: FnOnce(Result<Self::Output>) + Send + 'static, |
| 50 | + { |
| 51 | + if self.status()? == AsyncStatus::Started { |
| 52 | + // The `set_completed` closure is guaranteed to only be called once, like `FnOnce`, by the async pattern, |
| 53 | + // but Rust doesn't know that so `RefCell` is used to pass `op` in to the closure. |
| 54 | + let op = core::cell::RefCell::new(Some(op)); |
| 55 | + self.set_completed(move |sender| { |
| 56 | + if let Some(op) = op.take() { |
| 57 | + op(sender.get_results()); |
| 58 | + } |
| 59 | + })?; |
| 60 | + } else { |
| 61 | + op(self.get_results()); |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Async for IAsyncAction { |
| 68 | + type Output = (); |
| 69 | + type CompletedHandler = AsyncActionCompletedHandler; |
| 70 | + |
| 71 | + fn set_completed<F: Fn(&Self) + Send + 'static>(&self, handler: F) -> Result<()> { |
| 72 | + self.SetCompleted(&AsyncActionCompletedHandler::new(move |sender, _| { |
| 73 | + handler(sender.ok()?); |
| 74 | + Ok(()) |
| 75 | + })) |
| 76 | + } |
| 77 | + |
| 78 | + #[cfg(feature = "std")] |
| 79 | + fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) { |
| 80 | + _ = handler.Invoke(self, status); |
| 81 | + } |
| 82 | + |
| 83 | + fn get_results(&self) -> Result<Self::Output> { |
| 84 | + self.GetResults() |
| 85 | + } |
| 86 | + |
| 87 | + fn status(&self) -> Result<AsyncStatus> { |
| 88 | + self.Status() |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<T: RuntimeType> Async for IAsyncOperation<T> { |
| 93 | + type Output = T; |
| 94 | + type CompletedHandler = AsyncOperationCompletedHandler<T>; |
| 95 | + |
| 96 | + fn set_completed<F: Fn(&Self) + Send + 'static>(&self, handler: F) -> Result<()> { |
| 97 | + self.SetCompleted(&AsyncOperationCompletedHandler::new(move |sender, _| { |
| 98 | + handler(sender.ok()?); |
| 99 | + Ok(()) |
| 100 | + })) |
| 101 | + } |
| 102 | + |
| 103 | + #[cfg(feature = "std")] |
| 104 | + fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) { |
| 105 | + _ = handler.Invoke(self, status); |
| 106 | + } |
| 107 | + |
| 108 | + fn get_results(&self) -> Result<Self::Output> { |
| 109 | + self.GetResults() |
| 110 | + } |
| 111 | + |
| 112 | + fn status(&self) -> Result<AsyncStatus> { |
| 113 | + self.Status() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<P: RuntimeType> Async for IAsyncActionWithProgress<P> { |
| 118 | + type Output = (); |
| 119 | + type CompletedHandler = AsyncActionWithProgressCompletedHandler<P>; |
| 120 | + |
| 121 | + fn set_completed<F: Fn(&Self) + Send + 'static>(&self, handler: F) -> Result<()> { |
| 122 | + self.SetCompleted(&AsyncActionWithProgressCompletedHandler::new( |
| 123 | + move |sender, _| { |
| 124 | + handler(sender.ok()?); |
| 125 | + Ok(()) |
| 126 | + }, |
| 127 | + )) |
| 128 | + } |
| 129 | + |
| 130 | + #[cfg(feature = "std")] |
| 131 | + fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) { |
| 132 | + _ = handler.Invoke(self, status); |
| 133 | + } |
| 134 | + |
| 135 | + fn get_results(&self) -> Result<Self::Output> { |
| 136 | + self.GetResults() |
| 137 | + } |
| 138 | + |
| 139 | + fn status(&self) -> Result<AsyncStatus> { |
| 140 | + self.Status() |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +impl<T: RuntimeType, P: RuntimeType> Async for IAsyncOperationWithProgress<T, P> { |
| 145 | + type Output = T; |
| 146 | + type CompletedHandler = AsyncOperationWithProgressCompletedHandler<T, P>; |
| 147 | + |
| 148 | + fn set_completed<F: Fn(&Self) + Send + 'static>(&self, handler: F) -> Result<()> { |
| 149 | + self.SetCompleted(&AsyncOperationWithProgressCompletedHandler::new( |
| 150 | + move |sender, _| { |
| 151 | + handler(sender.ok()?); |
| 152 | + Ok(()) |
| 153 | + }, |
| 154 | + )) |
| 155 | + } |
| 156 | + |
| 157 | + #[cfg(feature = "std")] |
| 158 | + fn invoke_completed(&self, handler: &Self::CompletedHandler, status: AsyncStatus) { |
| 159 | + _ = handler.Invoke(self, status); |
| 160 | + } |
| 161 | + |
| 162 | + fn get_results(&self) -> Result<Self::Output> { |
| 163 | + self.GetResults() |
| 164 | + } |
| 165 | + |
| 166 | + fn status(&self) -> Result<AsyncStatus> { |
| 167 | + self.Status() |
| 168 | + } |
| 169 | +} |
0 commit comments