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