Skip to content

Commit f3737e8

Browse files
committed
Allow to omit #[inherit] attribute in NativeClass derive (defaults to Reference)
1 parent 52e7788 commit f3737e8

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

gdnative-derive/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
8383

8484
/// Makes it possible to use a type as a NativeScript.
8585
///
86-
/// ## Required attributes
86+
/// ## Type attributes
8787
///
88-
/// The following attributes are required on the type deriving `NativeClass`:
88+
/// The behavior of the derive macro can be customized using attributes on the type
89+
/// deriving `NativeClass`. All type attributes are optional.
8990
///
9091
/// ### `#[inherit(gdnative::api::BaseClass)]`
9192
///
@@ -97,9 +98,10 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
9798
/// Inheritance from other scripts, either in Rust or other languages, is
9899
/// not supported.
99100
///
100-
/// ## Optional type attributes
101+
/// If no `#[inherit(...)]` is provided, [`gdnative::api::Reference`](../gdnative/api/struct.Reference.html)
102+
/// is used as a base class. This behavior is consistent with GDScript: omitting the
103+
/// `extends` keyword will inherit `Reference`.
101104
///
102-
/// Behavior of the derive macro can be customized using attribute on the type:
103105
///
104106
/// ### `#[user_data(gdnative::user_data::SomeWrapper<Self>)]`
105107
///
@@ -134,7 +136,10 @@ pub fn profiled(meta: TokenStream, input: TokenStream) -> TokenStream {
134136
///
135137
/// See documentation on `Instance::emplace` for an example on how this can be used.
136138
///
137-
/// ## Optional field attributes
139+
///
140+
/// ## Field attributes
141+
///
142+
/// All field attributes are optional.
138143
///
139144
/// ### `#[property]`
140145
///

gdnative-derive/src/native_script.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ fn parse_derive_input(input: &DeriveInput) -> Result<DeriveData, syn::Error> {
144144

145145
let ident = input.ident.clone();
146146

147-
let inherit_attr = input
148-
.attrs
149-
.iter()
150-
.find(|a| a.path.is_ident("inherit"))
151-
.ok_or_else(|| syn::Error::new(span, "No \"inherit\" attribute found"))?;
147+
let inherit_attr = input.attrs.iter().find(|a| a.path.is_ident("inherit"));
152148

153149
// read base class
154-
let base = inherit_attr.parse_args::<Type>()?;
150+
let base = if let Some(attr) = inherit_attr {
151+
attr.parse_args::<Type>()?
152+
} else {
153+
syn::parse2::<Type>(quote! { ::gdnative::api::Reference }).unwrap()
154+
};
155155

156156
let register_callback = input
157157
.attrs

0 commit comments

Comments
 (0)