From b8e77e54c874c6f47e574d554e04fa8bd8939b54 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 6 Apr 2026 20:55:20 +0200 Subject: feat(engine): make Reflection trait unsafe --- engine-macros/src/lib.rs | 2 +- engine/src/reflection.rs | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/engine-macros/src/lib.rs b/engine-macros/src/lib.rs index b7bbd43..75ff209 100644 --- a/engine-macros/src/lib.rs +++ b/engine-macros/src/lib.rs @@ -106,7 +106,7 @@ pub fn reflection_derive(input: TokenStream) -> TokenStream }); quote! { - impl #impl_generics #engine_crate_path::reflection::Reflection for + unsafe impl #impl_generics #engine_crate_path::reflection::Reflection for #input_ident #type_generics #where_clause { const TYPE_REFLECTION: &#engine_crate_path::reflection::Type = diff --git a/engine/src/reflection.rs b/engine/src/reflection.rs index 13384fe..83bc636 100644 --- a/engine/src/reflection.rs +++ b/engine/src/reflection.rs @@ -4,7 +4,13 @@ use std::fmt::Debug; pub use engine_macros::Reflection; -pub trait Reflection: 'static +/// Trait implemented by types that support runtime reflection on them. +/// +/// # Safety +/// Implementors of this trait must provide accurate reflection information in the +/// `TYPE_REFLECTION` associated constant and the `type_reflection` and +/// `get_type_reflection` methods. +pub unsafe trait Reflection: 'static { const TYPE_REFLECTION: &Type; @@ -92,7 +98,7 @@ pub struct Literal macro_rules! impl_with_for_literals { ($($literal: ty),*) => { $( - impl Reflection for $literal + unsafe impl Reflection for $literal { const TYPE_REFLECTION: &Type = &Type::Literal(Literal { layout: Layout::new::<$literal>(), @@ -122,7 +128,7 @@ impl_with_for_literals!( &'static str ); -impl Reflection for [T; LEN] +unsafe impl Reflection for [T; LEN] { const TYPE_REFLECTION: &Type = &Type::Array(Array { item_reflection: T::TYPE_REFLECTION, @@ -130,7 +136,7 @@ impl Reflection for [T; LEN] }); } -impl Reflection for &'static [T] +unsafe impl Reflection for &'static [T] { const TYPE_REFLECTION: &Type = &Type::Slice(Slice { item_reflection: T::TYPE_REFLECTION }); -- cgit v1.2.3-18-g5258