aboutsummaryrefslogtreecommitdiff
path: root/src/castable_factory.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-20 14:29:45 +0200
committerHampusM <hampus@hampusmat.com>2022-07-20 14:29:45 +0200
commit2d1a6b2d432408d74eb57e0bda3f7434617e1070 (patch)
tree7e21f8126edfdfd9c40b4b51ba5626c6440442d9 /src/castable_factory.rs
parent7863d9859a5cbce99c3769e4fdb40283115d358d (diff)
refactor: reorganize folder hierarchy
Diffstat (limited to 'src/castable_factory.rs')
-rw-r--r--src/castable_factory.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/castable_factory.rs b/src/castable_factory.rs
new file mode 100644
index 0000000..5d582bb
--- /dev/null
+++ b/src/castable_factory.rs
@@ -0,0 +1,76 @@
+use crate::interfaces::factory::IFactory;
+use crate::libs::intertrait::CastFrom;
+use crate::ptr::InterfacePtr;
+
+pub trait AnyFactory: CastFrom {}
+
+pub struct CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+ func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>,
+}
+
+impl<Args, ReturnInterface> CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+ pub fn new(
+ func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>,
+ ) -> Self
+ {
+ Self { func }
+ }
+}
+
+impl<Args, ReturnInterface> IFactory<Args, ReturnInterface>
+ for CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+}
+
+impl<Args, ReturnInterface> Fn<Args> for CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+ extern "rust-call" fn call(&self, args: Args) -> Self::Output
+ {
+ self.func.call(args)
+ }
+}
+
+impl<Args, ReturnInterface> FnMut<Args> for CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+ extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output
+ {
+ self.call(args)
+ }
+}
+
+impl<Args, ReturnInterface> FnOnce<Args> for CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+ type Output = InterfacePtr<ReturnInterface>;
+
+ extern "rust-call" fn call_once(self, args: Args) -> Self::Output
+ {
+ self.call(args)
+ }
+}
+
+impl<Args, ReturnInterface> AnyFactory for CastableFactory<Args, ReturnInterface>
+where
+ Args: 'static,
+ ReturnInterface: 'static + ?Sized,
+{
+}