mirror of
https://github.com/guilhermewerner/reflection
synced 2025-06-15 13:14:19 +00:00
Create initial reflection system
This commit is contained in:
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
[build]
|
||||||
|
target-dir = "Binaries"
|
20
Cargo.toml
Normal file
20
Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
[package]
|
||||||
|
name = "reflection"
|
||||||
|
version = "0.0.1"
|
||||||
|
description = "Reflection"
|
||||||
|
repository = "https://github.com/GuilhermeWerner/Reflection"
|
||||||
|
license = "MIT"
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "Reflection"
|
||||||
|
crate-type = ["rlib"]
|
||||||
|
path = "Source/lib.rs"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = ["Macros"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
reflection-macros = { path = "Macros" }
|
||||||
|
anyhow = "1.0"
|
18
Macros/Cargo.toml
Normal file
18
Macros/Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "reflection-macros"
|
||||||
|
version = "0.0.1"
|
||||||
|
description = "Reflection"
|
||||||
|
repository = "https://github.com/GuilhermeWerner/Reflection"
|
||||||
|
license = "MIT"
|
||||||
|
edition = "2021"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "Reflection_Macros"
|
||||||
|
proc-macro = true
|
||||||
|
path = "lib.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
proc-macro2 = "1.0.24"
|
||||||
|
quote = "1.0.7"
|
||||||
|
syn = { version = "1.0.48", features = ["full"] }
|
24
Macros/lib.rs
Normal file
24
Macros/lib.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
#![allow(unused_variables)]
|
||||||
|
#![recursion_limit = "256"]
|
||||||
|
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
|
|
||||||
|
#[proc_macro_derive(Reflect, attributes(property))]
|
||||||
|
pub fn reflect(input: TokenStream) -> TokenStream {
|
||||||
|
let ast = parse_macro_input!(input as DeriveInput);
|
||||||
|
let name = &ast.ident;
|
||||||
|
|
||||||
|
let expanded = quote! {
|
||||||
|
impl Reflect for #name {}
|
||||||
|
};
|
||||||
|
|
||||||
|
expanded.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_attribute]
|
||||||
|
pub fn function(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
|
item
|
||||||
|
}
|
23
Source/Class.rs
Normal file
23
Source/Class.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use std::any::{type_name, TypeId};
|
||||||
|
|
||||||
|
pub struct Class {
|
||||||
|
id: TypeId,
|
||||||
|
name: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Class {
|
||||||
|
pub fn New<T: 'static>() -> Self {
|
||||||
|
Self {
|
||||||
|
id: TypeId::of::<T>(),
|
||||||
|
name: type_name::<T>().into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetId(&self) -> TypeId {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetName(&self) -> &'static str {
|
||||||
|
self.name
|
||||||
|
}
|
||||||
|
}
|
32
Source/Examples/mod.rs
Normal file
32
Source/Examples/mod.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[derive(Reflect)]
|
||||||
|
pub struct Foo {
|
||||||
|
#[property(visible, editable, category = "Default")]
|
||||||
|
pub a: u32,
|
||||||
|
|
||||||
|
#[property(visible, editable, category = "Default")]
|
||||||
|
pub b: Bar,
|
||||||
|
|
||||||
|
#[property(visible, editable, category = "Default")]
|
||||||
|
pub c: Vec<u128>,
|
||||||
|
|
||||||
|
#[property(visible, editable, category = "Default")]
|
||||||
|
pub d: Vec<Bar>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Foo {
|
||||||
|
#[function(callable, category = "Default")]
|
||||||
|
pub fn Func(&mut self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Reflect)]
|
||||||
|
pub struct Bar {
|
||||||
|
#[property(visible, editable, category = "Default")]
|
||||||
|
pub value: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bar {
|
||||||
|
#[function(callable, category = "Default")]
|
||||||
|
pub fn Func(&mut self) {}
|
||||||
|
}
|
27
Source/Object.rs
Normal file
27
Source/Object.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
use crate::{Class, Reflect};
|
||||||
|
use anyhow::Result;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct Object {
|
||||||
|
inner: Arc<dyn Reflect>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Object {
|
||||||
|
fn New(obj: impl Reflect) -> Self {
|
||||||
|
Self {
|
||||||
|
inner: Arc::new(obj),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn InstanceOf(&self, class: &Class) -> bool {
|
||||||
|
self.inner.as_ref().type_id() == class.GetId()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn GetClass(&self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn GetProperty(&self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
3
Source/Reflect.rs
Normal file
3
Source/Reflect.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
use std::any::Any;
|
||||||
|
|
||||||
|
pub trait Reflect: Any + Send + Sync + 'static {}
|
7
Source/TypeRegistry.rs
Normal file
7
Source/TypeRegistry.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
use std::any::TypeId;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct TypeRegistry {
|
||||||
|
types: HashMap<TypeId, ()>,
|
||||||
|
}
|
18
Source/lib.rs
Normal file
18
Source/lib.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
pub use Reflection_Macros::*;
|
||||||
|
|
||||||
|
pub mod Examples;
|
||||||
|
|
||||||
|
#[path = "Class.rs"]
|
||||||
|
mod _Class;
|
||||||
|
pub use self::_Class::*;
|
||||||
|
|
||||||
|
#[path = "Object.rs"]
|
||||||
|
mod _Object;
|
||||||
|
pub use self::_Object::*;
|
||||||
|
|
||||||
|
#[path = "Reflect.rs"]
|
||||||
|
mod _Reflect;
|
||||||
|
pub use self::_Reflect::*;
|
Reference in New Issue
Block a user