diff --git a/Cargo.toml b/Cargo.toml index 8f54f63..f3bda84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "re_set-lib" -version = "2.0.0" +version = "2.1.0" edition = "2021" description = "Data structure library for ReSet" repository = "https://github.com/Xetibo/ReSet-Lib" diff --git a/src/lib.rs b/src/lib.rs index 3cf4ecd..1df3673 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![feature(trait_upcasting)] +#![feature(unsized_fn_params)] use directories_next as dirs; use std::{fmt, fs, path::PathBuf}; diff --git a/src/utils/plugin.rs b/src/utils/plugin.rs index 7aa0572..925897c 100644 --- a/src/utils/plugin.rs +++ b/src/utils/plugin.rs @@ -1,7 +1,25 @@ use std::collections::HashMap; -use super::variant::TVariant; +use super::variant::Variant; -pub fn plugin_data() -> HashMap> { - HashMap::new() +pub fn plugin_data() -> PluginData { + PluginData(HashMap::new()) +} + +#[repr(C)] +pub struct PluginCapabilities(Vec); + +impl PluginCapabilities { + pub fn get_capabilities(&self) -> Vec { + self.0.clone() + } +} + +#[repr(C)] +pub struct PluginData(HashMap); + +impl PluginData { + pub fn get_data(&self) -> HashMap { + self.0.clone() + } } diff --git a/src/utils/variant.rs b/src/utils/variant.rs index bab580f..34b7e28 100644 --- a/src/utils/variant.rs +++ b/src/utils/variant.rs @@ -1,4 +1,8 @@ -use std::{any::Any, collections::HashMap, ops::Deref}; +use std::{ + any::{Any, TypeId}, + collections::HashMap, + ops::Deref, +}; pub trait Debug: std::fmt::Debug {} @@ -19,118 +23,163 @@ impl Debug for HashMap {} pub trait TVariant: Debug + Any + Send { fn into_mock_variant(self) -> Variant; + fn value(&self) -> Box; } impl TVariant for bool { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "bool") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for u8 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "u8") + Variant::new::(self) + } + + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for i8 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "i8") + Variant::new::(self) + } + + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for u16 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "u16") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for i16 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "i16") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for u32 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "u32") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for i32 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "i32") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for u64 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "u64") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for i64 { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "i64") + Variant::new::(self) + } + fn value(&self) -> Box { + Box::new(*self) } } impl TVariant for String { fn into_mock_variant(self) -> Variant { - Variant::new::(self.clone(), "String") + Variant::new::(self.clone()) + } + fn value(&self) -> Box { + Box::new(self.clone()) } } -impl TVariant for Option +impl TVariant for Option where - T: IntrospectType + Clone, + T: Clone, { fn into_mock_variant(self) -> Variant { - Variant::new(self, "Option ".to_string() + &T::get_type()) + Variant::new(self) + } + fn value(&self) -> Box { + Box::new(self.clone()) } } -impl TVariant for Vec +impl TVariant for Vec where - T: IntrospectType + Clone, + T: Clone, { fn into_mock_variant(self) -> Variant { - Variant::new(self, "Vec ".to_string() + &T::get_type()) + Variant::new(self) + } + fn value(&self) -> Box { + Box::new(self.clone()) } } -impl - TVariant for HashMap +impl TVariant for HashMap where - K: IntrospectType + Clone, - V: IntrospectType + Clone, + K: Clone, + V: Clone, { fn into_mock_variant(self) -> Variant { - Variant::new( - self, - "HashMap ".to_string() + &K::get_type() + " " + &V::get_type(), - ) + Variant::new(self) + } + fn value(&self) -> Box { + Box::new(self.clone()) } } #[derive(Debug)] pub struct Variant { value: Box, - kind: String, + kind: TypeId, +} + +impl Clone for Variant { + fn clone(&self) -> Self { + Self { + value: self.value.value(), + kind: self.kind, + } + } } impl Variant { - pub fn empty() -> Self { - Self::new::(Empty {}, "None") + pub fn empty() -> Variant { + Self::new::(Empty {}) } - pub fn new(value: T, kind: impl Into) -> Self { + pub fn new(value: T) -> Self { + let id = value.type_id(); Variant { value: Box::new(value), - kind: kind.into(), + kind: id, } } - pub fn to_value(&self, conversion_type: &'static str) -> Result { - if self.kind != conversion_type { + pub fn to_value(&self) -> Result { + if self.kind != TypeId::of::() { return Err(ConversionError("Conversion Failed")); } unsafe { Ok(*self.to_value_unchecked::()) } } - pub fn to_value_cloned( - &self, - conversion_type: &'static str, - ) -> Result<&T, ConversionError> { - if self.kind != conversion_type { + pub fn to_value_cloned(&self) -> Result<&T, ConversionError> { + if self.kind != TypeId::of::() { return Err(ConversionError("Conversion Failed")); } unsafe { Ok(self.to_value_unchecked::()) } @@ -147,123 +196,36 @@ pub struct ConversionError(pub &'static str); #[derive(Clone, Copy, Debug)] pub struct Empty {} -impl IntrospectType for Empty { - fn get_type() -> String { - "None".into() - } -} - impl TVariant for Empty { fn into_mock_variant(self) -> Variant { - Variant::new::(self, "None") - } -} - -pub trait IntrospectType { - fn get_type() -> String; -} - -impl IntrospectType for bool { - fn get_type() -> String { - "bool".into() - } -} - -impl IntrospectType for u8 { - fn get_type() -> String { - "u8".into() - } -} - -impl IntrospectType for i8 { - fn get_type() -> String { - "i8".into() - } -} - -impl IntrospectType for u16 { - fn get_type() -> String { - "u16".into() - } -} - -impl IntrospectType for i16 { - fn get_type() -> String { - "i16".into() + Variant::new::(self) } -} - -impl IntrospectType for u32 { - fn get_type() -> String { - "u32".into() - } -} - -impl IntrospectType for i32 { - fn get_type() -> String { - "i32".into() - } -} - -impl IntrospectType for u64 { - fn get_type() -> String { - "u64".into() - } -} -impl IntrospectType for i64 { - fn get_type() -> String { - "i64".into() - } -} - -impl IntrospectType for String { - fn get_type() -> String { - "String".into() - } -} - -impl IntrospectType for Option { - fn get_type() -> String { - "Option".to_string() + " " + &T::get_type() - } -} - -impl IntrospectType for Vec { - fn get_type() -> String { - "Vec".to_string() + " " + &T::get_type() - } -} - -impl IntrospectType for HashMap { - fn get_type() -> String { - "HashMap".to_string() + " " + &K::get_type() + " " + &V::get_type() + fn value(&self) -> Box { + todo!() } } #[test] fn test_i32() { let mock = 5.into_mock_variant(); - assert_eq!(mock.kind, "i32".to_string()); - assert_eq!(mock.to_value::("i32").unwrap(), 5); + assert_eq!(mock.kind, TypeId::of::()); + assert_eq!(mock.to_value::().unwrap(), 5); } #[test] fn test_option() { let mock = Some(10).into_mock_variant(); - assert_eq!(mock.kind, "Option i32".to_string()); - assert_eq!( - mock.to_value::>("Option i32").unwrap(), - Some(10) - ); + assert_eq!(mock.kind, TypeId::of::>()); + assert_eq!(mock.to_value::>().unwrap(), Some(10)); } #[test] fn test_vec() { let mock = vec![3, 2, 4, 5, 10].into_mock_variant(); - assert_eq!(mock.kind, "Vec i32".to_string()); + assert_eq!(mock.kind, TypeId::of::>()); assert_eq!( - mock.to_value_cloned::>("Vec i32").unwrap().clone(), + mock.to_value_cloned::>().unwrap().clone(), vec![3, 2, 4, 5, 10] ); } @@ -277,9 +239,9 @@ fn test_hashmap() { let mut testmap = HashMap::new(); testmap.insert("Something".to_string(), 20); - assert_eq!(mock.kind, "HashMap String i32".to_string()); + assert_eq!(mock.kind, TypeId::of::>()); assert_eq!( - mock.to_value_cloned::>("HashMap String i32") + mock.to_value_cloned::>() .unwrap() .clone(), testmap @@ -289,6 +251,25 @@ fn test_hashmap() { #[test] fn test_conversion_fail() { let mock = "hello".to_string().into_mock_variant(); - assert_eq!(mock.kind, "String".to_string()); - assert!(mock.to_value_cloned::("Not String").is_err()); + assert_eq!(mock.kind, TypeId::of::()); + assert!(mock.to_value_cloned::().is_err()); +} + +#[test] +fn test_variant_clone() { + let mut map = HashMap::new(); + map.insert("Something".to_string(), 20); + let mock = map.into_mock_variant(); + let new_mock = mock.clone(); + + let mut testmap = HashMap::new(); + testmap.insert("Something".to_string(), 20); + + assert_eq!( + new_mock + .to_value_cloned::>() + .unwrap() + .clone(), + testmap + ); }