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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#![forbid(
    rust_2018_idioms,
    future_incompatible,
    elided_lifetimes_in_paths,
    unsafe_code
)]
#![warn(
    missing_debug_implementations,
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unreachable_pub,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications
)]

//! An attribute macro that generates methods for retrieving supertraits from
//! trait-objects (upcasting).
//!
//! If you have a trait with a supertrait, you sometimes want to upcast a trait object.
//! Rust currently does not support this.
//!
//! ```compile_fail
//! trait Super {}
//!
//! trait Sub: Super {}
//!
//! fn wants_upcast(sub: Box<dyn Sub>) {
//!     let s: Box<dyn Super> = sub;
//!     // do something with super.
//! }
//! ```
//!
//! This results in the following error:
//!
//! ```text
//! error[E0308]: mismatched types
//!  --> src/lib.rs:27:29
//!   |
//! 8 |     let s: Box<dyn Super> = sub;
//!   |            --------------   ^^^ expected trait `Super`, found trait `Sub`
//!   |            |
//!   |            expected due to this
//!   |
//!   = note: expected struct `std::boxed::Box<dyn Super>`
//!              found struct `std::boxed::Box<(dyn Sub + 'static)>`
//!
//! error: aborting due to previous error
//! ```
//!
//! The `as_dyn_trait` attribute solves this problem:
//!
//! ```no_run
//! # use as_dyn_trait::as_dyn_trait;
//! #[as_dyn_trait]
//! trait Super {}
//!
//! trait Sub: Super {}
//!
//! fn wants_upcast(sub: Box<dyn Sub>) {
//!     // s has type Box<dyn Super>.
//!     let s = sub.as_dyn_super();
//!     // do something with super.
//! }
//! ```
//!
//! To achieve this, the macro generates several traits. For a trait `MyTrait`, the names of these
//! traits and their methods are:
//! * `AsDynMyTraitRef`:
//!     * `fn as_dyn_my_trait(&self) -> &dyn MyTrait;`
//!     * `fn as_dyn_my_trait_mut(&mut self) -> &mut dyn MyTrait;`
//! * `AsDynMyTraitBox`:
//!     * `fn as_dyn_my_trait(self: Box<Self>) -> Box<dyn MyTrait>;`
//! * `AsDynMyTraitRc`:
//!     * `fn as_dyn_my_trait(self: Rc<Self>) -> Rc<dyn MyTrait>;`
//! * `AsDynMyTraitArc`:
//!     * `fn as_dyn_my_trait(self: Arc<Self>) -> Arc<dyn MyTrait>;`
//! * `AsDynMyTraitPinRef`:
//!     * `fn as_dyn_my_trait(self: Pin<&Self>) -> Pin<&dyn MyTrait>;`
//!     * `fn as_dyn_my_trait_mut(self: Pin<&mut Self>) -> Pin<&mut dyn MyTrait>;`
//! * `AsDynMyTraitPinBox`:
//!     * `fn as_dyn_my_trait(self: Pin<Box<Self>>) -> Pin<Box<dyn MyTrait>>;`
//! * `AsDynMyTraitPinRc`:
//!     * `fn as_dyn_my_trait(self: Pin<Rc<Self>>) -> Pin<Rc<dyn MyTrait>>;`
//! * `AsDynMyTraitPinArc`:
//!     * `fn as_dyn_my_trait(self: Pin<Arc<Self>>) -> Pin<Arc<dyn MyTrait>>;`
//!
//! These traits are automatically implemented for all `Sized` types that implement `MyTrait`. If you
//! want to implement `MyTrait` for dynamically sized types, you need to do add these implementations manually.
//! Since you cannot turn a DST into a trait object, such an implementation must always panic.
//!
//! In order for those traits to work on trait objects, all of them are automatically supertraits of `MyTrait`.
//!
//! The attribute supports several options. The options are passed to the attribute as a comma-separated list:
//!
//! ```no_run
//! # use as_dyn_trait::as_dyn_trait;
//! #[as_dyn_trait(enable_pin = true, trait_name_prefix = DifferentName)]
//! trait Super {}
//! ```
//!
//! * `trait_name_prefix`: The prefix to use for the generated traits. Default is `AsDyn` followed by
//!   the trait name.
//! * `ref_trait_name`: The name of the trait for references. Default is the trait name prefix with `Ref` appended.
//! * `box_trait_name`: The name of the trait for `Box<_>`. Default is the trait name prefix with `Box` appended.
//! * `rc_trait_name`: The name of the trait for `Rc<_>`. Default is the trait name prefix with `Rc` appended.
//! * `arc_trait_name`: The name of the trait for `Arc<_>`. Default is the trait name prefix with `Arc` appended.
//! * `pin_ref_trait_name`: The name of the trait for Pin<_> references. Default is the trait name prefix with `PinRef` appended.
//! * `pin_box_trait_name`: The name of the trait for `Pin<Box<_>>`. Default is the trait name prefix with `PinBox`
//!    appended.
//! * `pin_rc_trait_name`: The name of the trait for `Pin<Rc<_>>`. Default is the trait name prefix with `PinRc`
//!    appended.
//! * `pin_arc_trait_name`: The name of the trait for `Pin<Arc<_>>`. Default is the trait name prefix with `PinArc`
//!    appended.
//! * `method_name`: The name of the conversion method. Default is the `as_dyn_` followed by the trait name
//!    (converted to lower-snake-case).
//! * `mut_method_name`: The name of the conversion method for mutable references. Default is the `as_dyn_`
//!    followed by the trait name (converted to lower-snake-case) and `_mut`.
//! * `enable_ref`: Enables conversion of references. Default is true.
//! * `enable_box`: Enables conversion of `Box<_>`. Default is true.
//! * `enable_rc`: Enables conversion of `Rc<_>`. Default is true.
//! * `enable_arc`: Enables conversion of `Arc<_>`. Default is true.
//! * `enable_pin`: Enables conversion of the `Pin<_>` types. These are only generated conversion of the
//!    corresponding base pointer is also enabled. Default is false.
//!
//! This attribute does not use or generate any unsafe code.

