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
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
use std::marker::PhantomData;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::cmp::Ordering;
pub struct Invariant<T>(PhantomData<*mut T>);
unsafe impl<T> Send for Invariant<T> {}
unsafe impl<T> Sync for Invariant<T> {}
impl<T> Invariant<T> {
#[inline]
pub fn new() -> Self { Invariant(PhantomData) }
}
impl<T> Default for Invariant<T> {
#[inline]
fn default() -> Self { Invariant::new() }
}
impl<T> Copy for Invariant<T> {}
impl<T> Clone for Invariant<T> {
#[inline]
fn clone(&self) -> Self { Invariant::new() }
}
impl<T> fmt::Debug for Invariant<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("Invariant Type Marker")
}
}
impl<T> PartialEq<Invariant<T>> for Invariant<T> {
#[inline]
fn eq(&self, _: &Self) -> bool { true }
#[inline]
fn ne(&self, _: &Self) -> bool { false }
}
impl<T> PartialOrd<Invariant<T>> for Invariant<T> {
#[inline]
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
Some(Ordering::Equal)
}
#[inline]
fn lt(&self, _: &Self) -> bool { false }
#[inline]
fn le(&self, _: &Self) -> bool { true }
#[inline]
fn gt(&self, _: &Self) -> bool { false }
#[inline]
fn ge(&self, _: &Self) -> bool { true }
}
impl<T> Eq for Invariant<T> {}
impl<T> Ord for Invariant<T> {
#[inline]
fn cmp(&self, _: &Self) -> Ordering { Ordering::Equal }
}
impl<T> Hash for Invariant<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
().hash(state)
}
}
#[derive(Copy, Clone, Default, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct InvariantLifetime<'id>(Invariant<&'id ()>);
fn _assert_bounds() {
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
fn is_derived<T: Copy + Clone + fmt::Debug + PartialEq + Eq + PartialOrd + Ord + Hash + Default>() {}
struct Nothing;
is_send::<Invariant<*mut ()>>();
is_sync::<Invariant<*mut ()>>();
is_derived::<Invariant<Nothing>>();
fn lifetime<'a>() {
is_send::<InvariantLifetime<'a>>();
is_sync::<InvariantLifetime<'a>>();
is_derived::<InvariantLifetime<'a>>();
}
}