Skip to content

Commit

Permalink
Rename RefKind -> Memory
Browse files Browse the repository at this point in the history
Also change Memory type-tags to enums (non-inhabitable types, no need for derive)
  • Loading branch information
Bromeon committed Nov 12, 2021
1 parent 4d96a95 commit 1e5b2cc
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 92 deletions.
2 changes: 1 addition & 1 deletion bindings_generator/src/special_methods.rs
Expand Up @@ -52,7 +52,7 @@ pub fn generate_godot_object_impl(class: &GodotClass) -> TokenStream {
impl gdnative_core::private::godot_object::Sealed for #class_name {}

unsafe impl GodotObject for #class_name {
type RefKind = #memory;
type Memory = #memory;

#[inline]
fn class_name() -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion examples/dodge_the_creeps/src/main_scene.rs
Expand Up @@ -140,7 +140,7 @@ impl Main {
/// scene as the root. For instance Spatial is used for this example.
fn instance_scene<Root>(scene: &Ref<PackedScene, Shared>) -> Ref<Root, Unique>
where
Root: gdnative::object::GodotObject<RefKind = ManuallyManaged> + SubClass<Node>,
Root: gdnative::object::GodotObject<Memory = ManuallyManaged> + SubClass<Node>,
{
let scene = unsafe { scene.assume_safe() };

Expand Down
2 changes: 1 addition & 1 deletion examples/native_plugin/src/lib.rs
Expand Up @@ -58,7 +58,7 @@ impl MyButton {

unsafe fn load<T>(path: &str, hint: &str) -> Option<Ref<T, Shared>>
where
T: GodotObject<RefKind = RefCounted> + SubClass<Resource>,
T: GodotObject<Memory = RefCounted> + SubClass<Resource>,
{
let resource = ResourceLoader::godot_singleton().load(path, hint, false)?;
let resource = resource.assume_safe().claim();
Expand Down
2 changes: 1 addition & 1 deletion examples/scene_create/src/lib.rs
Expand Up @@ -116,7 +116,7 @@ pub fn load_scene(path: &str) -> Option<Ref<PackedScene, ThreadLocal>> {
/// scene as the root. For instance Spatial is used for this example.
fn instance_scene<Root>(scene: &PackedScene) -> Result<Ref<Root, Unique>, ManageErrs>
where
Root: gdnative::object::GodotObject<RefKind = ManuallyManaged> + SubClass<Node>,
Root: gdnative::object::GodotObject<Memory = ManuallyManaged> + SubClass<Node>,
{
let instance = scene
.instance(PackedScene::GEN_EDIT_STATE_DISABLED)
Expand Down
72 changes: 36 additions & 36 deletions gdnative-core/src/object/bounds.rs
Expand Up @@ -8,51 +8,51 @@ use crate::object::ownership::*;
use crate::object::*;

// ----------------------------------------------------------------------------------------------------------------------------------------------
// Implementation for RefKind policy
// Implementation for Memory policy

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

#[doc(hidden)]
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<RefKind = Self>>(
unsafe fn impl_from_maybe_ref_counted<T: GodotObject<Memory = Self>>(
ptr: NonNull<sys::godot_object>,
) -> Option<Ref<T, Unique>>
where
Self: RefKind;
Self: Memory;

#[doc(hidden)]
unsafe fn impl_assume_safe<'a, T: GodotObject<RefKind = Self>>(
unsafe fn impl_assume_safe<'a, T: GodotObject<Memory = Self>>(
this: &Ref<T, Shared>,
) -> TRef<'a, T, Shared>
where
Self: RefKind;
Self: Memory;

#[doc(hidden)]
unsafe fn impl_assume_unique<T: GodotObject<RefKind = Self>>(
unsafe fn impl_assume_unique<T: GodotObject<Memory = Self>>(
this: Ref<T, Shared>,
) -> Ref<T, Unique>
where
Self: RefKind;
Self: Memory;

#[doc(hidden)]
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>)
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>)
where
Self: RefKind;
Self: Memory;

#[doc(hidden)]
unsafe fn maybe_init_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>)
unsafe fn maybe_init_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>)
where
Self: RefKind;
Self: Memory;
}

impl RefKindSpec for ManuallyManaged {
impl MemorySpec for ManuallyManaged {
type PtrWrapper = Forget;

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

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

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

#[inline]
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(_raw: &RawObject<T>) {}
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(_raw: &RawObject<T>) {}
#[inline]
unsafe fn maybe_init_ref<T: GodotObject<RefKind = Self>>(_raw: &RawObject<T>) {}
unsafe fn maybe_init_ref<T: GodotObject<Memory = Self>>(_raw: &RawObject<T>) {}
}

impl RefKindSpec for RefCounted {
impl MemorySpec for RefCounted {
type PtrWrapper = UnRef;

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

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

#[inline(always)]
unsafe fn impl_assume_unique<T: GodotObject<RefKind = Self>>(
unsafe fn impl_assume_unique<T: GodotObject<Memory = Self>>(
this: Ref<T, Shared>,
) -> Ref<T, Unique> {
this.cast_access()
}

#[inline]
unsafe fn maybe_add_ref<T: GodotObject<RefKind = Self>>(raw: &RawObject<T>) {
unsafe fn maybe_add_ref<T: GodotObject<Memory = Self>>(raw: &RawObject<T>) {
raw.add_ref();
}

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

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

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

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

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

// ----------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -240,7 +240,7 @@ pub struct RefImplBound {

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

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

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

unsafe impl<Own: Ownership> SafeAsRaw<RefCounted, Own> for RefImplBound {
#[inline]
fn impl_as_raw<T: GodotObject<RefKind = RefCounted>>(this: &Ref<T, Own>) -> &RawObject<T> {
fn impl_as_raw<T: GodotObject<Memory = RefCounted>>(this: &Ref<T, Own>) -> &RawObject<T> {
unsafe { this.as_raw_unchecked() }
}
}
18 changes: 9 additions & 9 deletions gdnative-core/src/object/instance.rs
Expand Up @@ -233,7 +233,7 @@ impl<T: NativeClass, Own: Ownership> Instance<T, Own> {

impl<T: NativeClass, Own: Ownership> Instance<T, Own>
where
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::RefKind, Own>,
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::Memory, Own>,
{
/// Try to downcast `Ref<T::Base, Own>` to `Instance<T>`, without changing the reference
/// count if reference-counted.
Expand Down Expand Up @@ -263,7 +263,7 @@ where

impl<T: NativeClass, Own: Ownership> Instance<T, Own>
where
RefImplBound: SafeDeref<<T::Base as GodotObject>::RefKind, Own>,
RefImplBound: SafeDeref<<T::Base as GodotObject>::Memory, Own>,
{
/// Calls a function with a NativeClass instance and its owner, and returns its return
/// value. Can be used on reference counted types for multiple times.
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<T: NativeClass> Instance<T, Shared> {
#[inline]
pub unsafe fn assume_safe<'a, 'r>(&'r self) -> TInstance<'a, T, Shared>
where
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::RefKind>,
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::Memory>,
{
TInstance {
owner: self.owner.assume_safe(),
Expand All @@ -325,7 +325,7 @@ impl<T: NativeClass> Instance<T, Shared> {

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

impl<T: NativeClass> Instance<T, Unique>
where
T::Base: GodotObject<RefKind = ManuallyManaged>,
T::Base: GodotObject<Memory = ManuallyManaged>,
{
/// Frees the base object and user-data wrapper.
///
Expand All @@ -355,7 +355,7 @@ where

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

impl<T: NativeClass> Instance<T, Unique>
where
T::Base: GodotObject<RefKind = RefCounted>,
T::Base: GodotObject<Memory = RefCounted>,
{
/// Coverts into a `ThreadLocal` instance.
#[inline]
Expand Down Expand Up @@ -414,7 +414,7 @@ impl<T: NativeClass> Instance<T, Shared> {

impl<T: NativeClass> Instance<T, Shared>
where
T::Base: GodotObject<RefKind = RefCounted>,
T::Base: GodotObject<Memory = RefCounted>,
{
/// Assume that all references to the underlying object is local to the current thread.
///
Expand Down Expand Up @@ -570,7 +570,7 @@ where
impl<T> FromVariant for Instance<T, Shared>
where
T: NativeClass,
T::Base: GodotObject<RefKind = RefCounted>,
T::Base: GodotObject<Memory = RefCounted>,
{
#[inline]
fn from_variant(variant: &Variant) -> Result<Self, FromVariantError> {
Expand Down
15 changes: 7 additions & 8 deletions gdnative-core/src/object/memory.rs
@@ -1,24 +1,23 @@
//! Marker types to express the memory management method of Godot types.
// Note: markers are enums to prevent instantiation and avoid derive (empty/non-inhabitable types)

use crate::object::bounds::RefKindSpec;
use crate::object::bounds::MemorySpec;

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

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

/// Trait to parameterize over the memory management markers [`ManuallyManaged`] and [`RefCounted`].
///
/// This trait is sealed and has no public members.
pub trait RefKind: RefKindSpec + private::Sealed {}
pub trait Memory: MemorySpec + private::Sealed {}

impl RefKind for ManuallyManaged {}
impl Memory for ManuallyManaged {}
impl private::Sealed for ManuallyManaged {}

impl RefKind for RefCounted {}
impl Memory for RefCounted {}
impl private::Sealed for RefCounted {}
mod private {
pub trait Sealed {}
Expand Down

0 comments on commit 1e5b2cc

Please sign in to comment.