atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git
bindings/matrix-sdk-ffi/src/runtime.rsRust110 lines3.6 KB
1
// Copyright 2025 The Matrix.org Foundation C.I.C.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
//! Runtime abstractions for cross-platform async execution. This provides
16
//! a stand-in for tokio's `get_runtime_handle` method that will work on
17
//! both Wasm and non-Wasm platforms. It also provides corresponding types
18
//! that can be used in place of tokio's `Handle` and `Runtime` types.
19
20
#[cfg(not(target_family = "wasm"))]
21
mod sys {
22
    pub use tokio::runtime::Handle;
23
24
    /// Get a runtime handle appropriate for the current target platform.
25
    ///
26
    /// This function returns a unified `Handle` type that works across both
27
    /// Wasm and non-Wasm platforms, allowing code to be written that is
28
    /// agnostic to the platform-specific runtime implementation.
29
    ///
30
    /// Returns:
31
    /// - A `tokio::runtime::Handle` on non-Wasm platforms
32
    /// - A `WasmRuntimeHandle` on Wasm platforms
33
    pub fn get_runtime_handle() -> Handle {
34
        async_compat::get_runtime_handle()
35
    }
36
}
37
38
#[cfg(target_family = "wasm")]
39
mod sys {
40
    use std::future::Future;
41
42
    use matrix_sdk_common::executor::{JoinHandle, spawn};
43
44
    /// A dummy guard that does nothing when dropped.
45
    /// This is used for the Wasm implementation to match
46
    /// tokio::runtime::EnterGuard.
47
    #[derive(Debug)]
48
    pub struct RuntimeGuard;
49
50
    /// A runtime handle implementation for WebAssembly targets.
51
    ///
52
    /// This implements a minimal subset of the tokio::runtime::Handle API
53
    /// that is needed for the matrix-rust-sdk to function on Wasm.
54
    #[derive(Default, Debug)]
55
    pub struct Handle;
56
    pub type Runtime = Handle;
57
58
    impl Handle {
59
        /// Spawns a future in the wasm32 bindgen runtime.
60
        #[track_caller]
61
        pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
62
        where
63
            F: Future + 'static,
64
            F::Output: 'static,
65
        {
66
            spawn(future)
67
        }
68
69
        /// Runs the provided function on an executor dedicated to blocking
70
        /// operations.
71
        #[track_caller]
72
        pub fn spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>
73
        where
74
            F: FnOnce() -> R + 'static,
75
            R: 'static,
76
        {
77
            spawn(async move { func() })
78
        }
79
80
        /// Runs a future to completion on the current thread.
81
        pub fn block_on<F, T>(&self, future: F) -> T
82
        where
83
            F: Future<Output = T>,
84
        {
85
            futures_executor::block_on(future)
86
        }
87
88
        /// Enters the runtime context.
89
        ///
90
        /// For WebAssembly, this is a no-op that returns a dummy guard.
91
        pub fn enter(&self) -> RuntimeGuard {
92
            RuntimeGuard
93
        }
94
    }
95
96
    /// Get a runtime handle appropriate for the current target platform.
97
    ///
98
    /// This function returns a unified `Handle` type that works across both
99
    /// Wasm and non-Wasm platforms, allowing code to be written that is
100
    /// agnostic to the platform-specific runtime implementation.
101
    ///
102
    /// Returns:
103
    /// - A `tokio::runtime::Handle` on non-Wasm platforms
104
    /// - A `WasmRuntimeHandle` on Wasm platforms
105
    pub fn get_runtime_handle() -> Handle {
106
        Handle
107
    }
108
}
109
110
pub use sys::*;