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
234
235
236
237
238
//! Procedural macros of `whichever`.
//!
//! `#[dispatchable]` on a trait re-emits the trait and exports its carrier
//! macro, a hidden `macro_rules!` that carries the trait's tokens; the macro
//! is re-exported under the trait's own name, so importing the trait imports
//! the macro. `#[dispatch(Trait)]` on an enum invokes that macro, which
//! forwards the trait tokens, the enum tokens and the options into the
//! implementation macro, `implement!`, which writes `impl Trait for Enum`
//! with one `match` per method. No state is shared between expansions, so
//! the trait and the enum may live in different crates.
use TokenStream;
use quote;
use ;
use crate;
use crate;
use crateexpand_dispatchable;
use crateexpand_implement;
use crateDispatchableOptions;
use crate;
use crateSubject;
/// Folds `errors` into the first, so that all of them are reported at once.
pub
/// Makes a trait available to `#[dispatch]`, from this crate or any other.
///
/// The trait is emitted unchanged, except that `#[dispatch(..)]` attributes
/// on its methods are consumed. The only method option is `mismatch`, which
/// gives the result of a method taking `Self` arguments when the arguments
/// hold a different variant than `self`:
///
/// ```rust,ignore
/// #[whichever::dispatchable]
/// pub trait Shape {
/// fn area(&self) -> u32;
/// #[dispatch(mismatch = false)]
/// fn congruent(&self, other: &Self) -> bool;
/// }
/// ```
///
/// Options:
/// - `remote = path` declares a trait of another crate: the item is a
/// restatement of that trait's signatures, nothing is emitted for it but
/// the carrier macro, and `#[dispatch]` of the declaration implements the
/// trait at `path`. Only the methods restated are forwarded.
/// - `name = ident` names the carrier macro after `ident` instead of the
/// trait's text and position, for a trait that one `macro_rules!` emits
/// several times with identical tokens.
///
/// ```rust,ignore
/// #[whichever::dispatchable(remote = std::io::Write)]
/// trait Write {
/// fn write(&mut self, buf: &[u8]) -> std::io::Result<usize>;
/// fn flush(&mut self) -> std::io::Result<()>;
/// fn write_all(&mut self, buf: &[u8]) -> std::io::Result<()>;
/// }
/// ```
///
/// The trait's signature types are later written into the dispatching
/// enum's module, so they must be nameable there; `crate::` paths are
/// rewritten to this crate, `super::` and `self::` paths are not and must
/// not be used.
/// Implements a `#[dispatchable]` trait for an enum by forwarding every
/// method to whichever variant the value holds, or forwards the methods of
/// an inherent `impl` block of such an enum.
///
/// On an enum, each attribute names one trait, with its generic arguments
/// when it has any, followed by options; several attributes dispatch several
/// traits, and a bare `#[dispatch]` only registers the enum. Every variant
/// must hold exactly one unnamed field whose type implements the trait. The
/// enum also gets a `From<Payload>` impl for every payload type that appears
/// in exactly one variant.
///
/// ```rust,ignore
/// #[whichever::dispatch(Shape)]
/// #[whichever::dispatch(Encoder<Json>, Error = EncodeError)]
/// pub enum AnyShape {
/// Circle(Circle),
/// #[dispatch(deref)]
/// Square(Box<Square>),
/// }
/// ```
///
/// Options, after the trait path:
/// - `Name = Type`: the associated type `Name` of the enum's impl, instead
/// of the first variant's. Methods returning `Self::Name` are converted
/// with `Into`, methods returning `Result<_, Self::Name>` with
/// `map_err(Into::into)`.
/// - `mismatch(method = expr, ..)`: the result of `method` when its `Self`
/// arguments hold a different variant than `self`.
/// - `from = false`: no `From<Payload>` impls.
/// - `try_from = true`: a `TryFrom<Enum> for Payload` impl per payload type,
/// returning the enum unchanged when it holds another variant.
///
/// Three options go on a variant. `#[dispatch(deref)]` calls the trait on what
/// the payload dereferences to, for `Box<T>`, `Rc<T>`, `Arc<T>`, `&T` and
/// `&mut T` payloads; such a variant cannot take part in a method that
/// returns `Self`. `#[dispatch(from = false)]` leaves the variant out of the
/// conversions, and `#[dispatch(try_from = true)]` adds the `TryFrom` impl
/// for that payload alone.
///
/// On a struct, the trait is forwarded to its one field, or to the field
/// marked `#[dispatch]`, with `#[dispatch(deref)]` for a field behind a
/// pointer. A struct gets no conversions, needs no `mismatch`, and forwards
/// a return-position `impl Trait` as it is, since its `match` has one arm.
///
/// On an inherent `impl` block of a dispatch enum, in the same crate, every
/// method carrying `#[dispatch(..)]` is forwarded to the named variants'
/// own method of the same name, and the body is what the other variants
/// return. `*` names every variant; the method then has no fallback and is
/// written without a body:
///
/// ```rust,ignore
/// #[whichever::dispatch]
/// impl AnyShape {
/// /// Only circles have a radius.
/// #[dispatch(Circle, with = Some)]
/// pub fn radius(&self) -> Option<f64> {
/// None
/// }
///
/// #[dispatch(*)]
/// pub fn perimeter(&self) -> f64;
/// }
/// ```
///
/// Method options: `with = path` wraps each forwarded result in a call to
/// `path`, and `call = name` forwards to a payload method of another name.
/// Methods without the attribute are left as they are.
///
/// On a trait impl block of a dispatch enum, in the same crate, the block
/// holds the items the enum implements itself, a generic associated type
/// among them, and the rest of the trait is forwarded around them. The enum
/// then does not name that trait in its own attribute; the trait's options
/// go on the block's attribute:
///
/// ```rust,ignore
/// #[whichever::dispatch]
/// pub enum Handler {
/// Text(TextHandler),
/// Map(MapHandler),
/// }
///
/// #[whichever::dispatch]
/// impl HandlerTrait for Handler {
/// fn from_handler(handler: Handler) -> Option<Self> {
/// Some(handler)
/// }
/// }
/// ```
/// Writes the trait impl; called by the macro that `#[dispatchable]` exports.
/// Rewrites an impl block of a dispatch enum or struct; called by the macro
/// that `#[dispatch]` defines for the item.