7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2023 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::{mem::ManuallyDrop, ops::Deref}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use ruma::{MilliSecondsSinceUnixEpoch, UInt}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | use tracing::warn; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | use crate::runtime::get_runtime_handle; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | #[derive(Debug, Clone)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | pub struct Timestamp(u64); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | impl From<MilliSecondsSinceUnixEpoch> for Timestamp { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | fn from(date: MilliSecondsSinceUnixEpoch) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | Self(date.0.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | uniffi::custom_newtype!(Timestamp, u64); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | pub(crate) fn u64_to_uint(u: u64) -> UInt { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | UInt::new(u).unwrap_or_else(|| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | warn!("u64 -> UInt conversion overflowed, falling back to UInt::MAX"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | UInt::MAX |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | /// Tiny wrappers for data types that must be dropped in the context of an async |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | /// runtime. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | /// This is useful whenever such a data type may transitively call some |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | /// runtime's `block_on` function in their `Drop` impl (since we lack async drop |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | /// at the moment), like done in some `deadpool` drop impls. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | pub(crate) struct AsyncRuntimeDropped<T>(ManuallyDrop<T>); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | impl<T> AsyncRuntimeDropped<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | /// Create a new wrapper for this type that will be dropped under an async |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | /// runtime. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | pub fn new(val: T) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | Self(ManuallyDrop::new(val)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | impl<T> Drop for AsyncRuntimeDropped<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | fn drop(&mut self) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | let _guard = get_runtime_handle().enter(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | // SAFETY: self.inner is never used again, which is the only requirement |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | // for ManuallyDrop::drop to be used safely. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | unsafe { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | ManuallyDrop::drop(&mut self.0); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | // What is an `AsyncRuntimeDropped<T>`, if not a `T` in disguise? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | impl<T> Deref for AsyncRuntimeDropped<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | type Target = T; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | fn deref(&self) -> &Self::Target { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | &self.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | impl<T: Clone> Clone for AsyncRuntimeDropped<T> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | fn clone(&self) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | Self(self.0.clone()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | } |