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
//! Wrapper types that impl [`TypeName`] when their generic type `T` is `'static`.

use crate::{higher_ranked_type, hkt::Marker};

use super::*;

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

higher_ranked_type! {
    impl TypeName {
        impl['a, 'ctx, T] type T['a, 'ctx] for OwnedStatic<T> = OwnedStatic<T> where {
            T: ?Sized + 'static
        };

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for OwnedStatic<T> = OwnedStatic<T> where {
            T: ?Sized + 'static
        };
    }
}

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

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

higher_ranked_type! {
    impl TypeName {
        impl['a, 'ctx, T] type T['a, 'ctx] for BorrowedStaticHrt<T> = BorrowedStatic<'ctx, T> where {
            T: ?Sized + 'static
        };

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for BorrowedStatic<'ctx, T> = BorrowedStaticHrt<T> where {
            T: ?Sized + 'static
        };
    }
}

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

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

higher_ranked_type! {
    impl TypeName {
        impl['a, 'ctx, T] type T['a, 'ctx] for TempBorrowedStaticHrt<T> = TempBorrowedStatic<'a, T> where {
            T: ?Sized + 'static
        };

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for TempBorrowedStatic<'a, T> = TempBorrowedStaticHrt<T> where {
            T: ?Sized + 'static
        };
    }
}

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

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

higher_ranked_type! {
    impl TypeName {
        impl['a, 'ctx, T] type T['a, 'ctx] for BorrowedMutStaticHrt<T> = BorrowedMutStatic<'ctx, T> where {
            T: ?Sized + 'static
        };

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for BorrowedMutStatic<'ctx, T> = BorrowedMutStaticHrt<T> where {
            T: ?Sized + 'static
        };
    }
}

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

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

higher_ranked_type! {
    impl TypeName {
        impl['a, 'ctx, T] type T['a, 'ctx] for TempBorrowedMutStaticHrt<T> = TempBorrowedMutStatic<'a, T> where {
            T: ?Sized + 'static
        };

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for TempBorrowedMutStatic<'a, T> = TempBorrowedMutStaticHrt<T> where {
            T: ?Sized + 'static
        };
    }
}

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

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

        impl['a, 'ctx, T] type HigherRanked['a, 'ctx] for BoxedStatic<T> = BoxedStatic<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);
    }
}