-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Returns the dereferenced type in the exported method #870
Conversation
Thanks for the PR!
Just to understand -- why do you want to do that? Since GDScript doesn't support such types, it's not immediately clear how the type would be marshalled. I mean this from a perspective that |
In this example f64, but usually what I want to pass by reference is type like Vec<_>. If the return type is Variant, it avoids cloning twice, but there is no type checking. #[derive(NativeClass)]
#[no_constructor]
struct Foo {
bar: Rc<RefCell<Vec<i32>>>,
}
#[methods]
impl Foo {
#[export]
fn get_bar(&self) -> Variant {
self.bar.borrow().to_variant()
}
} |
Is there a reason that diff --git a/gdnative-core/src/export/macros.rs b/gdnative-core/src/export/macros.rs
index 92b3bea..aab3c12 100644
--- a/gdnative-core/src/export/macros.rs
+++ b/gdnative-core/src/export/macros.rs
@@ -56,7 +56,7 @@ macro_rules! godot_wrap_method_inner {
$($pname,)*
$($opt_pname,)*
);
- gdnative::core_types::OwnedToVariant::owned_to_variant(ret)
+ gdnative::core_types::ToVariant::to_variant(&ret)
}
})
.unwrap_or_else(|err| { But I can only assume there is a reason this wasn't done in the first place. The best reason I can think of is that feels unusual to a Rust programmer because it will implicitly clone from borrows, as in the example below. #[derive(NativeClass)]
#[no_constructor]
struct Foo {
bar: Vec<f32>,
}
#[methods]
impl Foo {
#[export]
fn get_bar(&self, _base: &Node) -> &[f32] {
&self.bar
}
} |
- Enable to use Path syntax. - Remove abstractions that inhibit expansion. - Remove code that only accepts [export = "wrong"]. - Add error message.
The ways of emit warnings is a terrible hack. This is because there is no way to emit warnings from macros in stable Rust. Follow these steps to emit warnings. - Detect whether reference types are used in derive_methods(). - Expand the call to deprecated_reference_return!() macro to user code.
Feature
Add #[export(deref_return)] to allow exported methods to return dereferenced types. This can be used with types that implement Deref traits.
For example.
This feature has the following advantages.
std::cell::Ref<f64>
can be written in the return type signature.Incidental changes
Returning a reference without applying deref_return will display the following warning.
Compatibility
The godot_wrap_method! macro is not compatible because its definition has changed. (Can be fixed for compatibility if needed)