mas_storage/
utils.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7//! Wrappers and useful type aliases
8
9use rand_core::CryptoRngCore;
10
11use crate::Clock;
12
13/// A wrapper which is used to map the error type of a repository to another
14pub struct MapErr<R, F> {
15    pub(crate) inner: R,
16    pub(crate) mapper: F,
17    _private: (),
18}
19
20impl<R, F> MapErr<R, F> {
21    /// Create a new [`MapErr`] wrapper from an inner repository and a mapper
22    /// function
23    #[must_use]
24    pub fn new(inner: R, mapper: F) -> Self {
25        Self {
26            inner,
27            mapper,
28            _private: (),
29        }
30    }
31}
32
33/// A boxed [`Clock`]
34pub type BoxClock = Box<dyn Clock + Send>;
35
36/// A boxed random number generator
37pub type BoxRng = Box<dyn CryptoRngCore + Send>;
38
39/// A macro to implement a repository trait for the [`MapErr`] wrapper and for
40/// [`Box<R>`]
41#[macro_export]
42macro_rules! repository_impl {
43    ($repo_trait:ident:
44        $(
45            async fn $method:ident (
46                &mut self
47                $(, $arg:ident: $arg_ty:ty )*
48                $(,)?
49            ) -> Result<$ret_ty:ty, Self::Error>;
50        )*
51    ) => {
52        #[::async_trait::async_trait]
53        impl<R: ?Sized> $repo_trait for ::std::boxed::Box<R>
54        where
55            R: $repo_trait,
56        {
57            type Error = <R as $repo_trait>::Error;
58
59            $(
60                async fn $method (&mut self $(, $arg: $arg_ty)*) -> Result<$ret_ty, Self::Error> {
61                    (**self).$method ( $($arg),* ).await
62                }
63            )*
64        }
65
66        #[::async_trait::async_trait]
67        impl<R, F, E> $repo_trait for $crate::MapErr<R, F>
68        where
69            R: $repo_trait,
70            F: FnMut(<R as $repo_trait>::Error) -> E + ::std::marker::Send + ::std::marker::Sync,
71        {
72            type Error = E;
73
74            $(
75                async fn $method (&mut self $(, $arg: $arg_ty)*) -> Result<$ret_ty, Self::Error> {
76                    self.inner.$method ( $($arg),* ).await.map_err(&mut self.mapper)
77                }
78            )*
79        }
80    };
81}