mssf_core/sync/
mod.rs

1// ------------------------------------------------------------
2// Copyright (c) Microsoft Corporation.  All rights reserved.
3// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
4// ------------------------------------------------------------
5
6// this contains some experiments for async
7#![allow(non_snake_case)]
8
9use std::cell::Cell;
10
11use mssf_com::FabricCommon::{
12    IFabricAsyncOperationCallback, IFabricAsyncOperationCallback_Impl, IFabricAsyncOperationContext,
13};
14use windows_core::implement;
15
16mod token;
17pub mod wait;
18pub use token::SimpleCancelToken;
19
20// This is intentional private. User should directly use bridge mod.
21mod bridge_context;
22pub use bridge_context::BridgeContext;
23
24mod channel;
25pub use channel::{FabricReceiver, FabricSender, oneshot_channel};
26
27mod proxy;
28pub use proxy::fabric_begin_end_proxy;
29
30// fabric code begins here
31
32pub trait Callback: FnOnce(windows_core::Ref<IFabricAsyncOperationContext>) + 'static {}
33impl<T: FnOnce(windows_core::Ref<IFabricAsyncOperationContext>) + 'static> Callback for T {}
34
35// TODO: rename.
36// Fabric Callback that wraps an arbitrary Fn closure.
37// Used primarily for bridging Begin and End fabric functions.
38#[implement(IFabricAsyncOperationCallback)]
39pub struct AwaitableCallback<F>
40where
41    F: Callback,
42{
43    callback: Cell<Option<F>>,
44}
45
46impl<F: Callback> IFabricAsyncOperationCallback_Impl for AwaitableCallback_Impl<F> {
47    // notify the function has been invoked.
48    fn Invoke(&self, context: windows_core::Ref<IFabricAsyncOperationContext>) {
49        let cb_opt = self.callback.take();
50        match cb_opt {
51            Some(cb) => {
52                cb(context);
53            }
54            None => {
55                unreachable!("Invoke has been run already");
56            }
57        }
58    }
59}
60
61impl<F: Callback> AwaitableCallback<F> {
62    /// Creates a new obj and convert to the COM interface type.
63    pub fn new_interface(callback: F) -> IFabricAsyncOperationCallback {
64        let a = AwaitableCallback {
65            callback: Cell::new(Some(callback)),
66        };
67        a.into()
68    }
69}
70
71#[cfg(test)]
72mod test {
73    use mssf_com::FabricClient::IFabricClusterManagementClient3;
74
75    #[test]
76    fn local_client_create() {
77        let _mgmt = crate::client::FabricClient::builder()
78            .build_interface::<IFabricClusterManagementClient3>()
79            .unwrap();
80    }
81}