Skip to content

Commit e8c85bf

Browse files
committed
Native structure + other pointers now implement Engine{To,From}Godot, not {To,FromGodot}
1 parent 00344cf commit e8c85bf

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

godot-codegen/src/generator/native_structures.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,15 @@ fn make_native_structure(
8888

8989
let imports = util::make_imports();
9090
let (fields, methods) = make_native_structure_fields_and_methods(structure, ctx);
91-
let doc = format!("[`ToGodot`] and [`FromGodot`] are implemented for `*mut {class_name}` and `*const {class_name}`.");
9291

9392
// mod re_export needed, because class should not appear inside the file module, and we can't re-export private struct as pub
9493
let tokens = quote! {
9594
#imports
9695
use std::ffi::c_void; // for opaque object pointer fields
97-
use crate::meta::{GodotConvert, FromGodot, ToGodot};
96+
use crate::meta::{GodotConvert, EngineFromGodot, EngineToGodot};
9897

9998
/// Native structure; can be passed via pointer in APIs that are not exposed to GDScript.
10099
///
101-
#[doc = #doc]
102100
#[derive(Clone, PartialEq, Debug)]
103101
#[repr(C)]
104102
pub struct #class_name {
@@ -113,36 +111,53 @@ fn make_native_structure(
113111
type Via = i64;
114112
}
115113

116-
impl ToGodot for *mut #class_name {
114+
// Native structure pointers implement internal-only conversion traits for use in engine APIs.
115+
impl EngineToGodot for *mut #class_name {
117116
type Pass = crate::meta::ByValue;
118117

119-
fn to_godot(&self) -> Self::Via {
118+
fn engine_to_godot(&self) -> crate::meta::ToArg<'_, Self::Via, Self::Pass> {
120119
*self as i64
121120
}
121+
122+
fn engine_to_variant(&self) -> crate::builtin::Variant {
123+
crate::builtin::Variant::from(*self as i64)
124+
}
122125
}
123126

124-
impl FromGodot for *mut #class_name {
125-
fn try_from_godot(via: Self::Via) -> Result<Self, crate::meta::error::ConvertError> {
127+
impl EngineFromGodot for *mut #class_name {
128+
fn engine_try_from_godot(via: Self::Via) -> Result<Self, crate::meta::error::ConvertError> {
126129
Ok(via as Self)
127130
}
131+
132+
fn engine_try_from_variant(variant: &crate::builtin::Variant) -> Result<Self, crate::meta::error::ConvertError> {
133+
variant.try_to::<i64>().map(|i| i as Self)
134+
}
128135
}
129136

130137
impl GodotConvert for *const #class_name {
131138
type Via = i64;
132139
}
133140

134-
impl ToGodot for *const #class_name {
141+
impl EngineToGodot for *const #class_name {
135142
type Pass = crate::meta::ByValue;
136143

137-
fn to_godot(&self) -> Self::Via {
144+
fn engine_to_godot(&self) -> crate::meta::ToArg<'_, Self::Via, Self::Pass> {
138145
*self as i64
139146
}
147+
148+
fn engine_to_variant(&self) -> crate::builtin::Variant {
149+
crate::builtin::Variant::from(*self as i64)
150+
}
140151
}
141152

142-
impl FromGodot for *const #class_name {
143-
fn try_from_godot(via: Self::Via) -> Result<Self, crate::meta::error::ConvertError> {
153+
impl EngineFromGodot for *const #class_name {
154+
fn engine_try_from_godot(via: Self::Via) -> Result<Self, crate::meta::error::ConvertError> {
144155
Ok(via as Self)
145156
}
157+
158+
fn engine_try_from_variant(variant: &crate::builtin::Variant) -> Result<Self, crate::meta::error::ConvertError> {
159+
variant.try_to::<i64>().map(|i| i as Self)
160+
}
146161
}
147162
};
148163
// note: TypePtr -> ObjectPtr conversion OK?

godot-core/src/meta/godot_convert/impls.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,27 @@ macro_rules! impl_pointer_convert {
460460
type Via = i64;
461461
}
462462

463-
impl ToGodot for $Ptr {
463+
// Pointers implement internal-only conversion traits for use in engine APIs and virtual methods.
464+
impl meta::EngineToGodot for $Ptr {
464465
type Pass = meta::ByValue;
465466

466-
fn to_godot(&self) -> Self::Via {
467+
fn engine_to_godot(&self) -> meta::ToArg<'_, Self::Via, Self::Pass> {
467468
*self as i64
468469
}
470+
471+
fn engine_to_variant(&self) -> Variant {
472+
Variant::from(*self as i64)
473+
}
469474
}
470475

471-
impl FromGodot for $Ptr {
472-
fn try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
476+
impl meta::EngineFromGodot for $Ptr {
477+
fn engine_try_from_godot(via: Self::Via) -> Result<Self, ConvertError> {
473478
Ok(via as Self)
474479
}
480+
481+
fn engine_try_from_variant(variant: &Variant) -> Result<Self, ConvertError> {
482+
variant.try_to::<i64>().map(|i| i as Self)
483+
}
475484
}
476485
};
477486
}
@@ -480,7 +489,7 @@ impl_pointer_convert!(*const std::ffi::c_void);
480489
impl_pointer_convert!(*mut std::ffi::c_void);
481490

482491
// Some other pointer types are used by various other methods, see https://github.com/godot-rust/gdext/issues/677
483-
// TODO: Find better solution to this, this may easily break still if godot decides to add more pointer arguments.
492+
// Keep manually extending this; no point in automating with how rarely Godot adds new pointer types.
484493

485494
impl_pointer_convert!(*mut *const u8);
486495
impl_pointer_convert!(*mut i32);

0 commit comments

Comments
 (0)