From 52a5e23aa13e095889d079d1cd689c02c56cc6fa Mon Sep 17 00:00:00 2001 From: wandalen Date: Mon, 19 Feb 2024 00:05:08 +0200 Subject: [PATCH] logical milestone --- module/core/derive_tools/src/reflect.rs | 62 ++----------------- .../only_test/reflect_struct_with_lifetime.rs | 24 +++++-- ...eflect_struct_with_lifetime_manual_test.rs | 6 +- 3 files changed, 27 insertions(+), 65 deletions(-) diff --git a/module/core/derive_tools/src/reflect.rs b/module/core/derive_tools/src/reflect.rs index 0d86558595..5c264bc7b0 100644 --- a/module/core/derive_tools/src/reflect.rs +++ b/module/core/derive_tools/src/reflect.rs @@ -183,7 +183,7 @@ pub( crate ) mod private impl< T > Entity for EntityDescriptor< T > where - T : InstanceMarker + Default + 'static, + T : InstanceMarker + 'static, { #[ inline( always ) ] fn type_name( &self ) -> &'static str @@ -320,62 +320,6 @@ pub( crate ) mod private } } - // impl Instance for i8 {} - // impl Instance for i16 {} - // impl Instance for i32 {} - // impl Instance for i64 {} - // impl Instance for u8 {} - // impl Instance for u16 {} - // impl Instance for u32 {} - // impl Instance for u64 {} - // impl Instance for f32 {} - // impl Instance for f64 {} - // impl Instance for String {} - // impl Instance for &'static str {} - - /// Implements Entity for a types. - #[ macro_export ] - macro_rules! impl_entity_for - { - - ( - $( $Path : tt )* - ) - => - { - impl crate::reflect::Entity for crate::reflect::EntityDescriptor< $( $Path )* > - { - #[ inline( always ) ] - fn type_name( &self ) -> &'static str - { - core::any::type_name::< $( $Path )* >() - } - } - }; - - } - - // impl Entity for EntityDescriptor< i8 > - // { - // fn type_name( &self ) -> &'static str - // { - // core::any::type_name::< i8 >() - // } - // } - - // impl_entity_for!( i8 ); - // impl_entity_for!( i16 ); - // impl_entity_for!( i32 ); - // impl_entity_for!( i64 ); - // impl_entity_for!( u8 ); - // impl_entity_for!( u16 ); - // impl_entity_for!( u32 ); - // impl_entity_for!( u64 ); - // impl_entity_for!( f32 ); - // impl_entity_for!( f64 ); - // impl_entity_for!( String ); - // impl_entity_for!( &'static str ); - impl InstanceMarker for i8 {} impl InstanceMarker for i16 {} impl InstanceMarker for i32 {} @@ -389,6 +333,10 @@ pub( crate ) mod private impl InstanceMarker for String {} impl InstanceMarker for &'static str {} + impl< T > InstanceMarker for &T + where T : InstanceMarker + {} + impl IsScalar for i8 {} impl IsScalar for i16 {} impl IsScalar for i32 {} diff --git a/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs b/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs index 4186b6a3f3..d4814182f2 100644 --- a/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs +++ b/module/core/derive_tools/tests/inc/only_test/reflect_struct_with_lifetime.rs @@ -12,22 +12,36 @@ fn reflect_struct_with_lifetime() let ins = Struct1 { f1 : &x, - f2 : "2".into(), + f2 : 2, f3 : &z, }; + // for understanding + println!( "TypeId< i32 > : {:?}", core::any::TypeId::of::< i32 >() ); + println!( "TypeId< &i32 > : {:?}", core::any::TypeId::of::< & i32 >() ); // qqq : qqq fro Yuliia : problem. should be distinct id + println!( "TypeId< String > : {:?}", core::any::TypeId::of::< String >() ); + println!( "TypeId< &String > : {:?}", core::any::TypeId::of::< & String >() ); + println!( "TypeId< str > : {:?}", core::any::TypeId::of::< str >() ); + println!( "TypeId< &str > : {:?}", core::any::TypeId::of::< & str >() ); + println!( "i32 : {:?}", 1i32.reflect().type_id() ); println!( "&i32 : {:?}", ( &1i32 ).reflect().type_id() ); println!( "String : {:?}", "abc".to_string().reflect().type_id() ); println!( "&String : {:?}", ( &"abc".to_string() ).reflect().type_id() ); println!( "str : {:?}", "abc".reflect().type_id() ); println!( "&str : {:?}", ( &"abc" ).reflect().type_id() ); - println!( "Struct1 : {:?}", ins.reflect().type_id() ); println!( "Struct1.f1 : {:?}", ins.reflect().elements().next().unwrap().val.type_id() ); println!( "Struct1.f2 : {:?}", ins.reflect().elements().skip( 1 ).next().unwrap().val.type_id() ); println!( "Struct1.f3 : {:?}", ins.reflect().elements().skip( 2 ).next().unwrap().val.type_id() ); + println!( "i32.type_id : {:?}", 1i32.reflect().type_id() ); + println!( "i32.type_name : {:?}", 1i32.reflect().type_name() ); + println!( "&i32.type_id : {:?}", ( &1i32 ).reflect().type_id() ); + println!( "&i32.type_name : {:?}", ( &1i32 ).reflect().type_name() ); + println!( "&i32.type_id : {:?}", reflect::Instance::reflect( &1i32 ).type_id() ); + println!( "&i32.type_name : {:?}", reflect::Instance::reflect( &1i32 ).type_name() ); + // inspection of structure a_id!( ins.reflect().is_container(), true ); a_id!( ins.reflect().len(), 3 ); @@ -36,15 +50,15 @@ fn reflect_struct_with_lifetime() let names = ins.reflect().elements().map( | e | e.key ).collect::< Vec< _ > >(); a_id!( names, vec![ reflect::Primitive::str( "f1" ), reflect::Primitive::str( "f2" ), reflect::Primitive::str( "f3" ) ] ); let types = ins.reflect().elements().map( | e | e.val.type_name() ).collect::< Vec< _ > >(); - a_id!( types, vec![ "i32", "alloc::string::String", "&str" ] ); + a_id!( types, vec![ "&i32", "i32", "&str" ] ); // inspection of a field let f1 = ins.reflect().elements().next().unwrap(); a_id!( f1.key, reflect::Primitive::str( "f1" ) ); a_id!( f1.val.is_container(), false ); a_id!( f1.val.len(), 0 ); - a_id!( f1.val.type_name(), "i32" ); - a_id!( f1.val.type_id(), core::any::TypeId::of::< &'static str >() ); + a_id!( f1.val.type_name(), "&i32" ); + a_id!( f1.val.type_id(), core::any::TypeId::of::< &'static i32 >() ); a_id!( f1.val.elements().collect::< Vec< _ > >(), vec![] ); } diff --git a/module/core/derive_tools/tests/inc/reflect_struct_with_lifetime_manual_test.rs b/module/core/derive_tools/tests/inc/reflect_struct_with_lifetime_manual_test.rs index a490b39e3e..220bdf0b26 100644 --- a/module/core/derive_tools/tests/inc/reflect_struct_with_lifetime_manual_test.rs +++ b/module/core/derive_tools/tests/inc/reflect_struct_with_lifetime_manual_test.rs @@ -5,7 +5,7 @@ pub use TheModule::reflect; pub struct Struct1< 'a, 'b > { pub f1 : &'a i32, - pub f2 : String, + pub f2 : i32, pub f3 : &'b str, } @@ -73,8 +73,8 @@ impl< 'a, 'b > reflect::Entity for EntityDescriptor< 'a, 'b, Struct1< 'a, 'b > > { let result = vec! [ - reflect::KeyVal { key : reflect::Primitive::str( "f1" ), val : Box::new( < i32 as reflect::Instance >::Reflect() ) }, - reflect::KeyVal { key : reflect::Primitive::str( "f2" ), val : Box::new( < String as reflect::Instance >::Reflect() ) }, + reflect::KeyVal { key : reflect::Primitive::str( "f1" ), val : Box::new( < &'static i32 as reflect::Instance >::Reflect() ) }, + reflect::KeyVal { key : reflect::Primitive::str( "f2" ), val : Box::new( < i32 as reflect::Instance >::Reflect() ) }, reflect::KeyVal { key : reflect::Primitive::str( "f3" ), val : Box::new( < &'static str as reflect::Instance >::Reflect() ) }, ]; Box::new( result.into_iter() )