|
| 1 | +//! Marker types for limiting access. |
| 2 | +
|
| 3 | +/// Private trait that is implemented for the types in this module. |
| 4 | +pub trait Access: Copy + Default { |
| 5 | + /// Ensures that this trait cannot be implemented outside of this crate. |
| 6 | + #[doc(hidden)] |
| 7 | + fn _private() -> _Private { |
| 8 | + _Private |
| 9 | + } |
| 10 | + |
| 11 | + /// Reduced access level to safely share the corresponding value. |
| 12 | + type RestrictShared: Access; |
| 13 | +} |
| 14 | + |
1 | 15 | /// Helper trait that is implemented by [`ReadWrite`] and [`ReadOnly`].
|
2 |
| -pub trait Readable {} |
| 16 | +pub trait Readable: Copy + Default { |
| 17 | + /// Reduced access level to safely share the corresponding value. |
| 18 | + type RestrictShared: Readable + Access; |
| 19 | + |
| 20 | + /// Ensures that this trait cannot be implemented outside of this crate. |
| 21 | + fn _private() -> _Private { |
| 22 | + _Private |
| 23 | + } |
| 24 | +} |
3 | 25 |
|
4 | 26 | /// Helper trait that is implemented by [`ReadWrite`] and [`WriteOnly`].
|
5 |
| -pub trait Writable {} |
| 27 | +pub trait Writable: Access { |
| 28 | + /// Ensures that this trait cannot be implemented outside of this crate. |
| 29 | + fn _private() -> _Private { |
| 30 | + _Private |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/// Implemented for access types that permit copying of `VolatileRef`. |
| 35 | +pub trait Copyable { |
| 36 | + /// Ensures that this trait cannot be implemented outside of this crate. |
| 37 | + fn _private() -> _Private { |
| 38 | + _Private |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl<T> Access for T |
| 43 | +where |
| 44 | + T: Readable + Default + Copy, |
| 45 | +{ |
| 46 | + type RestrictShared = <T as Readable>::RestrictShared; |
| 47 | +} |
6 | 48 |
|
7 | 49 | /// Zero-sized marker type for allowing both read and write access.
|
8 |
| -#[derive(Debug, Copy, Clone)] |
| 50 | +#[derive(Debug, Default, Copy, Clone)] |
9 | 51 | pub struct ReadWrite;
|
10 |
| -impl Readable for ReadWrite {} |
| 52 | +impl Readable for ReadWrite { |
| 53 | + type RestrictShared = ReadOnly; |
| 54 | +} |
11 | 55 | impl Writable for ReadWrite {}
|
12 | 56 |
|
13 | 57 | /// Zero-sized marker type for allowing only read access.
|
14 |
| -#[derive(Debug, Copy, Clone)] |
| 58 | +#[derive(Debug, Default, Copy, Clone)] |
15 | 59 | pub struct ReadOnly;
|
16 |
| - |
17 |
| -impl Readable for ReadOnly {} |
| 60 | +impl Readable for ReadOnly { |
| 61 | + type RestrictShared = ReadOnly; |
| 62 | +} |
| 63 | +impl Copyable for ReadOnly {} |
18 | 64 |
|
19 | 65 | /// Zero-sized marker type for allowing only write access.
|
20 |
| -#[derive(Debug, Copy, Clone)] |
| 66 | +#[derive(Debug, Default, Copy, Clone)] |
21 | 67 | pub struct WriteOnly;
|
| 68 | +impl Access for WriteOnly { |
| 69 | + type RestrictShared = NoAccess; |
| 70 | +} |
22 | 71 | impl Writable for WriteOnly {}
|
| 72 | + |
| 73 | +/// Zero-sized marker type that grants no access. |
| 74 | +#[derive(Debug, Default, Copy, Clone)] |
| 75 | +pub struct NoAccess; |
| 76 | +impl Access for NoAccess { |
| 77 | + type RestrictShared = NoAccess; |
| 78 | +} |
| 79 | +impl Copyable for NoAccess {} |
| 80 | + |
| 81 | +#[non_exhaustive] |
| 82 | +#[doc(hidden)] |
| 83 | +pub struct _Private; |
0 commit comments