diff options
Diffstat (limited to 'src/castable_factory')
-rw-r--r-- | src/castable_factory/blocking.rs | 74 | ||||
-rw-r--r-- | src/castable_factory/mod.rs | 2 | ||||
-rw-r--r-- | src/castable_factory/threadsafe.rs | 88 |
3 files changed, 164 insertions, 0 deletions
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<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static dyn Fn<Args, Output = TransientPtr<ReturnInterface>>, +} + +impl<Args, ReturnInterface> CastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static dyn Fn<Args, Output = TransientPtr<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 = TransientPtr<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, +{ +} 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<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + func: &'static (dyn Fn<Args, Output = TransientPtr<ReturnInterface>> + Send + Sync), +} + +impl<Args, ReturnInterface> ThreadsafeCastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + pub fn new( + func: &'static (dyn Fn<Args, Output = TransientPtr<ReturnInterface>> + + Send + + Sync), + ) -> Self + { + Self { func } + } +} + +impl<Args, ReturnInterface> IFactory<Args, ReturnInterface> + for ThreadsafeCastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl<Args, ReturnInterface> Fn<Args> for ThreadsafeCastableFactory<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 ThreadsafeCastableFactory<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 ThreadsafeCastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ + type Output = TransientPtr<ReturnInterface>; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output + { + self.call(args) + } +} + +impl<Args, ReturnInterface> AnyFactory + for ThreadsafeCastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} + +impl<Args, ReturnInterface> AnyThreadsafeFactory + for ThreadsafeCastableFactory<Args, ReturnInterface> +where + Args: 'static, + ReturnInterface: 'static + ?Sized, +{ +} |