1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use crate::{ReadApi, WriteApi};

impl<T> ReadApi for &T
    where
        T: AutoReadApi
{
    type Target = <T as AutoReadApi>::Target;
    type ReadGuard<'a> = &'a <T as AutoReadApi>::Target
        where Self: 'a;

    #[inline(always)]
    fn read(&self) -> &Self::Target {
        self.get()
    }
}

impl<T> ReadApi for &mut T
    where
        T: AutoReadApi
{
    type Target = <T as AutoReadApi>::Target;
    type ReadGuard<'a> = &'a <T as AutoReadApi>::Target
        where Self: 'a;

    #[inline(always)]
    fn read(&self) -> &Self::Target {
        self.get()
    }
}

impl<T> WriteApi for &mut T
    where
        T: AutoWriteApi
{
    type Target = <T as AutoWriteApi>::Target;
    type WriteGuard<'a> = &'a mut <T as AutoWriteApi>::Target
        where Self: 'a;

    #[inline(always)]
    fn write(&mut self) -> &mut Self::Target {
        self.get_mut()
    }
}

/// Marker trait used to tell the compiler to automatically implement [`ReadApi`]
/// for references to instances of the type.
/// See the `Examples` section of the [`ReadWriteApi`](crate::ReadWriteApi) documentation.
pub trait AutoReadApi {
    /// Type to be used as a read guard.
    type Target;
    /// Returns read guard for [`Self`].
    fn get(&self) -> &Self::Target;
}

/// Marker trait used to tell the compiler to automatically implement [`WriteApi`]
/// for references to instances of the type.
/// See the `Examples` section of the [`ReadWriteApi`](crate::ReadWriteApi) documentation.
pub trait AutoWriteApi {
    /// Resulting write guard for the type.
    type Target;
    /// Returns write guard for [`Self`].
    fn get_mut(&mut self) -> &mut Self::Target;
}