extern crate proc_macro;

mod generics;
mod settings;

use self::{
    generics::GenericsExt,
    settings::{MacroOptions, Settings},
};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use std::collections::HashSet;
use syn::{
    parse_macro_input, parse_quote, punctuated::Punctuated, FnArg, GenericParam, Generics, Ident,
    ItemTrait, Lifetime, LifetimeDef, Path, Token, TraitBound, TraitBoundModifier, Type,
    TypeParamBound, Visibility, WhereClause,
};

fn make_trait_bound(path: Path) -> TypeParamBound {
    TypeParamBound::Trait(TraitBound {
        paren_token: None,
        modifier: TraitBoundModifier::None,
        lifetimes: None,
        path,
    })
}

fn get_trait_doc(from: &str, to: &str) -> String {
    format!(
        "Trait for converting {} to {}. This is mainly useful for upcasting trait objects.",
        from, to,
    )
}

fn get_method_doc(from: &str, to: &str) -> String {
    format!(
        "Converts {} to {}. Internally, this only uses a type coercion.",
        from, to,
    )
}

struct MethodInfo<'a> {
    name: &'a Ident,
    doc_from: &'a str,
    doc_to: &'a str,
    lifetime: Option<&'a Lifetime>,
    self_param: FnArg,
    return_type: Type,
    where_clause: Option<WhereClause>,
}

