Test generics and other things

This commit is contained in:
Werner
2022-05-22 13:41:48 -03:00
parent acb22d7ce6
commit ced258e5c5
16 changed files with 283 additions and 32 deletions

View File

@ -10,9 +10,12 @@ use syn::{parse_macro_input, DeriveInput, ItemFn};
pub fn reflect(input: TokenStream) -> TokenStream { pub fn reflect(input: TokenStream) -> TokenStream {
let class = parse_macro_input!(input as DeriveInput); let class = parse_macro_input!(input as DeriveInput);
let name = &class.ident; let name = &class.ident;
let generics = &class.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let expanded = quote! { let expanded = quote! {
unsafe impl Reflect for #name { unsafe impl #impl_generics Reflect for #name #ty_generics #where_clause {
fn TypeName(&self) -> &'static str { fn TypeName(&self) -> &'static str {
std::any::type_name::<Self>() std::any::type_name::<Self>()
} }

View File

@ -1,8 +1,12 @@
use crate::Property;
use std::any::{type_name, TypeId}; use std::any::{type_name, TypeId};
use std::borrow::Cow;
use std::collections::HashMap;
pub struct Class { pub struct Class {
id: TypeId, id: TypeId,
name: &'static str, name: Cow<'static, str>,
properties: HashMap<Cow<'static, str>, Property>,
} }
impl Class { impl Class {
@ -10,6 +14,7 @@ impl Class {
Self { Self {
id: TypeId::of::<T>(), id: TypeId::of::<T>(),
name: type_name::<T>().into(), name: type_name::<T>().into(),
properties: HashMap::new(),
} }
} }
@ -17,7 +22,7 @@ impl Class {
self.id self.id
} }
pub fn GetName(&self) -> &'static str { pub fn GetName(&self) -> &str {
self.name &self.name
} }
} }

38
Source/Examples/Array.rs Normal file
View File

@ -0,0 +1,38 @@
use crate::*;
#[derive(Reflect)]
pub struct Array<T>
where
T: Reflect,
{
inner: Vec<T>,
}
impl<T> Default for Array<T>
where
T: Reflect,
{
fn default() -> Self {
Self { inner: Vec::new() }
}
}
impl<T> Array<T>
where
T: Reflect,
{
#[function]
pub fn New() -> Self {
Self::default()
}
#[function]
pub fn Get(&mut self, index: usize) -> Option<&T> {
self.inner.get(index)
}
#[function]
pub fn GetMut(&mut self, index: usize) -> Option<&mut T> {
self.inner.get_mut(index)
}
}

View File

@ -0,0 +1,11 @@
use crate::*;
#[derive(Reflect)]
pub struct BinaryHeap<T>
where
T: Reflect,
{
inner: T,
}
impl<T> BinaryHeap<T> where T: Reflect {}

View File

@ -0,0 +1,11 @@
use crate::*;
#[derive(Reflect)]
pub struct BinaryTree<T>
where
T: Reflect,
{
inner: T,
}
impl<T> BinaryTree<T> where T: Reflect {}

View File

@ -0,0 +1,28 @@
use crate::*;
use std::collections::HashMap as InnerHashMap;
use std::hash::Hash;
#[derive(Reflect)]
pub struct HashMap<K, V>
where
K: Reflect + Eq + Hash,
V: Reflect,
{
inner: InnerHashMap<K, V>,
}
impl<K, V> HashMap<K, V>
where
K: Reflect + Eq + Hash,
V: Reflect,
{
#[function]
pub fn Insert(&mut self, key: K, value: V) -> Option<V> {
self.inner.insert(key, value)
}
#[function]
pub fn Remove(&mut self, key: &K) -> Option<V> {
self.inner.remove(key)
}
}

View File

@ -0,0 +1,12 @@
use crate::*;
use std::collections::HashSet as InnerHashSet;
#[derive(Reflect)]
pub struct HashSet<T>
where
T: Reflect,
{
inner: InnerHashSet<T>,
}
impl<T> HashSet<T> where T: Reflect {}

View File

@ -0,0 +1,11 @@
use crate::*;
#[derive(Reflect)]
pub struct LinkedList<T>
where
T: Reflect,
{
inner: T,
}
impl<T> LinkedList<T> where T: Reflect {}

11
Source/Examples/Queue.rs Normal file
View File

@ -0,0 +1,11 @@
use crate::*;
#[derive(Reflect)]
pub struct Queue<T>
where
T: Reflect,
{
inner: T,
}
impl<T> Queue<T> where T: Reflect {}

48
Source/Examples/Stack.rs Normal file
View File

@ -0,0 +1,48 @@
use crate::*;
#[derive(Reflect)]
pub struct Stack<T>
where
T: Reflect,
{
inner: Vec<T>,
}
impl<T> Default for Stack<T>
where
T: Reflect,
{
fn default() -> Self {
Self { inner: Vec::new() }
}
}
impl<T> Stack<T>
where
T: Reflect,
{
#[function]
pub fn New() -> Self {
Self::default()
}
#[function]
pub fn Push(&mut self, element: T) {
self.inner.push(element);
}
#[function]
pub fn Peek(&mut self) -> Option<&T> {
self.inner.last()
}
#[function]
pub fn PeekMut(&mut self) -> Option<&mut T> {
self.inner.last_mut()
}
#[function]
pub fn Pop(&mut self) -> Option<T> {
self.inner.pop()
}
}

