Skip to content

Commit 83ce546

Browse files
committed
- Cleanup docs in rawgd/gd
- Other cleanup
1 parent 5719363 commit 83ce546

File tree

12 files changed

+208
-235
lines changed

12 files changed

+208
-235
lines changed

godot-core/src/builtin/callable.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ impl Callable {
4646
unsafe {
4747
sys::from_sys_init_or_init_default::<Self>(|self_ptr| {
4848
let ctor = sys::builtin_fn!(callable_from_object_method);
49-
let args = [object.to_ffi().as_arg_ptr(), method.sys_const()];
49+
let raw = object.to_ffi();
50+
let args = [raw.as_arg_ptr(), method.sys_const()];
5051
ctor(self_ptr, args.as_ptr());
5152
})
5253
}

godot-core/src/builtin/meta/godot_compat/impls.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ where
4545
}
4646

4747
impl GodotCompatible for sys::VariantType {
48-
type Via = i64;
48+
type Via = i32;
4949
}
5050

5151
impl ToGodot for sys::VariantType {
5252
fn to_godot(&self) -> Self::Via {
53-
*self as i64
53+
*self as i32
5454
}
5555

5656
fn into_godot(self) -> Self::Via {
57-
self as i64
57+
self as i32
5858
}
5959
}
6060

@@ -65,16 +65,16 @@ impl FromGodot for sys::VariantType {
6565
}
6666

6767
impl GodotCompatible for sys::VariantOperator {
68-
type Via = i64;
68+
type Via = i32;
6969
}
7070

7171
impl ToGodot for sys::VariantOperator {
7272
fn to_godot(&self) -> Self::Via {
73-
*self as i64
73+
*self as i32
7474
}
7575

7676
fn into_godot(self) -> Self::Via {
77-
self as i64
77+
self as i32
7878
}
7979
}
8080

godot-core/src/builtin/meta/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ where
183183
}
184184
}
185185

186-
/// Stores meta-information about registered types or properties.
187-
///
188-
/// Filling this information properly is important so that Godot can use ptrcalls instead of varcalls
189-
/// (requires typed GDScript + sufficient information from the extension side)
190-
191186
// ----------------------------------------------------------------------------------------------------------------------------------------------
192187

193188
/// Rusty abstraction of `sys::GDExtensionPropertyInfo`.

