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
use TokenStream;
/// Generate boilerplate for a trait and unit structs to represent a set of possible states
///
/// ```
/// # fn main() {}
/// # use typetrait::union;
/// union! {
/// pub Status = Validated | Unvalidated
/// }
/// ```
/// This generates a trait `Status` and two unit structs `Validated` and `Unvalidated` that
/// implement `Status`. These types can then be used to provide a more type-safe API.
///
/// For example, consider a struct `Data` that represents some user input from a form:
/// ```
/// # fn main() {}
/// struct Data {
/// name: String,
/// age: u8,
/// }
/// ```
/// However, you might want to perform some validation on this struct before using it. To enforce
/// this, you can make it generic over `T: Status` to represent its validation status:
/// ```
/// # fn main() {}
/// # use typetrait::union;
/// union! {
/// pub Status = Validated | Unvalidated
/// }
///
/// struct Data<T: Status> {
/// _marker: std::marker::PhantomData<T>,
/// name: String,
/// age: u8,
/// }
/// ```
/// You might then have an API that looks like:
/// ```
/// # fn main() {}
/// # use typetrait::union;
/// # union! {
/// # pub Status = Validated | Unvalidated
/// # }
/// # struct Data<T: Status> {
/// # _marker: std::marker::PhantomData<T>,
/// # name: String,
/// # age: u8,
/// # }
/// fn get_data() -> Data<Unvalidated> {
/// todo!()
/// }
///
/// fn validate(data: Data<Unvalidated>) -> Data<Validated> {
/// todo!()
/// }
///
/// fn use_valid_data(data: Data<Validated>) {
/// todo!()
/// }
/// ```
/// With this API, using unvalidated data can now be prevented at compile time.
/// Generate blanket implementations for traits
///
/// For example:
/// ```
/// # use typetrait::blanket;
/// blanket!{ pub ThreadSafe = Send + Sync + 'static }
/// ```
/// expands (roughly) to:
/// ```
/// pub trait ThreadSafe: Send + Sync + 'static {}
/// impl<T> ThreadSafe for T where T: Send + Sync + 'static {}
/// ```