diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-12-15 22:51:55 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-12-15 23:11:20 +0100 |
| commit | b6202b646a0d5ecced301d9bac8bfcaf977d7ee4 (patch) | |
| tree | 7d42cb50f9e66153e7e8b2217009684e25f54f42 /macros/src/capable.rs | |
| parent | f3980c704544a464f9729cc8bc9f97d3a7454769 (diff) | |
Reflection for castables
Diffstat (limited to 'macros/src/capable.rs')
| -rw-r--r-- | macros/src/capable.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/macros/src/capable.rs b/macros/src/capable.rs new file mode 100644 index 00000000..dcfdfc82 --- /dev/null +++ b/macros/src/capable.rs @@ -0,0 +1,48 @@ +use syn::parse::Parser; +use syn::punctuated::Punctuated; +use syn::Token; + +use super::*; + +/// Expand the `#[capability]` macro. +pub fn capability(item: syn::ItemTrait) -> Result<TokenStream> { + let ident = &item.ident; + Ok(quote! { + #item + impl ::typst::model::Capability for dyn #ident {} + }) +} + +/// Expand the `#[capable(..)]` macro. +pub fn capable(attr: TokenStream, item: syn::Item) -> Result<TokenStream> { + let (ident, generics) = match &item { + syn::Item::Struct(s) => (&s.ident, &s.generics), + syn::Item::Enum(s) => (&s.ident, &s.generics), + _ => bail!(item, "only structs and enums are supported"), + }; + + let (params, args, clause) = generics.split_for_impl(); + let checks = Punctuated::<Ident, Token![,]>::parse_terminated + .parse2(attr)? + .into_iter() + .map(|capability| { + quote! { + if id == ::std::any::TypeId::of::<dyn #capability>() { + return Some(unsafe { + ::typst::util::fat::vtable(self as &dyn #capability) + }); + } + } + }); + + Ok(quote! { + #item + + unsafe impl #params ::typst::model::Capable for #ident #args #clause { + fn vtable(&self, id: ::std::any::TypeId) -> ::std::option::Option<*const ()> { + #(#checks)* + None + } + } + }) +} |