godot-core/src/builtin/meta/signature.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ macro_rules! impl_varcall_signature_for_tuple {
112112
$R:ident
113113
$(, ($pn:ident, $n:tt) : $Pn:ident)* // $n cannot be literal if substituted as tuple index .0
114114
) => {
115-
// R: FromVariantIndirect, Pn: ToVariant -> when calling engine APIs
116-
// R: ToVariant, Pn:
117115
#[allow(unused_variables)]
118116
impl<$R, $($Pn,)*> VarcallSignatureTuple for ($R, $($Pn,)*)
119117
where
@@ -400,7 +398,7 @@ unsafe fn varcall_return<R: ToGodot>(
400398
/// # Safety
401399
/// See [`varcall_return`].
402400
#[cfg(since_api = "4.2")] // unused before
403-
pub(crate) unsafe fn varcall_return_checked<R: ToVariant>(
401+
pub(crate) unsafe fn varcall_return_checked<R: ToGodot>(
404402
ret_val: Result<R, ()>, // TODO Err should be custom CallError enum
405403
ret: sys::GDExtensionVariantPtr,
406404
err: *mut sys::GDExtensionCallError,

godot-core/src/builtin/string/string_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7+
use std::fmt;
8+
79
use godot_ffi as sys;
810
use sys::{ffi_methods, GodotFfi};
911

1012
use crate::builtin::inner;
1113
use crate::builtin::meta::impl_godot_as_self;
1214
use crate::builtin::{GodotString, NodePath};
1315

14-
use std::fmt;
15-
1616
/// A string optimized for unique names.
1717
///
1818
/// StringNames are immutable strings designed for representing unique names. StringName ensures that only

godot-core/src/builtin/vectors/vector_axis.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ impl EngineEnum for Vector2Axis {
9898
}
9999

100100
impl GodotCompatible for Vector2Axis {
101-
type Via = i64;
101+
type Via = i32;
102102
}
103103

104104
impl ToGodot for Vector2Axis {
105105
fn to_godot(&self) -> Self::Via {
106-
self.ord() as i64
106+
self.ord()
107107
}
108108
}
109109

110110
impl FromGodot for Vector2Axis {
111111
fn try_from_godot(via: Self::Via) -> Option<Self> {
112-
Self::try_from_ord(via as i32)
112+
Self::try_from_ord(via)
113113
}
114114
}
115115

@@ -146,18 +146,18 @@ impl EngineEnum for Vector3Axis {
146146
}
147147

148148
impl GodotCompatible for Vector3Axis {
149-
type Via = i64;
149+
type Via = i32;
150150
}
151151

152152
impl ToGodot for Vector3Axis {
153153
fn to_godot(&self) -> Self::Via {
154-
self.ord() as i64
154+
self.ord()
155155
}
156156
}
157157

158158
impl FromGodot for Vector3Axis {
159159
fn try_from_godot(via: Self::Via) -> Option<Self> {
160-
Self::try_from_ord(via as i32)
160+
Self::try_from_ord(via)
161161
}
162162
}
163163

@@ -197,18 +197,18 @@ impl EngineEnum for Vector4Axis {
197197
}
198198

199199
impl GodotCompatible for Vector4Axis {
200-
type Via = i64;
200+
type Via = i32;
201201
}
202202

203203
impl ToGodot for Vector4Axis {
204204
fn to_godot(&self) -> Self::Via {
205-
self.ord() as i64
205+
self.ord()
206206
}
207207
}
208208

209209
impl FromGodot for Vector4Axis {
210210
fn try_from_godot(via: Self::Via) -> Option<Self> {
211-
Self::try_from_ord(via as i32)
211+
Self::try_from_ord(via)
212212
}
213213
}
214214

godot-core/src/engine.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
//! Godot engine classes and methods.
88
9-
use godot_ffi::GodotNullableFfi;
10-
119
// Re-exports of generated symbols
1210
use crate::builtin::{GodotString, NodePath};
1311
use crate::obj::dom::EngineDomain;
@@ -223,7 +221,7 @@ pub(crate) fn ensure_object_alive(
223221
method_name: &'static str,
224222
) {
225223
let Some(instance_id) = instance_id else {
226-
panic!("{method_name}: instance id is null")
224+
panic!("{method_name}: cannot call method on null object")
227225
};
228226

229227
let new_object_ptr = object_ptr_from_id(instance_id);

godot-core/src/obj/gd.rs

+27-60
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::ptr;
1010

1111
use godot_ffi as sys;
1212
use godot_ffi::VariantType;
13-
use sys::{interface_fn, static_assert_eq_size, GodotNullableFfi};
13+
use sys::static_assert_eq_size;
1414

1515
use crate::builtin::meta::{FromGodot, GodotCompatible, GodotType, ToGodot};
1616
use crate::builtin::{Callable, StringName};
@@ -149,28 +149,6 @@ where
149149
pub fn bind_mut(&mut self) -> GdMut<T> {
150150
self.raw.bind_mut()
151151
}
152-
153-
/// Storage object associated with the extension instance.
154-
// pub(crate) fn storage_mut(&mut self) -> &mut InstanceStorage<T> {
155-
// // SAFETY:
156-
// unsafe {
157-
// let binding = self.resolve_instance_ptr();
158-
// crate::private::as_storage_mut::<T>(binding)
159-
// }
160-
// }
161-
162-
unsafe fn resolve_instance_ptr(&self) -> sys::GDExtensionClassInstancePtr {
163-
let callbacks = crate::storage::nop_instance_callbacks();
164-
let token = sys::get_library() as *mut std::ffi::c_void;
165-
let binding = interface_fn!(object_get_instance_binding)(self.obj_sys(), token, &callbacks);
166-
167-
debug_assert!(
168-
!binding.is_null(),
169-
"Class {} -- null instance; does the class have a Godot creator function?",
170-
std::any::type_name::<T>()
171-
);
172-
binding as sys::GDExtensionClassInstancePtr
173-
}
174152
}
175153

176154
/// _The methods in this impl block are available for any `T`._ <br><br>
@@ -182,13 +160,9 @@ impl<T: GodotClass> Gd<T> {
182160
pub fn try_from_instance_id(instance_id: InstanceId) -> Option<Self> {
183161
let ptr = engine::object_ptr_from_id(instance_id);
184162

185-
if ptr.is_null() {
186-
None
187-
} else {
188-
// SAFETY: assumes that the returned GDExtensionObjectPtr is convertible to Object* (i.e. C++ upcast doesn't modify the pointer)
189-
let untyped = unsafe { Gd::<engine::Object>::from_obj_sys(ptr) };
190-
untyped.owned_cast::<T>().ok()
191-
}
163+
// SAFETY: assumes that the returned GDExtensionObjectPtr is convertible to Object* (i.e. C++ upcast doesn't modify the pointer)
164+
let untyped = unsafe { Gd::<engine::Object>::from_obj_sys_or_none(ptr)? };
165+
untyped.owned_cast::<T>().ok()
192166
}
193167

194168
/// ⚠️ Looks up the given instance ID and returns the associated object.
@@ -227,9 +201,13 @@ impl<T: GodotClass> Gd<T> {
227201
/// ⚠️ Returns the last known, possibly invalid instance ID of this object.
228202
///
229203
/// This function does not check that the returned instance ID points to a valid instance!
230-
/// Unless performance is a problem, use [`instance_id()`][Self::instance_id] or [`instance_id_or_none()`][Self::instance_id_or_none] instead.
204+
/// Unless performance is a problem, use [`instance_id()`][Self::instance_id] or
205+
/// [`instance_id_or_none()`][Self::instance_id_or_none] instead.
231206
pub fn instance_id_unchecked(&self) -> InstanceId {
232-
self.raw.cached_instance_id.get().unwrap()
207+
// SAFETY:
208+
// A `Gd` can only be created from a non-null `RawGd`. Meaning `raw.instance_id_unchecked()` will
209+
// always return `Some`.
210+
unsafe { self.raw.instance_id_unchecked().unwrap_unchecked() }
233211
}
234212

235213
/// Checks if this smart pointer points to a live object (read description!).
@@ -308,23 +286,33 @@ impl<T: GodotClass> Gd<T> {
308286
.map_err(Self::from_ffi)
309287
}
310288

289+
#[doc(hidden)]
290+
pub(crate) unsafe fn from_obj_sys_or_none(ptr: sys::GDExtensionObjectPtr) -> Option<Self> {
291+
Self::try_from_ffi(RawGd::from_obj_sys(ptr))
292+
}
293+
311294
/// Initializes this `Gd<T>` from the object pointer as a **strong ref**, meaning
312295
/// it initializes/increments the reference counter and keeps the object alive.
313296
///
314297
/// This is the default for most initializations from FFI. In cases where reference counter
315298
/// should explicitly **not** be updated, [`Self::from_obj_sys_weak`] is available.
316299
#[doc(hidden)]
317-
pub unsafe fn from_obj_sys(ptr: sys::GDExtensionObjectPtr) -> Self {
318-
Self::from_ffi(RawGd::from_obj_sys(ptr))
300+
pub(crate) unsafe fn from_obj_sys(ptr: sys::GDExtensionObjectPtr) -> Self {
301+
Self::from_obj_sys_or_none(ptr).unwrap()
319302
}
320303

321304
#[doc(hidden)]
322-
pub unsafe fn from_obj_sys_weak(ptr: sys::GDExtensionObjectPtr) -> Self {
323-
Self::from_ffi(RawGd::from_obj_sys_weak(ptr))
305+
pub(crate) unsafe fn from_obj_sys_weak_or_none(ptr: sys::GDExtensionObjectPtr) -> Option<Self> {
306+
Self::try_from_ffi(RawGd::from_obj_sys_weak(ptr))
324307
}
325308

326309
#[doc(hidden)]
327-
pub fn obj_sys(&self) -> sys::GDExtensionObjectPtr {
310+
pub(crate) unsafe fn from_obj_sys_weak(ptr: sys::GDExtensionObjectPtr) -> Self {
311+
Self::from_obj_sys_weak_or_none(ptr).unwrap()
312+
}
313+
314+
#[doc(hidden)]
315+
pub(crate) fn obj_sys(&self) -> sys::GDExtensionObjectPtr {
328316
self.raw.obj_sys()
329317
}
330318

@@ -341,34 +329,13 @@ impl<T: GodotClass> Deref for Gd<T> {
341329
type Target = <<T as GodotClass>::Declarer as dom::Domain>::DerefTarget<T>;
342330

343331
fn deref(&self) -> &Self::Target {
344-
// SAFETY:
345-
//
346-
// This relies on `Gd<Node3D>` having the layout as `Node3D` (as an example),
347-
// which also needs #[repr(transparent)]:
348-
//
349-
// struct Gd<T: GodotClass> {
350-
// opaque: OpaqueObject, // <- size of GDExtensionObjectPtr
351-
// cached_instance_id, // <- Cell is #[repr(transparent)] to its inner T
352-
// _marker: PhantomData, // <- ZST
353-
// }
354-
// struct Node3D {
355-
// object_ptr: sys::GDExtensionObjectPtr,
356-
// }
357-
self.raw.as_target()
332+
self.raw.as_target().expect("`Gd` is never null")
358333
}
359334
}
360335

361336
impl<T: GodotClass> DerefMut for Gd<T> {
362337
fn deref_mut(&mut self) -> &mut Self::Target {
363-
// SAFETY: see also Deref
364-
//
365-
// The resulting `&mut T` is transmuted from `&mut OpaqueObject`, i.e. a *pointer* to the `opaque` field.
366-
// `opaque` itself has a different *address* for each Gd instance, meaning that two simultaneous
367-
// DerefMut borrows on two Gd instances will not alias, *even if* the underlying Godot object is the
368-
// same (i.e. `opaque` has the same value, but not address).
369-
//
370-
// The `&mut self` guarantees that no other base access can take place for *the same Gd instance* (access to other Gds is OK).
371-
self.raw.as_target_mut()
338+
self.raw.as_target_mut().expect("`Gd` is never null")
372339
}
373340
}
374341

godot-core/src/obj/instance_id.rs

-25
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,3 @@ impl FromGodot for InstanceId {
8989
Self::try_from_u64(via)
9090
}
9191
}
92-
93-
/*
94-
// Note: Option impl is only possible as long as `FromVariant` and `InstanceId` are in same crate.
95-
// (Rust rationale: if upstream crate later adds blanket `impl FromVariant for Option<T>`, this would collide)
96-
impl FromVariant for Option<InstanceId> {
97-
fn try_from_variant(variant: &Variant) -> Result<Self, VariantConversionError> {
98-
if variant.is_nil() {
99-
Ok(None)
100-
} else {
101-
// Should 0 in variant be mapped to None, or cause an error like now?
102-
i64::try_from_variant(variant).map(|i| InstanceId::try_from_i64(i))
103-
}
104-
}
105-
}
106-
107-
impl ToVariant for Option<InstanceId> {
108-
fn to_variant(&self) -> Variant {
109-
if let Some(id) = self {
110-
id.to_variant()
111-
} else {
112-
0i64.to_variant()
113-
}
114-
}
115-
}
116-
*/

0 commit comments

Comments
 (0)