7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2022 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 | use std::{error::Error, fmt, time::Duration}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use futures_core::Future; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | #[cfg(target_family = "wasm")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | use futures_util::future::{Either, select}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | #[cfg(target_family = "wasm")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | use gloo_timers::future::TimeoutFuture; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | #[cfg(not(target_family = "wasm"))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | use tokio::time::timeout as tokio_timeout; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | /// Error type notifying that a timeout has elapsed. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | pub struct ElapsedError(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | impl fmt::Display for ElapsedError { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | write!(f, "time waiting for future has elapsed!") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | impl Error for ElapsedError {} |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | /// Wait for `future` to be completed. `future` needs to return |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | /// a `Result`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | /// If the given timeout has elapsed the method will stop waiting and return |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | /// an error. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | pub async fn timeout<F, T>(future: F, duration: Duration) -> Result<T, ElapsedError> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | F: Future<Output = T>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | #[cfg(not(target_family = "wasm"))] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | return tokio_timeout(duration, future).await.map_err(|_| ElapsedError()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | #[cfg(target_family = "wasm")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | let timeout_future = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | TimeoutFuture::new(u32::try_from(duration.as_millis()).expect("Overlong duration")); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | match select(std::pin::pin!(future), timeout_future).await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | Either::Left((res, _)) => Ok(res), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | Either::Right((_, _)) => Err(ElapsedError()), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | #[cfg(test)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | pub(crate) mod tests { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | use std::{future, time::Duration}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | use matrix_sdk_test_macros::async_test; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | use super::timeout; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | #[cfg(target_family = "wasm")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | #[async_test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | async fn test_without_timeout() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | timeout(future::ready(()), Duration::from_millis(100)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | .expect("future should have completed without ElapsedError"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | #[async_test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | async fn test_with_timeout() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | timeout(future::pending::<()>(), Duration::from_millis(100)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | .expect_err("future should return an ElapsedError"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | } |