From 080cc42bb1da09059dbc35049a7ded0649961e0c Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 29 Aug 2022 20:52:56 +0200 Subject: feat: implement async functionality --- src/castable_factory/blocking.rs | 74 ++++++++++++++++++++++++++++++++ src/castable_factory/mod.rs | 2 + src/castable_factory/threadsafe.rs | 88 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 src/castable_factory/blocking.rs create mode 100644 src/castable_factory/mod.rs create mode 100644 src/castable_factory/threadsafe.rs (limited to 'src/castable_factory') diff --git a/src/castable_factory/blocking.rs b/src/castable_factory/blocking.rs new file mode 100644 index 0000000..5ff4db0 --- /dev/null +++ b/src/castable_factory/blocking.rs @@ -0,0 +1,74 @@ +use crate::interfaces::any_factory::AnyFactory; +use crate::interfaces::factory::IFactory; +use crate::ptr::TransientPtr; + +pub struct CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static dyn Fn>, +} + +impl CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static dyn Fn>, + ) -> Self + { + Self { func } + } +} + +impl IFactory + for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl Fn for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + extern "rust-call" fn call(&self, args: Args) -> Self::Output + { + self.func.call(args) + } +} + +impl FnMut for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output + { + self.call(args) + } +} + +impl FnOnce for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + type Output = TransientPtr; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output + { + self.call(args) + } +} + +impl AnyFactory for CastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} diff --git a/src/castable_factory/mod.rs b/src/castable_factory/mod.rs new file mode 100644 index 0000000..530cc82 --- /dev/null +++ b/src/castable_factory/mod.rs @@ -0,0 +1,2 @@ +pub mod blocking; +pub mod threadsafe; diff --git a/src/castable_factory/threadsafe.rs b/src/castable_factory/threadsafe.rs new file mode 100644 index 0000000..7be055c --- /dev/null +++ b/src/castable_factory/threadsafe.rs @@ -0,0 +1,88 @@ +#![allow(clippy::module_name_repetitions)] +use crate::interfaces::any_factory::{AnyFactory, AnyThreadsafeFactory}; +use crate::interfaces::factory::IFactory; +use crate::ptr::TransientPtr; + +pub struct ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static (dyn Fn> + Send + Sync), +} + +impl ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static (dyn Fn> + + Send + + Sync), + ) -> Self + { + Self { func } + } +} + +impl IFactory + for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl Fn for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + extern "rust-call" fn call(&self, args: Args) -> Self::Output + { + self.func.call(args) + } +} + +impl FnMut + for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output + { + self.call(args) + } +} + +impl FnOnce + for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + type Output = TransientPtr; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output + { + self.call(args) + } +} + +impl AnyFactory + for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl AnyThreadsafeFactory + for ThreadsafeCastableFactory +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} -- cgit v1.2.3-18-g5258