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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Marker types for limiting access.

/// Private trait that is implemented for the types in this module.
pub trait Access: Copy + Default {
    /// Ensures that this trait cannot be implemented outside of this crate.
    #[doc(hidden)]
    fn _private() -> _Private {
        _Private
    }

    /// Reduced access level to safely share the corresponding value.
    type RestrictShared: Access;
}

/// Helper trait that is implemented by [`ReadWrite`] and [`ReadOnly`].
pub trait Readable: Copy + Default {
    /// Reduced access level to safely share the corresponding value.
    type RestrictShared: Readable + Access;

    /// Ensures that this trait cannot be implemented outside of this crate.
    fn _private() -> _Private {
        _Private
    }
}

/// Helper trait that is implemented by [`ReadWrite`] and [`WriteOnly`].
pub trait Writable: Access {
    /// Ensures that this trait cannot be implemented outside of this crate.
    fn _private() -> _Private {
        _Private
    }
}

/// Implemented for access types that permit copying of `VolatileRef`.
pub trait Copyable {
    /// Ensures that this trait cannot be implemented outside of this crate.
    fn _private() -> _Private {
        _Private
    }
}

impl<T> Access for T
where
    T: Readable + Default + Copy,
{
    type RestrictShared = <T as Readable>::RestrictShared;
}

/// Zero-sized marker type for allowing both read and write access.
#[derive(Debug, Default, Copy, Clone)]
pub struct ReadWrite;
impl Readable for ReadWrite {
    type RestrictShared = ReadOnly;
}
impl Writable for ReadWrite {}

/// Zero-sized marker type for allowing only read access.
#[derive(Debug, Default, Copy, Clone)]
pub struct ReadOnly;
impl Readable for ReadOnly {
    type RestrictShared = ReadOnly;
}
impl Copyable for ReadOnly {}

/// Zero-sized marker type for allowing only write access.
#[derive(Debug, Default, Copy, Clone)]
pub struct WriteOnly;
impl Access for WriteOnly {
    type RestrictShared = NoAccess;
}
impl Writable for WriteOnly {}

/// Zero-sized marker type that grants no access.
#[derive(Debug, Default, Copy, Clone)]
pub struct NoAccess;
impl Access for NoAccess {
    type RestrictShared = NoAccess;
}
impl Copyable for NoAccess {}

#[non_exhaustive]
#[doc(hidden)]
pub struct _Private;