Skip to content

Commit

Permalink
Add mocked derive implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 16, 2022
1 parent e185bd1 commit 1b3c9e9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
28 changes: 28 additions & 0 deletions objc2-proc-macros/src/derive.rs
Original file line number Diff line number Diff line change
@@ -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(
&<Self as ::objc2_encode::Encode>::ENCODING
);
}
};
gen.into()
}
10 changes: 6 additions & 4 deletions objc2-proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1b3c9e9

Please sign in to comment.