mirror of
https://github.com/guilhermewerner/reflection
synced 2025-06-15 21:24:19 +00:00
Test generics and other things
This commit is contained in:
@ -1,8 +1,12 @@
|
||||
use crate::Property;
|
||||
use std::any::{type_name, TypeId};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Class {
|
||||
id: TypeId,
|
||||
name: &'static str,
|
||||
name: Cow<'static, str>,
|
||||
properties: HashMap<Cow<'static, str>, Property>,
|
||||
}
|
||||
|
||||
impl Class {
|
||||
@ -10,6 +14,7 @@ impl Class {
|
||||
Self {
|
||||
id: TypeId::of::<T>(),
|
||||
name: type_name::<T>().into(),
|
||||
properties: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +22,7 @@ impl Class {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn GetName(&self) -> &'static str {
|
||||
self.name
|
||||
pub fn GetName(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
38
Source/Examples/Array.rs
Normal file
38
Source/Examples/Array.rs
Normal 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)
|
||||
}
|
||||
}
|
11
Source/Examples/BinaryHeap.rs
Normal file
11
Source/Examples/BinaryHeap.rs
Normal 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 {}
|
11
Source/Examples/BinaryTree.rs
Normal file
11
Source/Examples/BinaryTree.rs
Normal 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 {}
|
28
Source/Examples/HashMap.rs
Normal file
28
Source/Examples/HashMap.rs
Normal 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)
|
||||
}
|
||||
}
|
12
Source/Examples/HashSet.rs
Normal file
12
Source/Examples/HashSet.rs
Normal 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 {}
|
11
Source/Examples/LinkedList.rs
Normal file
11
Source/Examples/LinkedList.rs
Normal 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
11
Source/Examples/Queue.rs
Normal 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
48
Source/Examples/Stack.rs
Normal 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()
|
||||
}
|
||||
}
|
@ -1,32 +1,31 @@
|
||||
use crate::*;
|
||||
#[path = "Array.rs"]
|
||||
mod _Array;
|
||||
pub use self::_Array::Array;
|
||||
|
||||
#[derive(Reflect)]
|
||||
pub struct Foo {
|
||||
#[property(visible, editable, category = "Default")]
|
||||
pub a: u32,
|
||||
#[path = "BinaryTree.rs"]
|
||||
mod _BinaryTree;
|
||||
pub use self::_BinaryTree::BinaryTree;
|
||||
|
||||
#[property(visible, editable, readwrite, category = "Default")]
|
||||
pub b: Bar,
|
||||
#[path = "BinaryHeap.rs"]
|
||||
mod _BinaryHeap;
|
||||
pub use self::_BinaryHeap::BinaryHeap;
|
||||
|
||||
#[property(visible, readonly, category = "Default")]
|
||||
pub c: Vec<u128>,
|
||||
#[path = "HashMap.rs"]
|
||||
mod _HashMap;
|
||||
pub use self::_HashMap::HashMap;
|
||||
|
||||
#[property(hidden)]
|
||||
pub d: Vec<Bar>,
|
||||
}
|
||||
#[path = "HashSet.rs"]
|
||||
mod _HashSet;
|
||||
pub use self::_HashSet::HashSet;
|
||||
|
||||
impl Foo {
|
||||
#[function(callable, multicast, category = "Default")]
|
||||
pub fn Func(&mut self) {}
|
||||
}
|
||||
#[path = "LinkedList.rs"]
|
||||
mod _LinkedList;
|
||||
pub use self::_LinkedList::LinkedList;
|
||||
|
||||
#[derive(Reflect)]
|
||||
pub struct Bar {
|
||||
#[property(visible, editable, category = "Default")]
|
||||
pub value: f32,
|
||||
}
|
||||
#[path = "Queue.rs"]
|
||||
mod _Queue;
|
||||
pub use self::_Queue::Queue;
|
||||
|
||||
impl Bar {
|
||||
#[function(event, server, reliable, category = "Default")]
|
||||
pub fn Func(&mut self) {}
|
||||
}
|
||||
#[path = "Stack.rs"]
|
||||
mod _Stack;
|
||||
pub use self::_Stack::Stack;
|
||||
|
4
Source/Function.rs
Normal file
4
Source/Function.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub trait Function<Args = ()>: Send + Sync + 'static {
|
||||
type Result;
|
||||
fn Invoke(&self, args: Args) -> Self::Result;
|
||||
}
|
@ -2,22 +2,23 @@ use crate::{Class, Reflect};
|
||||
use anyhow::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Reflect, Clone)]
|
||||
pub struct Object {
|
||||
inner: Arc<dyn Reflect>,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
fn New(obj: impl Reflect) -> Self {
|
||||
pub fn New(obj: impl Reflect) -> Self {
|
||||
Self {
|
||||
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()
|
||||
}
|
||||
|
||||
fn GetClass(&self) -> Result<()> {
|
||||
pub fn GetClass(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -25,3 +26,34 @@ impl Object {
|
||||
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
5
Source/Property.rs
Normal 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
21
Source/Value.rs
Normal 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),
|
||||
}
|
@ -9,10 +9,22 @@ pub mod Examples;
|
||||
mod _Class;
|
||||
pub use self::_Class::*;
|
||||
|
||||
#[path = "Function.rs"]
|
||||
mod _Function;
|
||||
pub use self::_Function::*;
|
||||
|
||||
#[path = "Object.rs"]
|
||||
mod _Object;
|
||||
pub use self::_Object::*;
|
||||
|
||||
#[path = "Property.rs"]
|
||||
mod _Property;
|
||||
pub use self::_Property::*;
|
||||
|
||||
#[path = "Reflect.rs"]
|
||||
mod _Reflect;
|
||||
pub use self::_Reflect::*;
|
||||
|
||||
#[path = "Value.rs"]
|
||||
mod _Value;
|
||||
pub use self::_Value::*;
|
||||
|
Reference in New Issue
Block a user