trait_cast_macros/lib.rs
1//! Proc-macro automating the implementation of `trait_cast::TraitcastableAny`.
2//!
3//! See `derive_traitcastable_any` for more details.
4//!
5
6mod derive_traitcastable_any;
7
8use proc_macro::TokenStream;
9
10/// Derive macro implementing `TraitcastableAny` for a struct, enum or union.
11///
12/// Use the arguments to specify all possible target Traits for which trait objects are
13/// supposed to be downcastable from a dyn `TraitcastableAny`.
14///
15/// Example:
16/// ```ignore
17/// extern crate trait_cast;
18///
19/// use trait_cast::TraitcastableAny;
20///
21///
22/// #[derive(TraitcastableAny)]
23/// #[traitcast_targets(Print)]
24/// struct Source(i32);
25///
26/// trait Print {
27/// fn print(&self);
28/// }
29/// impl Print for Source {
30/// fn print(&self) {
31/// println!("{}", self.0)
32/// }
33/// }
34///
35/// fn main() {
36/// let source = Box::new(Source(5));
37/// let castable: Box<dyn TraitcastableAny> = source;
38/// let x: &dyn Print = castable.downcast_ref().unwrap();
39/// x.print();
40/// }
41/// ```
42#[proc_macro_derive(TraitcastableAny, attributes(traitcast_targets))]
43pub fn derive_traitcastable_any(input: TokenStream) -> TokenStream {
44 derive_traitcastable_any::derive_traitcastable_any(input)
45}