Skip to content

Commit

Permalink
More elegant solution regarding mutability of inner builtin types
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jan 24, 2023
1 parent d3253c2 commit 785dd81
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
25 changes: 13 additions & 12 deletions godot-codegen/src/class_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

//! Generates a file for each Godot class
//! Generates a file for each Godot engine + builtin class
use proc_macro2::{Ident, Literal, TokenStream};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -267,10 +267,7 @@ fn make_builtin_class(
} else {
panic!("Rust type `{}` categorized wrong", class.name)
};
// let opaque_name = format_ident!("Opaque{}", class.name);

// let constructor = make_constructor(class, ctx, &name_str);
let constructor = quote! {};
let class_enums = class.enums.as_ref().map(|class_enums| {
class_enums
.iter()
Expand All @@ -289,19 +286,21 @@ fn make_builtin_class(
use crate::sys::GodotFfi as _;
use crate::engine::Object;

// #[derive(Debug)]
#[repr(transparent)]
pub struct #inner_class<'a> {
// opaque: sys::types::#opaque_name,
pub outer: &'a mut #outer_class
_outer_lifetime: std::marker::PhantomData<&'a ()>,
sys_ptr: sys::GDExtensionTypePtr,
}
impl<'a> #inner_class<'a> {
#constructor
pub fn from_outer(outer: &#outer_class) -> Self {
Self {
_outer_lifetime: std::marker::PhantomData,
sys_ptr: outer.sys(),
}
}

#methods
}
// impl sys::GodotFfi for #class_name {
// sys::ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
// }

#enums
};
Expand Down Expand Up @@ -573,11 +572,13 @@ fn make_builtin_method_definition(
}

let method_name_str = &method.name;

let receiver = if method.is_const {
quote! { &self, }
} else {
quote! { &mut self, }
};

let return_value = method.return_type.as_deref().map(MethodReturn::from_type);
let hash = method.hash;
let is_varcall = method.is_vararg;
Expand All @@ -595,7 +596,7 @@ fn make_builtin_method_definition(
let __call_fn = __call_fn.unwrap_unchecked();
};
let ptrcall_invocation = quote! {
__call_fn(self.outer.sys(), __args_ptr, return_ptr, __args.len() as i32);
__call_fn(self.sys_ptr, __args_ptr, return_ptr, __args.len() as i32);
};

make_function_definition(
Expand Down
4 changes: 2 additions & 2 deletions godot-core/src/builtin/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Array {

#[cfg(not(any(gdext_test, doctest)))]
#[doc(hidden)]
pub fn as_inner(&mut self) -> inner::InnerArray {
inner::InnerArray { outer: self }
pub fn as_inner(&self) -> inner::InnerArray {
inner::InnerArray::from_outer(self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions godot-core/src/builtin/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl Callable {

#[cfg(not(any(gdext_test, doctest)))]
#[doc(hidden)]
pub fn as_inner(&mut self) -> inner::InnerCallable {
inner::InnerCallable { outer: self }
pub fn as_inner(&self) -> inner::InnerCallable {
inner::InnerCallable::from_outer(self)
}
}

Expand Down
4 changes: 2 additions & 2 deletions godot-core/src/builtin/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ impl Vector2 {

#[cfg(not(any(gdext_test, doctest)))]
#[doc(hidden)]
pub fn as_inner(&mut self) -> inner::InnerVector2 {
inner::InnerVector2 { outer: self }
pub fn as_inner(&self) -> inner::InnerVector2 {
inner::InnerVector2::from_outer(self)
}
}

Expand Down
6 changes: 3 additions & 3 deletions itest/rust/src/builtin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn run() -> bool {

#[itest]
fn test_builtins_vector2() {
let mut vec = Vector2::new(3.0, -4.0);
let vec = Vector2::new(3.0, -4.0);
let inner: InnerVector2 = vec.as_inner();

let len_sq = inner.length_squared();
Expand All @@ -33,7 +33,7 @@ fn test_builtins_vector2() {

#[itest]
fn test_builtins_array() {
let mut array = Array::default();
let array = Array::default();
let mut inner: InnerArray = array.as_inner();

let a = 7.to_variant();
Expand All @@ -51,7 +51,7 @@ fn test_builtins_array() {
#[itest]
fn test_builtins_callable() {
let obj = Node2D::new_alloc();
let mut cb = Callable::from_object_method(obj.share(), "set_position");
let cb = Callable::from_object_method(obj.share(), "set_position");
let inner: InnerCallable = cb.as_inner();

assert_eq!(inner.is_null(), false);
Expand Down

0 comments on commit 785dd81

Please sign in to comment.