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
226
227
228
229
230
231
232
233
//! This crate provides a derive macro that takes care of boilerplate code to make transitive
//! conversions in Rust using [`From`] and [`TryFrom`] traits.
//!
//! It's not magic and it's completely static. The derive here merely implements [`From`] or
//! [`TryFrom`] between types by relying on existent impls between items in the path.
//!
//! The path taken for transitions must be annotated (correctly) for transitions to work.
//! Additonally, there can only be one transition between a source and a target type, as otherwise
//! there would be duplicate trait implementations.
//!
//! The path is provided in the [`#[transitive]`] attribute along with a direction:
//!
//! ```ignore, compile_fail
//! #[derive(Transitive)]
//! #[transitive(from(D, C, B))] // Results in `impl From<D> for A` as `D -> C -> B -> A`
//! #[transitive(into(B, C, D))] // Results in `impl From<A> for D` as `A -> B -> C -> D`
//! struct A;
//! ```
//!
//! # Conversions table:
//!
//! | Derived Type | Annotation | Will impl | Conditions |
//! |--------------|-----------------------------------|---------------------|---------------------------------------------------------------------------------------------------------------------------|
//! | A | #[transitive(into(B, C, D))] | `From<A> for D` | `From<A> for B`; `From<B> for C`; `From<C> for D` |
//! | A | #[transitive(from(D, C, B))] | `From<D> for A` | `From<D> for C`; `From<C> for B`; `From<B> for A` |
//! | A | #[transitive(try_into(B, C, D))] | `TryFrom<A> for D` | `TryFrom<A> for B`; `TryFrom<B> for C`; `TryFrom<C> for D`; errors must impl `From<ErrType> for <D as TryFrom<C>>::Error` |
//! | A | #[transitive(try_from(D, C, B))] | `TryFrom<D> for A` | `TryFrom<D> for C`; `TryFrom<C> for B`; `TryFrom<B> for A`; errors must impl `From<ErrType> for <A as TryFrom<B>>::Error` |
//!
//!
//! # Custom error type:
//!
//! For `try_from` and `try_into` annotations, the macro attribute can accept an `error = MyError`
//! argument as the last element, like so: `#[transitive(try_into(A, B, C, error = MyError))]`. This
//! overrides the default behavior and allows specifying a custom error type, but all the error
//! types resulting from conversions must be convertible to this type.
//!
//! # Examples:
//!
//! ```
//! use transitive::Transitive;
//!
//! #[derive(Transitive)]
//! #[transitive(into(B, C, D))] // impl From<A> for D by doing A -> B -> C -> D
//! struct A;
//!
//! #[derive(Transitive)]
//! #[transitive(into(C, D))] // impl From<B> for D by doing B -> C -> D
//! struct B;
//! struct C;
//! struct D;
//!
//! impl From<A> for B {
//! fn from(val: A) -> Self {
//! Self
//! }
//! };
//!
//! impl From<B> for C {
//! fn from(val: B) -> Self {
//! Self
//! }
//! };
//!
//! impl From<C> for D {
//! fn from(val: C) -> Self {
//! Self
//! }
//! };
//!
//! D::from(A);
//! D::from(B);
//! ```
//!
//! Note that the macro does nothing for types in the middle:
//!
//! ```compile_fail
//! use transitive::Transitive;
//!
//! #[derive(Transitive)]
//! #[transitive(into(B, C, D))] // impl From<A> for D by doing A -> B -> C -> D
//! struct A;
//! struct B;
//! struct C;
//! struct D;
//!
//! impl From<A> for B {
//! fn from(val: A) -> Self {
//! Self
//! }
//! };
//!
//! impl From<B> for C {
//! fn from(val: B) -> Self {
//! Self
//! }
//! };
//!
//! impl From<C> for D {
//! fn from(val: C) -> Self {
//! Self
//! }
//! };
//!
//! D::from(A); // works
//! C::from(A); // does not compile
//! ```
//!
//! ```
//! use transitive::Transitive;
//!
//! #[derive(Transitive)]
//! #[transitive(into(B, C))] // impl From<A> for C by doing A -> B -> C
//! #[transitive(into(C, D))] // impl From<A> for D by doing A -> C -> D
//! struct A;
//! struct B;
//! struct C;
//! struct D;
//!
//! impl From<A> for B {
//! fn from(val: A) -> Self {
//! Self
//! }
//! };
//!
//! impl From<B> for C {
//! fn from(val: B) -> Self {
//! Self
//! }
//! };
//!
//! impl From<C> for D {
//! fn from(val: C) -> Self {
//! Self
//! }
//! };
//!
//! D::from(A);
//! ```
//!
//! Let's see an example on how to use [`Transitive`] when combining the "reversed"
//! nature of the `from` and `try_from` attribute modifiers and the error transitions constraints:
//!
//! ```
//! #![allow(non_camel_case_types)]
//! use transitive::Transitive;
//!
//! // Note how the annotation now considers `A` as target type
//! // and `D`, the first element in the type list, as source.
//! #[derive(Transitive)]
//! #[transitive(try_from(D, C, B))] // impl TryFrom<D> for A
//! struct A;
//!
//! #[derive(Transitive)]
//! #[transitive(try_from(D, C, error = ConvErr))] // impl TryFrom<D> for B, with custom error
//! struct B;
//! struct C;
//! struct D;
//!
//! struct ConvErr;
//!
//! struct ErrD_C;
//! struct ErrC_B;
//!
//! #[derive(Transitive)]
//! #[transitive(from(ErrD_C, ErrC_B))] // impl From<ErrD_C> for ErrB_A
//! struct ErrB_A;
//!
//! impl From<ErrD_C> for ErrC_B {
//! fn from(val: ErrD_C) -> Self {
//! Self
//! }
//! };
//!
//! impl From<ErrC_B> for ErrB_A {
//! fn from(val: ErrC_B) -> Self {
//! Self
//! }
//! };
//!
//! impl From<ErrD_C> for ConvErr {
//! fn from(val: ErrD_C) -> Self {
//! Self
//! }
//! };
//!
//! impl From<ErrC_B> for ConvErr {
//! fn from(val: ErrC_B) -> Self {
//! Self
//! }
//! };
//!
//! impl TryFrom<D> for C {
//! type Error = ErrD_C;
//!
//! fn try_from(val: D) -> Result<Self, Self::Error> {
//! Ok(Self)
//! }
//! };
//!
//! impl TryFrom<C> for B {
//! type Error = ErrC_B;
//!
//! fn try_from(val: C) -> Result<Self, Self::Error> {
//! Ok(Self)
//! }
//! };
//!
//! impl TryFrom<B> for A {
//! type Error = ErrB_A;
//!
//! fn try_from(val: B) -> Result<Self, Self::Error> {
//! Ok(Self)
//! }
//! };
//!
//! A::try_from(D);
//! B::try_from(D);
//! ```
use TokenStream;
use ToTokens;
use parse_macro_input;
use crateTransitiveInput;