From 0c8893513730a1b9d2deefad3d0e3af51f197b84 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Mon, 17 Jul 2023 20:43:23 +0200 Subject: [PATCH] Make GodotClass an unsafe trait and document usage --- godot-codegen/src/class_generator.rs | 2 +- godot-core/src/obj/traits.rs | 11 ++++++++--- godot-macros/src/derive_godot_class/mod.rs | 2 +- godot-macros/src/lib.rs | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/godot-codegen/src/class_generator.rs b/godot-codegen/src/class_generator.rs index 2fcd44ca2..60691ece6 100644 --- a/godot-codegen/src/class_generator.rs +++ b/godot-codegen/src/class_generator.rs @@ -562,7 +562,7 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate #methods #constants } - impl crate::obj::GodotClass for #class_name { + unsafe impl crate::obj::GodotClass for #class_name { type Base = #base_ty; type Declarer = crate::obj::dom::EngineDomain; type Mem = crate::obj::mem::#memory; diff --git a/godot-core/src/obj/traits.rs b/godot-core/src/obj/traits.rs index 01e9b1b2b..351774226 100644 --- a/godot-core/src/obj/traits.rs +++ b/godot-core/src/obj/traits.rs @@ -14,8 +14,11 @@ use godot_ffi as sys; /// /// The behavior of types implementing this trait is influenced by the associated types; check their documentation for information. /// -/// You wouldn't usually implement this trait yourself; use the [`GodotClass`](godot_macros::GodotClass) derive macro instead. -pub trait GodotClass: 'static +/// # Safety +/// +/// Internal. +/// **You must not implement this trait yourself; use the [`GodotClass`](crate::bind::GodotClass) derive macro instead.** +pub unsafe trait GodotClass: 'static where Self: Sized, { @@ -51,7 +54,7 @@ where } /// Unit impl only exists to represent "no base", and is used for exactly one class: `Object`. -impl GodotClass for () { +unsafe impl GodotClass for () { type Base = (); type Declarer = dom::EngineDomain; type Mem = mem::ManualMemory; @@ -286,6 +289,8 @@ pub mod mem { false } } + + #[doc(hidden)] pub trait PossiblyManual {} /// Memory managed through Godot reference counter (always present). diff --git a/godot-macros/src/derive_godot_class/mod.rs b/godot-macros/src/derive_godot_class/mod.rs index 0e2b439c2..44f33ec58 100644 --- a/godot-macros/src/derive_godot_class/mod.rs +++ b/godot-macros/src/derive_godot_class/mod.rs @@ -44,7 +44,7 @@ pub fn transform(decl: Declaration) -> ParseResult { }; Ok(quote! { - impl ::godot::obj::GodotClass for #class_name { + unsafe impl ::godot::obj::GodotClass for #class_name { type Base = ::godot::engine::#base_ty; type Declarer = ::godot::obj::dom::UserDomain; type Mem = ::Mem; diff --git a/godot-macros/src/lib.rs b/godot-macros/src/lib.rs index 41627a8e1..2cad132f3 100644 --- a/godot-macros/src/lib.rs +++ b/godot-macros/src/lib.rs @@ -96,8 +96,8 @@ mod itest; mod method_registration; mod util; -/// Derive macro for [`GodotClass`](godot_core::obj::GodotClass) on structs. You should normally use -/// this macro, rather than implement `GodotClass` manually for your type. +/// Derive macro for [`GodotClass`](godot_core::obj::GodotClass) on structs. You must use this +/// macro; manual implementations of the `GodotClass` traits are not supported. /// /// # Construction ///