View File

@ -1,32 +1,31 @@
use crate::*; #[path = "Array.rs"]
mod _Array;
pub use self::_Array::Array;
#[derive(Reflect)] #[path = "BinaryTree.rs"]
pub struct Foo { mod _BinaryTree;
#[property(visible, editable, category = "Default")] pub use self::_BinaryTree::BinaryTree;
pub a: u32,
#[property(visible, editable, readwrite, category = "Default")] #[path = "BinaryHeap.rs"]
pub b: Bar, mod _BinaryHeap;
pub use self::_BinaryHeap::BinaryHeap;
#[property(visible, readonly, category = "Default")] #[path = "HashMap.rs"]
pub c: Vec<u128>, mod _HashMap;
pub use self::_HashMap::HashMap;
#[property(hidden)] #[path = "HashSet.rs"]
pub d: Vec<Bar>, mod _HashSet;
} pub use self::_HashSet::HashSet;
impl Foo { #[path = "LinkedList.rs"]
#[function(callable, multicast, category = "Default")] mod _LinkedList;
pub fn Func(&mut self) {} pub use self::_LinkedList::LinkedList;
}
#[derive(Reflect)] #[path = "Queue.rs"]
pub struct Bar { mod _Queue;
#[property(visible, editable, category = "Default")] pub use self::_Queue::Queue;
pub value: f32,
}
impl Bar { #[path = "Stack.rs"]
#[function(event, server, reliable, category = "Default")] mod _Stack;
pub fn Func(&mut self) {} pub use self::_Stack::Stack;
}

4
Source/Function.rs Normal file
View File

@ -0,0 +1,4 @@
pub trait Function<Args = ()>: Send + Sync + 'static {
type Result;
fn Invoke(&self, args: Args) -> Self::Result;
}

View File

@ -2,22 +2,23 @@ use crate::{Class, Reflect};
use anyhow::Result; use anyhow::Result;
use std::sync::Arc; use std::sync::Arc;
#[derive(Reflect, Clone)]
pub struct Object { pub struct Object {
inner: Arc<dyn Reflect>, inner: Arc<dyn Reflect>,
} }
impl Object { impl Object {
fn New(obj: impl Reflect) -> Self { pub fn New(obj: impl Reflect) -> Self {
Self { Self {
inner: Arc::new(obj), inner: Arc::new(obj),
} }
} }
fn InstanceOf(&self, class: &Class) -> bool { pub fn InstanceOf(&self, class: &Class) -> bool {
self.inner.as_ref().type_id() == class.GetId() self.inner.as_ref().type_id() == class.GetId()
} }
fn GetClass(&self) -> Result<()> { pub fn GetClass(&self) -> Result<()> {
Ok(()) Ok(())
} }
@ -25,3 +26,34 @@ impl Object {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod Test {
use super::*;
use crate::{Class, Reflect};
struct Foo {}
struct Bar {}
unsafe impl Reflect for Foo {
fn TypeName(&self) -> &'static str {
std::any::type_name::<Self>()
}
}
unsafe impl Reflect for Bar {
fn TypeName(&self) -> &'static str {
std::any::type_name::<Self>()
}
}
#[test]
fn InstanceOf() {
let foo_class: Class = Class::New::<Foo>();
let bar_class: Class = Class::New::<Bar>();
let foo_instance: Object = Object::New(Foo {});
assert!(foo_instance.InstanceOf(&foo_class));
assert!(!foo_instance.InstanceOf(&bar_class));
}
}

5
Source/Property.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::{Object, Value};
use anyhow::Result;
use std::sync::Arc;
pub struct Property(Arc<dyn Fn(&Object) -> Result<Value>>);

21
Source/Value.rs Normal file
View File

@ -0,0 +1,21 @@
use crate::Object;
#[derive(Clone)]
pub enum Value {
Bool(bool),
Char(char),
Int8(i8),
Int16(i16),
Int32(i32),
Int64(i64),
Int128(i128),
UInt8(u8),
UInt16(u16),
UInt32(u32),
UInt64(u64),
UInt128(u128),
Float32(f32),
Float64(f64),
String(String),
Object(Object),
}

View File

@ -9,10 +9,22 @@ pub mod Examples;
mod _Class; mod _Class;
pub use self::_Class::*; pub use self::_Class::*;
#[path = "Function.rs"]
mod _Function;
pub use self::_Function::*;
#[path = "Object.rs"] #[path = "Object.rs"]
mod _Object; mod _Object;
pub use self::_Object::*; pub use self::_Object::*;
#[path = "Property.rs"]
mod _Property;
pub use self::_Property::*;
#[path = "Reflect.rs"] #[path = "Reflect.rs"]
mod _Reflect; mod _Reflect;
pub use self::_Reflect::*; pub use self::_Reflect::*;
#[path = "Value.rs"]
mod _Value;
pub use self::_Value::*;