Skip to content

Commit

Permalink
logical milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Feb 18, 2024
1 parent 71cc8b3 commit 52a5e23
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 65 deletions.
62 changes: 5 additions & 57 deletions module/core/derive_tools/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {}
Expand All @@ -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 {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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![] );

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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() )
Expand Down

0 comments on commit 52a5e23

Please sign in to comment.