Skip to content

Commit 6d694bb

Browse files
bors[bot]Bromeon
andauthored
Merge #705
705: #[inherit] defaults to Reference if unspecified r=toasteater a=Bromeon Allows to omit the `#[inherit]` attribute in the `NativeClass` derive. In that case, `Reference` is chosen as a base class. This behavior is consistent with omitting the `extends` keyword in GDScript. Co-authored-by: Jan Haller <bromeon@gmail.com>
2 parents 52e7788 + 921f858 commit 6d694bb

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
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

gdnative/tests/ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ fn ui_tests() {
55
// NativeClass
66
t.pass("tests/ui/derive_pass.rs");
77
t.pass("tests/ui/derive_property_basic.rs");
8+
t.pass("tests/ui/derive_no_inherit.rs");
89
t.compile_fail("tests/ui/derive_fail_property_hint.rs");
9-
t.compile_fail("tests/ui/derive_fail_inherit.rs");
1010
t.compile_fail("tests/ui/derive_fail_inherit_param.rs");
1111
t.compile_fail("tests/ui/derive_fail_methods.rs");
1212
t.compile_fail("tests/ui/derive_fail_methods_param.rs");

gdnative/tests/ui/derive_fail_inherit.stderr

Lines changed: 0 additions & 7 deletions
This file was deleted.

gdnative/tests/ui/derive_fail_inherit.rs renamed to gdnative/tests/ui/derive_no_inherit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct Foo {}
55

66
#[methods]
77
impl Foo {
8-
fn new(_owner: &Node) -> Self {
8+
fn new(_owner: &Reference) -> Self {
99
Foo {}
1010
}
1111
}

0 commit comments

Comments
 (0)