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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`.

use crate::hkt::Marker;

use super::*;

/// Owned static `T`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct OwnedStatic<T: ?Sized>(pub T);

impl<'a, 'ctx, T> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for OwnedStatic<T>
where
    T: 'static + Send + Sync,
{
    type T = OwnedStatic<T>;
}

impl<'a, 'ctx, T> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for OwnedStatic<T>
where
    T: 'static + Send + Sync,
{
    type Higher = OwnedStatic<T>;
}

/// Borrowed static `T` for `'ctx`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct BorrowedStatic<'ctx, T: ?Sized>(pub &'ctx T);

pub struct BorrowedStaticHrt<T: ?Sized>(Marker<T>);

impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for BorrowedStaticHrt<T>
where
    T: 'static + Send + Sync,
{
    type T = BorrowedStatic<'ctx, T>;
}

impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
    for BorrowedStatic<'ctx, T>
where
    T: 'static + Send + Sync,
{
    type Higher = BorrowedStaticHrt<T>;
}

/// Borrowed static `T` for `'a`.
#[derive(PartialEq, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct TempBorrowedStatic<'a, T: ?Sized>(pub &'a T);

pub struct TempBorrowedStaticHrt<T: ?Sized>(Marker<T>);

impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
    for TempBorrowedStaticHrt<T>
where
    T: 'static + Send + Sync,
{
    type T = TempBorrowedStatic<'a, T>;
}

impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
    for TempBorrowedStatic<'a, T>
where
    T: 'static + Send + Sync,
{
    type Higher = TempBorrowedStaticHrt<T>;
}

/// Mutably borrowed static `T` for `'ctx`.
#[derive(PartialEq, Debug)]
#[repr(transparent)]
pub struct BorrowedMutStatic<'ctx, T: ?Sized>(pub &'ctx mut T);

pub struct BorrowedMutStaticHrt<T: ?Sized>(Marker<T>);

impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
    for BorrowedMutStaticHrt<T>
where
    T: 'static + Send + Sync,
{
    type T = BorrowedMutStatic<'ctx, T>;
}

impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
    for BorrowedMutStatic<'ctx, T>
where
    T: 'static + Send + Sync,
{
    type Higher = BorrowedMutStaticHrt<T>;
}

/// Mutably borrowed static `T` for `'a`.
#[derive(PartialEq, Debug)]
#[repr(transparent)]
pub struct TempBorrowedMutStatic<'a, T: ?Sized>(pub &'a mut T);

pub struct TempBorrowedMutStaticHrt<T: ?Sized>(Marker<T>);

impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()>
    for TempBorrowedMutStaticHrt<T>
where
    T: 'static + Send + Sync,
{
    type T = TempBorrowedMutStatic<'a, T>;
}

impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()>
    for TempBorrowedMutStatic<'a, T>
where
    T: 'static + Send + Sync,
{
    type Higher = TempBorrowedMutStaticHrt<T>;
}

/// Boxed static `T`.
#[cfg(feature = "alloc")]
#[repr(transparent)]
pub struct BoxedStatic<T: ?Sized>(pub Box<T>);

#[cfg(feature = "alloc")]
impl<'a, 'ctx, T: ?Sized> TypeName::MemberTypeForLt<'a, 'ctx, &'a &'ctx ()> for BoxedStatic<T>
where
    T: 'static + Send + Sync,
{
    type T = BoxedStatic<T>;
}

#[cfg(feature = "alloc")]
impl<'a, 'ctx, T: ?Sized> TypeName::LowerTypeWithBound<'a, 'ctx, &'a &'ctx ()> for BoxedStatic<T>
where
    T: 'static + Send + Sync,
{
    type Higher = BoxedStatic<T>;
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn owned_static_has_type_name() {
        let a = TypeNameId::of_value(&OwnedStatic(42_i32));
        let b = TypeNameId::of_value(&OwnedStatic(123_i32));
        let c = TypeNameId::of_value(&OwnedStatic(true));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    fn borrowed_static_has_type_name() {
        let a = TypeNameId::of_value(&BorrowedStatic(&42_i32));
        let b = TypeNameId::of_value(&BorrowedStatic(&123_i32));
        let c = TypeNameId::of_value(&BorrowedStatic(&true));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    fn temp_borrowed_static_has_type_name() {
        let a = TypeNameId::of_value(&TempBorrowedStatic(&42_i32));
        let b = TypeNameId::of_value(&TempBorrowedStatic(&123_i32));
        let c = TypeNameId::of_value(&TempBorrowedStatic(&true));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    fn borrowed_mut_static_has_type_name() {
        let mut a = 42_i32;
        let mut b = 123_i32;
        let mut c = true;

        let a = TypeNameId::of_value(&BorrowedMutStatic(&mut a));
        let b = TypeNameId::of_value(&BorrowedMutStatic(&mut b));
        let c = TypeNameId::of_value(&BorrowedMutStatic(&mut c));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    fn temp_borrowed_mut_static_has_type_name() {
        let mut a = 42_i32;
        let mut b = 123_i32;
        let mut c = true;

        let a = TypeNameId::of_value(&TempBorrowedMutStatic(&mut a));
        let b = TypeNameId::of_value(&TempBorrowedMutStatic(&mut b));
        let c = TypeNameId::of_value(&TempBorrowedMutStatic(&mut c));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn boxed_static_has_type_name() {
        let a = TypeNameId::of_value(&BoxedStatic(Box::new(42_i32)));
        let b = TypeNameId::of_value(&BoxedStatic(Box::new(123_i32)));
        let c = TypeNameId::of_value(&BoxedStatic(Box::new(true)));

        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_ne!(b, c);
    }
}