Skip to content

Commit 3151842

Browse files
lilizoeymhoff12358
authored andcommitted
Add a get_gd method to InstanceStorage
1 parent 0d4670f commit 3151842

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

godot-core/src/obj/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub struct Base<T: GodotClass> {
3535
}
3636

3737
impl<T: GodotClass> Base<T> {
38+
/// # Safety
39+
/// The returned Base is a weak pointer, so holding it will not keep the object alive. It must not be accessed after the object is destroyed.
40+
pub(crate) unsafe fn from_base(base: &Base<T>) -> Base<T> {
41+
Base::from_obj(Gd::from_obj_sys_weak(base.obj_sys()))
42+
}
43+
3844
// Note: not &mut self, to only borrow one field and not the entire struct
3945
pub(crate) unsafe fn from_sys(base_ptr: sys::GDExtensionObjectPtr) -> Self {
4046
assert!(!base_ptr.is_null(), "instance base is null pointer");

godot-core/src/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ pub mod callbacks {
313313
unsafe { interface_fn!(classdb_construct_object)(base_class_name.string_sys()) };
314314

315315
let base = unsafe { Base::from_sys(base_ptr) };
316-
let user_instance = make_user_instance(base);
316+
let user_instance = make_user_instance(unsafe { Base::from_base(&base) });
317317

318-
let instance = InstanceStorage::<T>::construct(user_instance);
318+
let instance = InstanceStorage::<T>::construct(user_instance, base);
319319
let instance_ptr = instance.into_raw();
320320
let instance_ptr = instance_ptr as sys::GDExtensionClassInstancePtr;
321321

godot-core/src/storage.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ mod single_threaded {
2929
use std::any::type_name;
3030
use std::cell;
3131

32-
use crate::obj::GodotClass;
32+
use crate::obj::{Base, Gd, GodotClass, Inherits};
3333
use crate::out;
3434

3535
use super::Lifecycle;
3636

3737
/// Manages storage and lifecycle of user's extension class instances.
3838
pub struct InstanceStorage<T: GodotClass> {
3939
user_instance: cell::RefCell<T>,
40+
base: Base<T::Base>,
4041

4142
// Declared after `user_instance`, is dropped last
4243
pub lifecycle: cell::Cell<Lifecycle>,
@@ -45,11 +46,12 @@ mod single_threaded {
4546

4647
/// For all Godot extension classes
4748
impl<T: GodotClass> InstanceStorage<T> {
48-
pub fn construct(user_instance: T) -> Self {
49+
pub fn construct(user_instance: T, base: Base<T::Base>) -> Self {
4950
out!(" Storage::construct <{}>", type_name::<T>());
5051

5152
Self {
5253
user_instance: cell::RefCell::new(user_instance),
54+
base,
5355
lifecycle: cell::Cell::new(Lifecycle::Alive),
5456
godot_ref_count: cell::Cell::new(1),
5557
}
@@ -101,6 +103,13 @@ mod single_threaded {
101103
})
102104
}
103105

106+
pub fn get_gd(&self) -> Gd<T>
107+
where
108+
T: Inherits<<T as GodotClass>::Base>,
109+
{
110+
self.base.clone().cast()
111+
}
112+
104113
pub(super) fn godot_ref_count(&self) -> u32 {
105114
self.godot_ref_count.get()
106115
}
@@ -113,7 +122,7 @@ mod multi_threaded {
113122
use std::sync;
114123
use std::sync::atomic::{AtomicU32, Ordering};
115124

116-
use crate::obj::GodotClass;
125+
use crate::obj::{Base, Gd, GodotClass, Inherits, Share};
117126
use crate::out;
118127

119128
use super::Lifecycle;
@@ -146,6 +155,7 @@ mod multi_threaded {
146155
/// Manages storage and lifecycle of user's extension class instances.
147156
pub struct InstanceStorage<T: GodotClass> {
148157
user_instance: sync::RwLock<T>,
158+
base: Base<T::Base>,
149159

150160
// Declared after `user_instance`, is dropped last
151161
pub lifecycle: AtomicLifecycle,
@@ -154,11 +164,12 @@ mod multi_threaded {
154164

155165
/// For all Godot extension classes
156166
impl<T: GodotClass> InstanceStorage<T> {
157-
pub fn construct(user_instance: T) -> Self {
167+
pub fn construct(user_instance: T, base: Base<T::Base>) -> Self {
158168
out!(" Storage::construct <{}>", type_name::<T>());
159169

160170
Self {
161171
user_instance: sync::RwLock::new(user_instance),
172+
base,
162173
lifecycle: AtomicLifecycle::new(Lifecycle::Alive),
163174
godot_ref_count: AtomicU32::new(1),
164175
}
@@ -206,6 +217,13 @@ mod multi_threaded {
206217
})
207218
}
208219

220+
pub fn get_gd(&self) -> Gd<T>
221+
where
222+
T: Inherits<<T as GodotClass>::Base>,
223+
{
224+
self.base.clone().cast()
225+
}
226+
209227
pub(super) fn godot_ref_count(&self) -> u32 {
210228
self.godot_ref_count.load(Ordering::Relaxed)
211229
}

0 commit comments

Comments
 (0)