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
217
218
219
220
221
222
223
224
225
//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`.

use super::*;

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

bijective_higher_ranked_type! {
    /// Higher ranked type for [`OwnedStatic`].
    pub for['ctx] type DynOwnedStatic[][T]: WithContextLt['ctx][]
    for<'a>
    (OwnedStatic<T>)
    where {
        T: ?Sized + 'ctx,
    }
}

bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynOwnedStatic<T>)
    where {
        T: ?Sized + 'static
    }
}

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

bijective_higher_ranked_type! {
    /// Higher ranked type for [`BorrowedStatic`].
    pub type DynBorrowedStatic['ctx][T]: WithContextLt['ctx][]
    for<'a>
    (BorrowedStatic<'ctx, T>)
    where {
        T: ?Sized + 'ctx,
    }
}

bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynBorrowedStatic<'ctx, T>)
    where {
        T: ?Sized + 'static
    }
}

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

bijective_higher_ranked_type! {
    /// Higher ranked type for [`TempBorrowedStatic`].
    pub type DynTempBorrowedStatic['ctx][T]: WithContextLt['ctx][]
    for<'a>
    (TempBorrowedStatic<'a, T>)
    where {
        T: ?Sized + 'ctx,
    }
}

bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynTempBorrowedStatic<'ctx, T>)
    where {
        T: ?Sized + 'static
    }
}

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

bijective_higher_ranked_type! {
    /// Higher ranked type for [`BorrowedMutStatic`].
    pub type DynBorrowedMutStatic['ctx][T]: WithContextLt['ctx][]
    for<'a>
    (BorrowedMutStatic<'ctx, T>)
    where {
        T: ?Sized + 'ctx,
    }
}

bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynBorrowedMutStatic<'ctx, T>)
    where {
        T: ?Sized + 'static
    }
}

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

bijective_higher_ranked_type! {
    /// Higher ranked type for [`TempBorrowedMutStatic`].
    pub type DynTempBorrowedMutStatic['ctx][T]: WithContextLt['ctx][]
    for<'a>
    (TempBorrowedMutStatic<'a, T>)
    where {
        T: ?Sized + 'ctx,
    }
}

bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynTempBorrowedMutStatic<'ctx, T>)
    where {
        T: ?Sized + 'static
    }
}

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

#[cfg(feature = "alloc")]
bijective_higher_ranked_type! {
    /// Higher ranked type for [`BoxedStatic`].
    pub for['ctx] type DynBoxedStatic[][T]: WithContextLt['ctx][]
    for<'a>
    (BoxedStatic<T>)
    where {
        T: ?Sized + 'ctx,
    }
}

#[cfg(feature = "alloc")]
bijective_higher_ranked_type! {
    pub type [][T][]: TypeName[][]
    for<'ctx>
    (DynBoxedStatic<T>)
    where {
        T: ?Sized + 'static
    }
}

#[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);
    }
}