Skip to content

Commit

Permalink
Cancel futures on drop
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Jul 24, 2024
1 parent ff35ff3 commit 5cd9fd9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
3 changes: 3 additions & 0 deletions crates/libs/bindgen/src/rust/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,9 @@ impl Writer {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#features
impl<#constraints> std::future::IntoFuture for #ident {
Expand Down
20 changes: 13 additions & 7 deletions crates/libs/core/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ pub trait AsyncOperation {
/// Get the result value from a completed operation.
/// Wraps `self.GetResults()`.
fn get_results(&self) -> crate::Result<Self::Output>;
/// Attempts to cancel the operation. Any error is ignored.
/// Wraps `self.Cancel()`.
fn cancel(&self);
}

/// A wrapper around an `AsyncOperation` that implements `std::future::Future`.
/// This is used by generated `IntoFuture` impls. It shouldn't be necessary to use this type manually.
pub struct FutureWrapper<T> {
pub struct FutureWrapper<T: AsyncOperation> {
inner: T,
waker: Option<Arc<Mutex<Waker>>>,
}

impl<T> FutureWrapper<T> {
impl<T: AsyncOperation> FutureWrapper<T> {
/// Creates a `FutureWrapper`, which implements `std::future::Future`.
pub fn new(inner: T) -> Self {
Self {
inner,
waker: None,
}
Self { inner, waker: None }
}
}

impl<T> Unpin for FutureWrapper<T> {}
impl<T: AsyncOperation> Unpin for FutureWrapper<T> {}

impl<T: AsyncOperation> Future for FutureWrapper<T> {
type Output = crate::Result<T::Output>;
Expand All @@ -66,3 +66,9 @@ impl<T: AsyncOperation> Future for FutureWrapper<T> {
}
}
}

impl<T: AsyncOperation> Drop for FutureWrapper<T> {
fn drop(&mut self) {
self.inner.cancel();
}
}
18 changes: 18 additions & 0 deletions crates/libs/windows/src/Windows/Devices/Sms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,9 @@ impl windows_core::AsyncOperation for DeleteSmsMessageOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(feature = "deprecated")]
impl std::future::IntoFuture for DeleteSmsMessageOperation {
Expand Down Expand Up @@ -1160,6 +1163,9 @@ impl windows_core::AsyncOperation for DeleteSmsMessagesOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(feature = "deprecated")]
impl std::future::IntoFuture for DeleteSmsMessagesOperation {
Expand Down Expand Up @@ -1273,6 +1279,9 @@ impl windows_core::AsyncOperation for GetSmsDeviceOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(feature = "deprecated")]
impl std::future::IntoFuture for GetSmsDeviceOperation {
Expand Down Expand Up @@ -1386,6 +1395,9 @@ impl windows_core::AsyncOperation for GetSmsMessageOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(feature = "deprecated")]
impl std::future::IntoFuture for GetSmsMessageOperation {
Expand Down Expand Up @@ -1518,6 +1530,9 @@ impl windows_core::AsyncOperation for GetSmsMessagesOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(all(feature = "Foundation_Collections", feature = "deprecated"))]
impl std::future::IntoFuture for GetSmsMessagesOperation {
Expand Down Expand Up @@ -1628,6 +1643,9 @@ impl windows_core::AsyncOperation for SendSmsMessageOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
#[cfg(feature = "deprecated")]
impl std::future::IntoFuture for SendSmsMessageOperation {
Expand Down
12 changes: 12 additions & 0 deletions crates/libs/windows/src/Windows/Foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ impl windows_core::AsyncOperation for IAsyncAction {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl std::future::IntoFuture for IAsyncAction {
type Output = windows_core::Result<()>;
Expand Down Expand Up @@ -219,6 +222,9 @@ impl<TProgress: windows_core::RuntimeType + 'static> windows_core::AsyncOperatio
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl<TProgress: windows_core::RuntimeType + 'static> std::future::IntoFuture for IAsyncActionWithProgress<TProgress> {
type Output = windows_core::Result<()>;
Expand Down Expand Up @@ -396,6 +402,9 @@ impl<TResult: windows_core::RuntimeType + 'static> windows_core::AsyncOperation
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl<TResult: windows_core::RuntimeType + 'static> std::future::IntoFuture for IAsyncOperation<TResult> {
type Output = windows_core::Result<TResult>;
Expand Down Expand Up @@ -535,6 +544,9 @@ impl<TResult: windows_core::RuntimeType + 'static, TProgress: windows_core::Runt
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl<TResult: windows_core::RuntimeType + 'static, TProgress: windows_core::RuntimeType + 'static> std::future::IntoFuture for IAsyncOperationWithProgress<TResult, TProgress> {
type Output = windows_core::Result<TResult>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ impl windows_core::AsyncOperation for SignOutUserOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl std::future::IntoFuture for SignOutUserOperation {
type Output = windows_core::Result<()>;
Expand Down Expand Up @@ -623,6 +626,9 @@ impl windows_core::AsyncOperation for UserAuthenticationOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl std::future::IntoFuture for UserAuthenticationOperation {
type Output = windows_core::Result<UserIdentity>;
Expand Down
6 changes: 6 additions & 0 deletions crates/libs/windows/src/Windows/Storage/Streams/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,9 @@ impl windows_core::AsyncOperation for DataReaderLoadOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl std::future::IntoFuture for DataReaderLoadOperation {
type Output = windows_core::Result<u32>;
Expand Down Expand Up @@ -1629,6 +1632,9 @@ impl windows_core::AsyncOperation for DataWriterStoreOperation {
fn get_results(&self) -> windows_core::Result<Self::Output> {
self.GetResults()
}
fn cancel(&self) {
let _ = self.Cancel();
}
}
impl std::future::IntoFuture for DataWriterStoreOperation {
type Output = windows_core::Result<u32>;
Expand Down

0 comments on commit 5cd9fd9

Please sign in to comment.