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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! A [`Type`] based [`Identifier`]
//!
//! This works by making it `unsafe` to create a [`Type`].
//! Then by providing macro that safely creates a new type
//! and a `Type` identifier at the same time. Guaranteeing
//! that there is no duplication.
//!
//! You can also create a new `Type`, but then the burden
//! is on you to ensure uniqueness.

use core::{
    fmt,
    hash::{Hash, Hasher},
    marker::PhantomData,
};

use crate::{Identifier, Init, OneShotIdentifier, Token, Trivial};

struct Invariant<T>(fn() -> *mut T);
/// A type based [`Identifier`]
pub struct Type<T> {
    invariant: PhantomData<Invariant<T>>,
}

/// The [`Trivial`] [`Token`] generated by [`Type`]
pub struct TypeToken<T> {
    invariant: PhantomData<Invariant<T>>,
}

/// Create a new [`Type<T>`](Type)
///
/// Two calls to `scope` will never return the same type
/// (the difference is in the types)
///
/// ```rust, compile_fail
/// let a = pui_core::make_type_id!();
/// let b = pui_core::make_type_id!();
/// assert_eq!(a, b);
/// ```
///
/// ```rust
/// let a = pui_core::make_type_id!();
/// assert_eq!(a, a);
/// ```
///
/// You can also declare your own unit struct that
/// that will be used to generate a `Type`. Note:
/// this can only be used in positions where `let`
/// bindings are valid.
///
/// ```rust
/// pui_core::make_type_id! {
///     /// Documentation for Foo
///     #[derive(Debug, Clone, Copy)]
///     pub let Foo;
/// }
///
/// let token = Foo.token();
/// ```
///
/// Or in positions where `static` is valid.
///
/// ```rust
/// pui_core::make_type_id! {
///     /// Documentation for Foo
///     #[derive(Debug, Clone, Copy)]
///     pub static Foo;
/// }
///
/// let token = Foo.token();
/// ```
#[macro_export]
macro_rules! make_type_id {
    () => {
        unsafe { struct Anonomous; $crate::ty::Type::__internal_macro_new__(&Anonomous) }
    };
    (
        $(#[$meta:meta])*
        $v:vis let $name:ident;
    ) => {
        $(#[$meta])*
        #[allow(non_camel_case_types)]
        $v struct $name {}
        let $name = unsafe { $crate::ty::Type::__internal_macro_new__(&$name {}) };
    };
    (
        $(#[$meta:meta])*
        $v:vis static $name:ident;
    ) => {
        $(#[$meta])*
        #[allow(non_camel_case_types)]
        $v struct $name {}
        static $name: $crate::ty::Type<$name> = unsafe { $crate::ty::Type::__internal_macro_new__(&$name {}) };
    };
}

impl<T> Default for TypeToken<T> {
    fn default() -> Self { Self { invariant: PhantomData } }
}

impl<T> Type<T> {
    #[doc(hidden)]
    pub const unsafe fn __internal_macro_new__(_: &T) -> Self { Self::new_unchecked() }

    /// Create a new `Type<T>`
    ///
    /// # Safety
    ///
    /// There should be only one `Type` created for any given `T`
    pub const unsafe fn new_unchecked() -> Self { Self { invariant: PhantomData } }

    /// Create a new token
    #[inline]
    pub const fn token(&self) -> TypeToken<T> { TypeToken::new() }
}

impl<T> TypeToken<T> {
    /// Create a new token
    pub const fn new() -> Self { Self { invariant: PhantomData } }
}

impl<T> Trivial for TypeToken<T> {}
unsafe impl<T> Token for TypeToken<T> {}
unsafe impl<T> OneShotIdentifier for Type<T> {}
unsafe impl<T> Identifier for Type<T> {
    type Token = TypeToken<T>;

    #[inline]
    fn owns_token(&self, _: &Self::Token) -> bool { true }

    #[inline]
    fn token(&self) -> Self::Token { TypeToken::new() }
}

impl<T> Init for TypeToken<T> {
    const INIT: Self = Self { invariant: PhantomData };
}

impl<T> fmt::Debug for TypeToken<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "TypeToken<{}>", core::any::type_name::<T>()) }
}

impl<T> Copy for TypeToken<T> {}
impl<T> Clone for TypeToken<T> {
    fn clone(&self) -> Self { *self }
}

impl<T> Eq for TypeToken<T> {}
impl<T> PartialEq for TypeToken<T> {
    fn eq(&self, _: &Self) -> bool { true }
}

impl<T> PartialOrd for TypeToken<T> {
    fn partial_cmp(&self, _: &Self) -> Option<core::cmp::Ordering> { Some(core::cmp::Ordering::Equal) }
}
impl<T> Ord for TypeToken<T> {
    fn cmp(&self, _: &Self) -> core::cmp::Ordering { core::cmp::Ordering::Equal }
}

impl<T> Hash for TypeToken<T> {
    fn hash<H: Hasher>(&self, state: &mut H) { ().hash(state) }
}

impl<T> fmt::Debug for Type<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Type<{}>", core::any::type_name::<T>()) }
}

impl<T> Eq for Type<T> {}
impl<T> PartialEq for Type<T> {
    fn eq(&self, _: &Self) -> bool { true }
}

impl<T> PartialOrd for Type<T> {
    fn partial_cmp(&self, _: &Self) -> Option<core::cmp::Ordering> { Some(core::cmp::Ordering::Equal) }
}
impl<T> Ord for Type<T> {
    fn cmp(&self, _: &Self) -> core::cmp::Ordering { core::cmp::Ordering::Equal }
}

impl<T> Hash for Type<T> {
    fn hash<H: Hasher>(&self, state: &mut H) { ().hash(state) }
}