Skip to content

Commit fbc0c91

Browse files
committed
Added a stub objc-foundation-derive crate.
1 parent cc92447 commit fbc0c91

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

derive/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "objc-foundation-derive"
3+
version = "0.0.0"
4+
authors = ["Steven Sheldon"]
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
syn = "0.10.5"
11+
quote = "0.3.10"

derive/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(proc_macro, proc_macro_lib)]
2+
3+
extern crate proc_macro;
4+
extern crate syn;
5+
#[macro_use]
6+
extern crate quote;
7+
8+
use proc_macro::TokenStream;
9+
10+
#[proc_macro_derive(INSObject)]
11+
pub fn impl_object(input: TokenStream) -> TokenStream {
12+
// Construct a string representation of the type definition
13+
let s = input.to_string();
14+
15+
// Parse the string representation
16+
let ast = syn::parse_macro_input(&s).unwrap();
17+
18+
// Build the impl
19+
let name = &ast.ident;
20+
let gen = quote! {
21+
unsafe impl ::objc::Message for #name { }
22+
23+
impl ::objc_foundation::INSObject for #name {
24+
fn class() -> &'static ::objc::runtime::Class {
25+
let name = stringify!(#name);
26+
match ::objc::runtime::Class::get(name) {
27+
Some(cls) => cls,
28+
None => panic!("Class {} not found", name),
29+
}
30+
}
31+
}
32+
};
33+
34+
// Return the generated impl
35+
gen.parse().unwrap()
36+
}

0 commit comments

Comments
 (0)