mutability_marker/
lib.rs

1#![no_std]
2
3//! This crate provides two common marker types, `Const` and `Mut`, for use in distinguishing
4//! between mutable and immutable program state.
5
6use core::ops::*;
7
8/// Marks an immutable type.
9#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Const;
11
12/// Marks a mutable type.
13#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct Mut;
15
16/// Marks whether a type is mutable or immutable.
17pub trait Mutability: private::MutabilityInner {
18    /// The reference type associated with this mutability.
19    type Ref<'a, T: 'a + ?Sized>: Deref<Target = T>;
20}
21
22impl Mutability for Const {
23    type Ref<'a, T: 'a + ?Sized> = &'a T;
24}
25
26impl Mutability for Mut {
27    type Ref<'a, T: 'a + ?Sized> = &'a mut T;
28}
29
30/// Hides implementation details.
31mod private {
32    use super::*;
33
34    /// Seals the mutability marker types.
35    pub trait MutabilityInner: 'static + Copy + Clone + Send + Sync + core::fmt::Debug + Default + PartialEq + Eq + PartialOrd + Ord + core::hash::Hash {}
36
37    impl MutabilityInner for Const {}
38    impl MutabilityInner for Mut {}
39}