impl<'a> MethodInfo<'a> {
    fn build(self) -> (TokenStream, TokenStream) {
        let MethodInfo {
            name,
            doc_from,
            doc_to,
            lifetime,
            self_param,
            return_type,
            where_clause,
        } = self;

        let doc = get_method_doc(doc_from, doc_to);
        let lifetime_bound: Option<Generics> = lifetime.map(|lifetime| {
            let lifetime = GenericParam::Lifetime(LifetimeDef {
                attrs: Vec::new(),
                lifetime: lifetime.clone(),
                colon_token: None,
                bounds: Default::default(),
            });
            parse_quote!(<#lifetime>)
        });

        (
            quote! {
                #[doc = #doc]
                fn #name #lifetime_bound (#self_param) -> #return_type
                    #where_clause;
            },
            quote! {
                fn #name #lifetime_bound (#self_param) -> #return_type
                    #where_clause
                {
                    self
                }
            },
        )
    }
}

fn split_option<A, B>(option: Option<(A, B)>) -> (Option<A>, Option<B>) {
    match option {
        Some((a, b)) => (Some(a), Some(b)),
        None => (None, None),
    }
}

fn make_trait(
    generics: &Generics,
    generics_extended: &Generics,
    generics_without_bounds: &Generics,
    new_trait_name: &Ident,
    type_param: &Ident,
    vis: &Visibility,
    supertraits: &mut Punctuated<TypeParamBound, Token![+]>,
    trait_doc_from: &str,
    trait_doc_to: &str,
    method_info: MethodInfo<'_>,
    mut_method_info: Option<MethodInfo<'_>>,
) -> TokenStream {
    supertraits.push(make_trait_bound(
        parse_quote!(#new_trait_name #generics_without_bounds),
    ));

    let trait_doc = get_trait_doc(trait_doc_from, trait_doc_to);

    let (method, method_impl) = method_info.build();
    let (mut_method, mut_method_impl) = split_option(mut_method_info.map(|m| m.build()));
    let where_clause = &generics.where_clause;

    quote! {
        #[doc = #trait_doc]
        #vis trait #new_trait_name #generics
            #where_clause
        {
            #method
            #mut_method
        }

        impl #generics_extended #new_trait_name #generics_without_bounds for #type_param
            #where_clause
        {
            #method_impl
            #mut_method_impl
        }
    }
}

struct MakeTraitOptions<'a> {
    trait_name: &'a Ident,
    generics: Generics,
    generics_extended: Generics,
    generics_without_bounds: Generics,
    type_param: Ident,
    vis: &'a Visibility,
    supertraits: &'a mut Punctuated<TypeParamBound, Token![+]>,
    method_name: &'a Ident,
    mut_method_name: &'a Ident,
    method_lifetime: Lifetime,
}

fn make_ref_trait(
    options: &mut MakeTraitOptions<'_>,
    new_trait_name: &Ident,
    pinned: bool,
) -> TokenStream {
    let &mut MakeTraitOptions {
        trait_name,
        ref generics,
        ref generics_extended,
        ref generics_without_bounds,
        ref type_param,
        vis,
        ref mut supertraits,
        method_name,
        mut_method_name,
        method_lifetime: _,
    } = options;

    let (
        doc_from,
        doc_to,
        self_param,
        return_type,
        doc_from_mut,
        doc_to_mut,
        self_param_mut,
        return_type_mut,
    ) = if pinned {
        (
            "`Pin<&Self>`",
            format!("`Pin<&dyn {0}>`", trait_name),
            parse_quote!(self: std::pin::Pin<&Self>),
            parse_quote!(std::pin::Pin<&dyn #trait_name #generics_without_bounds>),
            "`Pin<&mut Self>`",
            format!("`Pin<&mut dyn {0}>`", trait_name),
            parse_quote!(self: std::pin::Pin<&mut Self>),
            parse_quote!(std::pin::Pin<&mut dyn #trait_name #generics_without_bounds>),
        )
    } else {
        (
            "`&Self`",
            format!("`&dyn {0}`", trait_name),
            parse_quote!(&self),
            parse_quote!(&dyn #trait_name #generics_without_bounds),
            "`&mut Self`",
            format!("`&mut dyn {0}`", trait_name),
            parse_quote!(&mut self),
            parse_quote!(&mut dyn #trait_name #generics_without_bounds),
        )
    };
    let trait_doc_from = format!("{} and {}", doc_from, doc_from_mut);
    let trait_doc_to = format!("{} and {}", doc_to, doc_to_mut);

    make_trait(
        generics,
        generics_extended,
        generics_without_bounds,
        new_trait_name,
        type_param,
        &vis,
        supertraits,
        &trait_doc_from,
        &trait_doc_to,
        MethodInfo {
            name: method_name,
            doc_from,
            doc_to: &doc_to,
            lifetime: None,
            self_param,
            return_type,
            where_clause: None,
        },
        Some(MethodInfo {
            name: mut_method_name,
            doc_from: doc_from_mut,
            doc_to: &doc_to_mut,
            lifetime: None,
            self_param: self_param_mut,
            return_type: return_type_mut,
            where_clause: None,
        }),
    )
}

fn make_smart_ptr_trait(
    options: &mut MakeTraitOptions<'_>,
    new_trait_name: &Ident,
    smart_ptr: Path,
    pinned: bool,
) -> TokenStream {
    let &mut MakeTraitOptions {
        trait_name,
        ref generics,
        ref generics_extended,
        ref generics_without_bounds,
        ref type_param,
        vis,
        ref mut supertraits,
        method_name,
        mut_method_name: _,
        ref method_lifetime,
    } = options;

    let smart_ptr_name = &smart_ptr.segments.last().as_ref().unwrap().ident;
    let (doc_from, doc_to, self_param, return_type) = if pinned {
        (
            format!("`Pin<{}<Self>>`", smart_ptr_name),
            format!("`Pin<{}<dyn {}>>`", smart_ptr_name, trait_name),
            parse_quote!(self: std::pin::Pin<#smart_ptr<Self>>),
            parse_quote!(std::pin::Pin<#smart_ptr<dyn #trait_name #generics_without_bounds + #method_lifetime>>),
        )
    } else {
        (
            format!("`{}<Self>`", smart_ptr_name),
            format!("`{}<dyn {}>`", smart_ptr_name, trait_name),
            parse_quote!(self: #smart_ptr<Self>),
            parse_quote!(#smart_ptr<dyn #trait_name #generics_without_bounds + #method_lifetime>),
        )
    };

    make_trait(
        generics,
        generics_extended,
        generics_without_bounds,
        new_trait_name,
        type_param,
        vis,
        supertraits,
        &doc_from,
        &doc_to,
        MethodInfo {
            name: method_name,
            doc_from: &doc_from,
            doc_to: &doc_to,
            lifetime: Some(method_lifetime),
            self_param,
            return_type,
            where_clause: parse_quote!(where Self: #method_lifetime),
        },
        None,
    )
}

fn extend_generics(
    generics: &Generics,
    generics_without_bounds: &Generics,
    trait_name: &Ident,
    type_param: &Ident,
) -> Generics {
    let lt_token = Some(generics.lt_token.clone().unwrap_or_default());
    let mut params = generics.params.clone();
    let gt_token = Some(generics.gt_token.clone().unwrap_or_default());
    // We only use the where clause from the original generics object, no need to clone it here.
    let where_clause = None;

    params.push(GenericParam::Type(
        parse_quote!(#type_param: #trait_name #generics_without_bounds + std::marker::Sized),
    ));

    Generics {
        lt_token,
        params,
        gt_token,
        where_clause,
    }
}

fn find_unused_type_param(generics: &Generics) -> Ident {
    let params: HashSet<_> = generics
        .type_params()
        .map(|p| p.ident.to_string())
        .collect();

    for candidate in (b'T'..=b'Z').chain(b'A'..b'T') {
        let candidate_slice = &[candidate];
        let candidate = std::str::from_utf8(candidate_slice).unwrap();

        if !params.contains(candidate) {
            return Ident::new(candidate, Span::call_site());
        }
    }

    panic!("Unable to find an unused type parameter. Please report a bug.");
}

fn find_unused_lifetime(generics: &Generics) -> Lifetime {
    let lifetimes: HashSet<_> = generics
        .lifetimes()
        .map(|l| l.lifetime.ident.to_string())
        .collect();

    for candidate in b'a'..=b'z' {
        let candidate_slice = &[candidate];
        let candidate = std::str::from_utf8(candidate_slice).unwrap();

        if !lifetimes.contains(candidate) {
            return Lifetime {
                apostrophe: Span::call_site(),
                ident: Ident::new(candidate, Span::call_site()),
            };
        }
    }

    panic!("Unable to find an unused lifetime. Please report a bug.");
}

/// Generates methods for retrieving trait objects of supertraits from
/// trait objects of subtraits.
///
/// See the [module-level documentation](index.html) for more details.
#[proc_macro_attribute]
pub fn as_dyn_trait(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let trait_item = parse_macro_input!(item as ItemTrait);
    let settings = parse_macro_input!(args as MacroOptions);
    as_dyn_trait_impl(trait_item, settings).into()
}

fn as_dyn_trait_impl(mut trait_item: ItemTrait, settings: MacroOptions) -> TokenStream {
    let trait_name = &trait_item.ident;
    let generics = trait_item.generics.clone().remove_defaults();
    let generics_without_bounds = generics.clone().remove_bounds();

    let type_param = find_unused_type_param(&generics);
    let method_lifetime = find_unused_lifetime(&generics);

    let generics_extended =
        extend_generics(&generics, &generics_without_bounds, trait_name, &type_param);

    let settings = match Settings::read(trait_name, settings) {
        Ok(settings) => settings,
        Err(err) => return err.to_compile_error(),
    };

    let mut options = MakeTraitOptions {
        trait_name,
        generics,
        generics_extended,
        generics_without_bounds,
        type_param,
        vis: &trait_item.vis,
        supertraits: &mut trait_item.supertraits,
        method_name: settings.method_name(),
        mut_method_name: settings.mut_method_name(),
        method_lifetime,
    };

    let ref_trait = if settings.enable_ref() {
        Some(make_ref_trait(
            &mut options,
            &settings.ref_trait_name(),
            false,
        ))
    } else {
        None
    };

    let box_trait = if settings.enable_box() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.box_trait_name(),
            parse_quote!(std::boxed::Box),
            false,
        ))
    } else {
        None
    };

    let rc_trait = if settings.enable_rc() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.rc_trait_name(),
            parse_quote!(std::rc::Rc),
            false,
        ))
    } else {
        None
    };

    let arc_trait = if settings.enable_arc() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.arc_trait_name(),
            parse_quote!(std::sync::Arc),
            false,
        ))
    } else {
        None
    };

    let pin_ref_trait = if settings.enable_pin() && settings.enable_ref() {
        Some(make_ref_trait(
            &mut options,
            &settings.pin_ref_trait_name(),
            true,
        ))
    } else {
        None
    };

    let pin_box_trait = if settings.enable_pin() && settings.enable_box() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.pin_box_trait_name(),
            parse_quote!(std::boxed::Box),
            true,
        ))
    } else {
        None
    };

    let pin_rc_trait = if settings.enable_pin() && settings.enable_rc() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.pin_rc_trait_name(),
            parse_quote!(std::rc::Rc),
            true,
        ))
    } else {
        None
    };

    let pin_arc_trait = if settings.enable_pin() && settings.enable_arc() {
        Some(make_smart_ptr_trait(
            &mut options,
            &settings.pin_arc_trait_name(),
            parse_quote!(std::sync::Arc),
            true,
        ))
    } else {
        None
    };

    quote! {
        #trait_item
        #ref_trait
        #box_trait
        #rc_trait
        #arc_trait
        #pin_ref_trait
        #pin_box_trait
        #pin_rc_trait
        #pin_arc_trait
    }
}