Skip to content

Commit 5f0a452

Browse files
authored
feat: Add TypedThrough and IntoScript derive macros (#294)
1 parent a5ea437 commit 5f0a452

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use proc_macro2::TokenStream;
2+
use quote::quote;
3+
use syn::DeriveInput;
4+
5+
pub fn into_script(input: TokenStream) -> TokenStream {
6+
let (ident, generics) = match syn::parse2(input) {
7+
Ok(DeriveInput {
8+
ident, generics, ..
9+
}) => (ident, generics),
10+
Err(err) => return err.to_compile_error(),
11+
};
12+
13+
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
14+
15+
quote! {
16+
impl #impl_generics ::bevy_mod_scripting::bindings::function::into::IntoScript for #ident #type_generics #where_clause {
17+
fn into_script(self, world: ::bevy_mod_scripting::core::bindings::WorldGuard) -> Result<::bevy_mod_scripting::core::bindings::script_value::ScriptValue, ::bevy_mod_scripting::core::error::InteropError> {
18+
::bevy_mod_scripting::core::bindings::function::into::IntoScript::into_script(
19+
::bevy_mod_scripting::core::bindings::function::from::Val(self),
20+
world,
21+
)
22+
}
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod into_script;
2+
mod typed_through;
3+
4+
pub use self::{into_script::into_script, typed_through::typed_through};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use proc_macro2::TokenStream;
2+
use quote::quote;
3+
use syn::DeriveInput;
4+
5+
pub fn typed_through(input: TokenStream) -> TokenStream {
6+
let (ident, generics) = match syn::parse2(input) {
7+
Ok(DeriveInput {
8+
ident, generics, ..
9+
}) => (ident, generics),
10+
Err(err) => return err.to_compile_error(),
11+
};
12+
13+
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
14+
15+
let turbofish = type_generics.as_turbofish();
16+
quote! {
17+
impl #impl_generics ::bevy_mod_scripting::core::docgen::typed_through::TypedThrough for #ident #type_generics #where_clause {
18+
fn through_type_info() -> ::bevy_mod_scripting::core::docgen::typed_through::ThroughTypeInfo {
19+
::bevy_mod_scripting::core::docgen::typed_through::ThroughTypeInfo::TypeInfo(#ident #turbofish ::type_info())
20+
}
21+
}
22+
}
23+
}

crates/bevy_mod_scripting_derive/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ use proc_macro2::{Span, TokenStream};
44
use quote::{format_ident, quote_spanned, ToTokens};
55
use syn::{spanned::Spanned, ImplItemFn, ItemImpl};
66

7+
mod derive;
8+
9+
#[proc_macro_derive(TypedThrough)]
10+
/// Default implementation for the `TypedThrough` trait
11+
pub fn typed_through(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
12+
derive::typed_through(input.into()).into()
13+
}
14+
15+
#[proc_macro_derive(IntoScript)]
16+
/// Default implementation for the `IntoScript` trait
17+
pub fn into_script(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
18+
derive::into_script(input.into()).into()
19+
}
20+
721
/// Derive macro for generating script bindings from an impl block.
822
///
923
/// Does not support generics.

0 commit comments

Comments
 (0)