aboutsummaryrefslogtreecommitdiff
path: root/macros/src/libs/intertrait_macros/gen_caster.rs
blob: a703a629202bec6c71af4d960c01434cd2ce50eb (plain)
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
/**
 * Originally from Intertrait by CodeChain
 *
 * <https://github.com/CodeChain-io/intertrait>
 * <https://crates.io/crates/intertrait/0.2.2>
 *
 * Licensed under either of
 *
 * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
 * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>)

 * at your option.
*/
use std::str::from_utf8;

use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, ToTokens};
use uuid::adapter::Simple;
use uuid::Uuid;

const CASTER_FN_NAME_PREFIX: &[u8] = b"__";

const FN_BUF_LEN: usize = CASTER_FN_NAME_PREFIX.len() + Simple::LENGTH;

pub fn generate_caster(
    ty: &impl ToTokens,
    dst_trait: &impl ToTokens,
    sync: bool,
) -> TokenStream
{
    let fn_ident = create_caster_fn_ident();

    let new_caster = if sync {
        quote! {
            syrette::libs::intertrait::Caster::<dyn #dst_trait>::new_sync(
                |from| {
                    let concrete = from
                        .downcast::<#ty>()
                        .map_err(|_| syrette::libs::intertrait::CasterError::CastBoxFailed)?;

                    Ok(concrete as Box<dyn #dst_trait>)
                },
                |from| {
                    let concrete = from
                        .downcast::<#ty>()
                        .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?;

                    Ok(concrete as std::rc::Rc<dyn #dst_trait>)
                },
                |from| {
                    let concrete = from
                        .downcast::<#ty>()
                        .map_err(|_| syrette::libs::intertrait::CasterError::CastArcFailed)?;

                    Ok(concrete as std::sync::Arc<dyn #dst_trait>)
                },
            )
        }
    } else {
        quote! {
            syrette::libs::intertrait::Caster::<dyn #dst_trait>::new(
                |from| {
                    let concrete = from
                        .downcast::<#ty>()
                        .map_err(|_| syrette::libs::intertrait::CasterError::CastBoxFailed)?;

                    Ok(concrete as Box<dyn #dst_trait>)
                },
                |from| {
                    let concrete = from
                        .downcast::<#ty>()
                        .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?;

                    Ok(concrete as std::rc::Rc<dyn #dst_trait>)
                },
            )
        }
    };

    quote! {
        #[syrette::libs::linkme::distributed_slice(syrette::libs::intertrait::CASTERS)]
        #[linkme(crate = syrette::libs::linkme)]
        fn #fn_ident() -> (::std::any::TypeId, syrette::libs::intertrait::BoxedCaster) {
            (::std::any::TypeId::of::<#ty>(), Box::new(#new_caster))
        }
    }
}

fn create_caster_fn_ident() -> Ident
{
    let buf = &mut [0u8; FN_BUF_LEN];

    buf[..CASTER_FN_NAME_PREFIX.len()].copy_from_slice(CASTER_FN_NAME_PREFIX);

    Uuid::new_v4()
        .to_simple()
        .encode_lower(&mut buf[CASTER_FN_NAME_PREFIX.len()..]);

    let fn_name =
        from_utf8(&buf[..FN_BUF_LEN]).expect("Created caster function name is not UTF-8");

    format_ident!("{}", fn_name)
}