diff --git a/objc2-proc-macros/src/derive.rs b/objc2-proc-macros/src/derive.rs new file mode 100644 index 000000000..4ee6aab4e --- /dev/null +++ b/objc2-proc-macros/src/derive.rs @@ -0,0 +1,28 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::DeriveInput; + +pub(crate) fn impl_encode(ast: &DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + unsafe impl ::objc2_encode::Encode for #name { + const ENCODING: ::objc2_encode::Encoding<'static> = ::objc2_encode::Encoding::Struct( + stringify!(#name), + &[CGFloat::ENCODING, CGFloat::ENCODING], + ); + } + }; + gen.into() +} + +pub(crate) fn impl_ref_encode(ast: &DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + unsafe impl ::objc2_encode::RefEncode for #name { + const ENCODING_REF: ::objc2_encode::Encoding<'static> = ::objc2_encode::Encoding::Pointer( + &::ENCODING + ); + } + }; + gen.into() +} diff --git a/objc2-proc-macros/src/lib.rs b/objc2-proc-macros/src/lib.rs index 604e6c7a0..9572d3d60 100644 --- a/objc2-proc-macros/src/lib.rs +++ b/objc2-proc-macros/src/lib.rs @@ -19,16 +19,18 @@ extern "C" {} use proc_macro::TokenStream; +mod derive; + /// TODO #[proc_macro_derive(Encode)] pub fn encode_derive(input: TokenStream) -> TokenStream { - dbg!(input); - todo!() + let ast = syn::parse(input).unwrap(); + derive::impl_encode(&ast) } /// TODO #[proc_macro_derive(RefEncode)] pub fn ref_encode_derive(input: TokenStream) -> TokenStream { - dbg!(input); - todo!() + let ast = syn::parse(input).unwrap(); + derive::impl_ref_encode(&ast) }