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
//! a type token that can be used to uniquly tag an instance of a type.
//!
//! these are singleton types, only a single instance
//! of a given token type may exist.
//! to make checking this at compile time possible, tokens cannot be returned
//! from functions.
/// marker trait that a type is a type token.
///
/// this trait is safe to use in trait bounds, but MUST NOT be implemented by any code
/// outside of the `typetoken` crate.
pub unsafe
/// The heart of crate, generates a token singleton.
///
/// Every macro use site corresponds to a unique type,
/// and creates a value of that type.
/// the generated type will implement [`Token`].
///
/// The value contains an exclusive borrow also local to the macro use site
/// (and thus the type contains the lifetime of that borrow),
/// therefore no two values of the same type can exist at the same time.
///
/// The downside of this approach is that the token cannot outlive the
/// stack frame of function it was defined in. for this reason, it is
/// generally advisable to construct tokens in `main()` if possible.
///
/// ## Examples
///
/// To get any use out of this crate, you generally need some generic interface
/// that takes two items of the tame generic type.
/// ```rust
/// # #[macro_use] extern crate typetoken;
/// use typetoken::Token;
/// fn assert_same_token<T: Token>(_: &T, _: &T) {}
/// fn main() {
/// token!(token1);
/// token!(token2);
/// assert_same_token(&token1, &token1);
/// assert_same_token(&token2, &token2);
/// }
/// ```
///
/// If used with a correctly designed interface, this can
/// catch programmer mistakes.
/// ```compile_fail
/// # #[macro_use] extern crate typetoken;
/// # use typetoken::Token;
/// # fn assert_same_token<T: Token>(_: &T, _: &T) {}
/// # fn main() {
/// token!(token1);
/// token!(token2);
/// assert_same_token(&token1, &token1);
/// assert_same_token(&token2, &token1); // oops, wrong token, compile error
/// # }
/// ```
///
/// Tokens should be unique in all circumstances, even if you do silly
/// things like define them on the same line
/// (this will make your error messages more confusing, so probably don't)
/// ```compile_fail
/// # #[macro_use] extern crate typetoken;
/// # use typetoken::Token;
/// # fn assert_same_token<T: Token>(_: &T, _: &T) {}
/// # fn main() {
/// token!(token1); token!(token2);
/// assert_same_token(&token1, &token1);
/// assert_same_token(&token2, &token1); // oops, wrong token, compile error
/// # }
/// ```
///
/// see the [`examples/`](https://codeberg.org/binarycat/typetoken/src/branch/trunk/examples)
/// directory for more practical usecases.