Skip to content

Commit 5bfdee7

Browse files
committed
Rename RefKind -> Memory
Also change Memory type-tags to enums (non-inhabitable types, no need for derive)
1 parent 4d96a95 commit 5bfdee7

File tree

10 files changed

+96
-92
lines changed

10 files changed

+96
-92
lines changed

bindings_generator/src/special_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn generate_godot_object_impl(class: &GodotClass) -> TokenStream {
5252
impl gdnative_core::private::godot_object::Sealed for #class_name {}
5353

5454
unsafe impl GodotObject for #class_name {
55-
type RefKind = #memory;
55+
type Memory = #memory;
5656

5757
#[inline]
5858
fn class_name() -> &'static str {

examples/dodge_the_creeps/src/main_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Main {
140140
/// scene as the root. For instance Spatial is used for this example.
141141
fn instance_scene<Root>(scene: &Ref<PackedScene, Shared>) -> Ref<Root, Unique>
142142
where
143-
Root: gdnative::object::GodotObject<RefKind = ManuallyManaged> + SubClass<Node>,
143+
Root: gdnative::object::GodotObject<Memory = ManuallyManaged> + SubClass<Node>,
144144
{
145145
let scene = unsafe { scene.assume_safe() };
146146

examples/native_plugin/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl MyButton {
5858

5959
unsafe fn load<T>(path: &str, hint: &str) -> Option<Ref<T, Shared>>
6060
where
61-
T: GodotObject<RefKind = RefCounted> + SubClass<Resource>,
61+
T: GodotObject<Memory = RefCounted> + SubClass<Resource>,
6262
{
6363
let resource = ResourceLoader::godot_singleton().load(path, hint, false)?;
6464
let resource = resource.assume_safe().claim();

examples/scene_create/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn load_scene(path: &str) -> Option<Ref<PackedScene, ThreadLocal>> {
116116
/// scene as the root. For instance Spatial is used for this example.
117117
fn instance_scene<Root>(scene: &PackedScene) -> Result<Ref<Root, Unique>, ManageErrs>
118118
where
119-
Root: gdnative::object::GodotObject<RefKind = ManuallyManaged> + SubClass<Node>,
119+
Root: gdnative::object::GodotObject<Memory = ManuallyManaged> + SubClass<Node>,
120120
{
121121
let instance = scene
122122
.instance(PackedScene::GEN_EDIT_STATE_DISABLED)

gdnative-core/src/object/bounds.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,51 @@ use crate::object::ownership::*;
88
use crate::object::*;
99

1010
// ----------------------------------------------------------------------------------------------------------------------------------------------
11-
// Implementation for RefKind policy
11+
// Implementation for Memory policy
1212

13-
/// Specialization trait depending on [`RefKind`]. This is an internal interface.
14-
pub trait RefKindSpec: Sized {
13+
/// Specialization trait depending on [`Memory`]. This is an internal interface.
14+
pub trait MemorySpec: Sized {
1515
/// Pointer wrapper that may be `Drop` or not.
1616
#[doc(hidden)]
1717
type PtrWrapper: PtrWrapper;
1818

1919
#[doc(hidden)]
20-
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<RefKind = Self>>(
20+
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<Memory = Self>>(
2121
ptr: NonNull<sys::godot_object>,
2222
) -> Option<Ref<T, Unique>>
2323
where
24-
Self: RefKind;
24+
Self: Memory;
2525

2626
#[doc(hidden)]
27-
unsafe fn impl_assume_safe<'a, T: GodotObject<RefKind = Self>>(
27+
unsafe fn impl_assume_safe<'a, T: GodotObject<Memory = Self>>(
2828
this: &Ref<T, Shared>,
2929
) -> TRef<'a, T, Shared>
3030
where
31-
Self: RefKind;
31+
Self: Memory;
3232

3333
#[doc(hidden)]
34-
unsafe fn impl_assume_unique<T: GodotObject<RefKind = Self>>(
34+
unsafe fn impl_assume_unique<T: GodotObject<Memory = Self>>(
3535
this: Ref<T, Shared>,
3636
) -> Ref<T, Unique>
3737
where
38-
Self: RefKind;
38+
Self: Memory;
3939

4040
#[doc(hidden)]
41-
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>)
41+
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>)
4242
where
43-
Self: RefKind;
43+
Self: Memory;
4444

4545
#[doc(hidden)]
46-
unsafe fn maybe_init_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>)
46+
unsafe fn maybe_init_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>)
4747
where
48-
Self: RefKind;
48+
Self: Memory;
4949
}
5050

51-
impl RefKindSpec for ManuallyManaged {
51+
impl MemorySpec for ManuallyManaged {
5252
type PtrWrapper = Forget;
5353

5454
#[inline(always)]
55-
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<RefKind = Self>>(
55+
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<Memory = Self>>(
5656
ptr: NonNull<sys::godot_object>,
5757
) -> Option<Ref<T, Unique>> {
5858
if RawObject::<ReferenceCountedClassPlaceholder>::try_from_sys_ref(ptr).is_some() {
@@ -71,7 +71,7 @@ impl RefKindSpec for ManuallyManaged {
7171
}
7272

7373
#[inline(always)]
74-
unsafe fn impl_assume_safe<'a, T: GodotObject<RefKind = Self>>(
74+
unsafe fn impl_assume_safe<'a, T: GodotObject<Memory = Self>>(
7575
this: &Ref<T, Shared>,
7676
) -> TRef<'a, T, Shared> {
7777
debug_assert!(
@@ -82,7 +82,7 @@ impl RefKindSpec for ManuallyManaged {
8282
}
8383

8484
#[inline(always)]
85-
unsafe fn impl_assume_unique<T: GodotObject<RefKind = Self>>(
85+
unsafe fn impl_assume_unique<T: GodotObject<Memory = Self>>(
8686
this: Ref<T, Shared>,
8787
) -> Ref<T, Unique> {
8888
debug_assert!(
@@ -93,16 +93,16 @@ impl RefKindSpec for ManuallyManaged {
9393
}
9494

9595
#[inline]
96-
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(_raw: &RawObject<T>) {}
96+
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(_raw: &RawObject<T>) {}
9797
#[inline]
98-
unsafe fn maybe_init_ref<T: GodotObject<RefKind = Self>>(_raw: &RawObject<T>) {}
98+
unsafe fn maybe_init_ref<T: GodotObject<Memory = Self>>(_raw: &RawObject<T>) {}
9999
}
100100

101-
impl RefKindSpec for RefCounted {
101+
impl MemorySpec for RefCounted {
102102
type PtrWrapper = UnRef;
103103

104104
#[inline(always)]
105-
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<RefKind = Self>>(
105+
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<Memory = Self>>(
106106
ptr: NonNull<sys::godot_object>,
107107
) -> Option<Ref<T, Unique>> {
108108
if RawObject::<ReferenceCountedClassPlaceholder>::try_from_sys_ref(ptr).is_some() {
@@ -120,26 +120,26 @@ impl RefKindSpec for RefCounted {
120120
}
121121

122122
#[inline(always)]
123-
unsafe fn impl_assume_safe<'a, T: GodotObject<RefKind = Self>>(
123+
unsafe fn impl_assume_safe<'a, T: GodotObject<Memory = Self>>(
124124
this: &Ref<T, Shared>,
125125
) -> TRef<'a, T, Shared> {
126126
this.assume_safe_unchecked()
127127
}
128128

129129
#[inline(always)]
130-
unsafe fn impl_assume_unique<T: GodotObject<RefKind = Self>>(
130+
unsafe fn impl_assume_unique<T: GodotObject<Memory = Self>>(
131131
this: Ref<T, Shared>,
132132
) -> Ref<T, Unique> {
133133
this.cast_access()
134134
}
135135

136136
#[inline]
137-
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>) {
137+
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>) {
138138
raw.add_ref();
139139
}
140140

141141
#[inline]
142-
unsafe fn maybe_init_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>) {
142+
unsafe fn maybe_init_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>) {
143143
raw.init_ref_count();
144144
}
145145
}
@@ -201,9 +201,9 @@ impl Drop for UnRef {
201201

202202
/// Trait for constraining `assume_safe` lifetimes to the one of `&self` when `T` is
203203
/// reference-counted. This is an internal interface.
204-
pub trait LifetimeConstraint<Kind: RefKind> {}
204+
pub trait LifetimeConstraint<Kind: Memory> {}
205205

206-
/// Type used to check lifetime constraint depending on `RefKind`. Internal interface.
206+
/// Type used to check lifetime constraint depending on `Memory`. Internal interface.
207207
#[doc(hidden)]
208208
pub struct AssumeSafeLifetime<'a, 'r> {
209209
_marker: PhantomData<(&'a (), &'r ())>,
@@ -215,19 +215,19 @@ impl<'a, 'r: 'a> LifetimeConstraint<RefCounted> for AssumeSafeLifetime<'a, 'r> {
215215
// ----------------------------------------------------------------------------------------------------------------------------------------------
216216
// SafeDeref, SafeAsRaw
217217

218-
/// Trait for combinations of `RefKind` and `ThreadAccess` that can be dereferenced safely.
218+
/// Trait for combinations of `Memory` and `Ownership` that can be dereferenced safely.
219219
/// This is an internal interface.
220-
pub unsafe trait SafeDeref<Kind: RefKind, Own: Ownership> {
220+
pub unsafe trait SafeDeref<Kind: Memory, Own: Ownership> {
221221
/// Returns a safe reference to the underlying object.
222222
#[doc(hidden)]
223-
fn impl_as_ref<T: GodotObject<RefKind = Kind>>(this: &Ref<T, Own>) -> TRef<'_, T, Own>;
223+
fn impl_as_ref<T: GodotObject<Memory = Kind>>(this: &Ref<T, Own>) -> TRef<'_, T, Own>;
224224
}
225225

226226
/// Trait for persistent `Ref`s that point to valid objects. This is an internal interface.
227-
pub unsafe trait SafeAsRaw<Kind: RefKind, Own: Ownership> {
227+
pub unsafe trait SafeAsRaw<Kind: Memory, Own: Ownership> {
228228
/// Returns a raw reference to the underlying object.
229229
#[doc(hidden)]
230-
fn impl_as_raw<T: GodotObject<RefKind = Kind>>(this: &Ref<T, Own>) -> &RawObject<T>;
230+
fn impl_as_raw<T: GodotObject<Memory = Kind>>(this: &Ref<T, Own>) -> &RawObject<T>;
231231
}
232232

233233
// ----------------------------------------------------------------------------------------------------------------------------------------------
@@ -240,7 +240,7 @@ pub struct RefImplBound {
240240

241241
unsafe impl SafeDeref<ManuallyManaged, Unique> for RefImplBound {
242242
#[inline]
243-
fn impl_as_ref<T: GodotObject<RefKind = ManuallyManaged>>(
243+
fn impl_as_ref<T: GodotObject<Memory = ManuallyManaged>>(
244244
this: &Ref<T, Unique>,
245245
) -> TRef<'_, T, Unique> {
246246
unsafe { this.assume_safe_unchecked() }
@@ -249,14 +249,14 @@ unsafe impl SafeDeref<ManuallyManaged, Unique> for RefImplBound {
249249

250250
unsafe impl<Own: LocalThreadOwnership> SafeDeref<RefCounted, Own> for RefImplBound {
251251
#[inline]
252-
fn impl_as_ref<T: GodotObject<RefKind = RefCounted>>(this: &Ref<T, Own>) -> TRef<'_, T, Own> {
252+
fn impl_as_ref<T: GodotObject<Memory = RefCounted>>(this: &Ref<T, Own>) -> TRef<'_, T, Own> {
253253
unsafe { this.assume_safe_unchecked() }
254254
}
255255
}
256256

257257
unsafe impl SafeAsRaw<ManuallyManaged, Unique> for RefImplBound {
258258
#[inline]
259-
fn impl_as_raw<T: GodotObject<RefKind = ManuallyManaged>>(
259+
fn impl_as_raw<T: GodotObject<Memory = ManuallyManaged>>(
260260
this: &Ref<T, Unique>,
261261
) -> &RawObject<T> {
262262
unsafe { this.as_raw_unchecked() }
@@ -265,7 +265,7 @@ unsafe impl SafeAsRaw<ManuallyManaged, Unique> for RefImplBound {
265265

266266
unsafe impl<Own: Ownership> SafeAsRaw<RefCounted, Own> for RefImplBound {
267267
#[inline]
268-
fn impl_as_raw<T: GodotObject<RefKind = RefCounted>>(this: &Ref<T, Own>) -> &RawObject<T> {
268+
fn impl_as_raw<T: GodotObject<Memory = RefCounted>>(this: &Ref<T, Own>) -> &RawObject<T> {
269269
unsafe { this.as_raw_unchecked() }
270270
}
271271
}

gdnative-core/src/object/instance.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<T: NativeClass, Own: Ownership> Instance<T, Own> {
233233

234234
impl<T: NativeClass, Own: Ownership> Instance<T, Own>
235235
where
236-
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::RefKind, Own>,
236+
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::Memory, Own>,
237237
{
238238
/// Try to downcast `Ref<T::Base, Own>` to `Instance<T>`, without changing the reference
239239
/// count if reference-counted.
@@ -263,7 +263,7 @@ where
263263

264264
impl<T: NativeClass, Own: Ownership> Instance<T, Own>
265265
where
266-
RefImplBound: SafeDeref<<T::Base as GodotObject>::RefKind, Own>,
266+
RefImplBound: SafeDeref<<T::Base as GodotObject>::Memory, Own>,
267267
{
268268
/// Calls a function with a NativeClass instance and its owner, and returns its return
269269
/// value. Can be used on reference counted types for multiple times.
@@ -314,7 +314,7 @@ impl<T: NativeClass> Instance<T, Shared> {
314314
#[inline]
315315
pub unsafe fn assume_safe<'a, 'r>(&'r self) -> TInstance<'a, T, Shared>
316316
where
317-
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::RefKind>,
317+
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::Memory>,
318318
{
319319
TInstance {
320320
owner: self.owner.assume_safe(),
@@ -325,7 +325,7 @@ impl<T: NativeClass> Instance<T, Shared> {
325325

326326
impl<T: NativeClass> Instance<T, Shared>
327327
where
328-
T::Base: GodotObject<RefKind = ManuallyManaged>,
328+
T::Base: GodotObject<Memory = ManuallyManaged>,
329329
{
330330
/// Returns `true` if the pointer currently points to a valid object of the correct type.
331331
/// **This does NOT guarantee that it's safe to use this pointer.**
@@ -342,7 +342,7 @@ where
342342

343343
impl<T: NativeClass> Instance<T, Unique>
344344
where
345-
T::Base: GodotObject<RefKind = ManuallyManaged>,
345+
T::Base: GodotObject<Memory = ManuallyManaged>,
346346
{
347347
/// Frees the base object and user-data wrapper.
348348
///
@@ -355,7 +355,7 @@ where
355355

356356
impl<T: NativeClass> Instance<T, Unique>
357357
where
358-
T::Base: GodotObject<RefKind = ManuallyManaged> + QueueFree,
358+
T::Base: GodotObject<Memory = ManuallyManaged> + QueueFree,
359359
{
360360
/// Queues the base object and user-data wrapper for deallocation in the near future.
361361
/// This should be preferred to `free` for `Node`s.
@@ -380,7 +380,7 @@ impl<T: NativeClass> Instance<T, Unique> {
380380

381381
impl<T: NativeClass> Instance<T, Unique>
382382
where
383-
T::Base: GodotObject<RefKind = RefCounted>,
383+
T::Base: GodotObject<Memory = RefCounted>,
384384
{
385385
/// Coverts into a `ThreadLocal` instance.
386386
#[inline]
@@ -414,7 +414,7 @@ impl<T: NativeClass> Instance<T, Shared> {
414414

415415
impl<T: NativeClass> Instance<T, Shared>
416416
where
417-
T::Base: GodotObject<RefKind = RefCounted>,
417+
T::Base: GodotObject<Memory = RefCounted>,
418418
{
419419
/// Assume that all references to the underlying object is local to the current thread.
420420
///
@@ -570,7 +570,7 @@ where
570570
impl<T> FromVariant for Instance<T, Shared>
571571
where
572572
T: NativeClass,
573-
T::Base: GodotObject<RefKind = RefCounted>,
573+
T::Base: GodotObject<Memory = RefCounted>,
574574
{
575575
#[inline]
576576
fn from_variant(variant: &Variant) -> Result<Self, FromVariantError> {

gdnative-core/src/object/memory.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
//! Marker types to express the memory management method of Godot types.
2+
// Note: markers are enums to prevent instantiation and avoid derive (empty/non-inhabitable types)
23

3-
use crate::object::bounds::RefKindSpec;
4+
use crate::object::bounds::MemorySpec;
45

56
/// Marker that indicates that a type is manually managed.
6-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
7-
pub struct ManuallyManaged;
7+
pub enum ManuallyManaged {}
88

99
/// Marker that indicates that a type is reference-counted.
10-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
11-
pub struct RefCounted;
10+
pub enum RefCounted {}
1211

1312
/// Trait to parameterize over the memory management markers [`ManuallyManaged`] and [`RefCounted`].
1413
///
1514
/// This trait is sealed and has no public members.
16-
pub trait RefKind: RefKindSpec + private::Sealed {}
15+
///
16+
/// It defines how memory is managed for Godot objects in smart pointers, for example [`Ref`][super::Ref].
17+
/// Generally, classes inheriting `Reference` are ref-counted, while the rest (i.e. everything inheriting
18+
/// `Object` which is not a `Reference`) is manually managed.
19+
pub trait Memory: MemorySpec + private::Sealed {}
1720

18-
impl RefKind for ManuallyManaged {}
21+
impl Memory for ManuallyManaged {}
1922
impl private::Sealed for ManuallyManaged {}
2023

21-
impl RefKind for RefCounted {}
24+
impl Memory for RefCounted {}
2225
impl private::Sealed for RefCounted {}
26+
2327
mod private {
2428
pub trait Sealed {}
2529
}

0 commit comments

Comments
 (0)