aboutsummaryrefslogtreecommitdiff
path: root/src/ptr.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-29 20:52:56 +0200
committerHampusM <hampus@hampusmat.com>2022-08-29 21:01:32 +0200
commit080cc42bb1da09059dbc35049a7ded0649961e0c (patch)
tree307ee564124373616022c1ba2b4d5af80845cd92 /src/ptr.rs
parent6e31d8f9e46fece348f329763b39b9c6f2741c07 (diff)
feat: implement async functionality
Diffstat (limited to 'src/ptr.rs')
-rw-r--r--src/ptr.rs89
1 files changed, 63 insertions, 26 deletions
diff --git a/src/ptr.rs b/src/ptr.rs
index 44fc15c..33f8a95 100644
--- a/src/ptr.rs
+++ b/src/ptr.rs
@@ -2,10 +2,11 @@
//! Smart pointer type aliases.
use std::rc::Rc;
+use std::sync::Arc;
use paste::paste;
-use crate::errors::ptr::SomePtrError;
+use crate::errors::ptr::{SomePtrError, SomeThreadsafePtrError};
/// A smart pointer for a interface in the transient scope.
pub type TransientPtr<Interface> = Box<Interface>;
@@ -13,44 +14,34 @@ pub type TransientPtr<Interface> = Box<Interface>;
/// A smart pointer to a interface in the singleton scope.
pub type SingletonPtr<Interface> = Rc<Interface>;
+/// A threadsafe smart pointer to a interface in the singleton scope.
+pub type ThreadsafeSingletonPtr<Interface> = Arc<Interface>;
+
/// A smart pointer to a factory.
#[cfg(feature = "factory")]
pub type FactoryPtr<FactoryInterface> = Rc<FactoryInterface>;
-/// Some smart pointer.
-#[derive(strum_macros::IntoStaticStr)]
-pub enum SomePtr<Interface>
-where
- Interface: 'static + ?Sized,
-{
- /// A smart pointer to a interface in the transient scope.
- Transient(TransientPtr<Interface>),
-
- /// A smart pointer to a interface in the singleton scope.
- Singleton(SingletonPtr<Interface>),
-
- /// A smart pointer to a factory.
- #[cfg(feature = "factory")]
- Factory(FactoryPtr<Interface>),
-}
+/// A threadsafe smart pointer to a factory.
+#[cfg(feature = "factory")]
+pub type ThreadsafeFactoryPtr<FactoryInterface> = Arc<FactoryInterface>;
macro_rules! create_as_variant_fn {
- ($variant: ident) => {
+ ($enum: ident, $variant: ident) => {
paste! {
#[doc =
- "Returns as " [<$variant:lower>] ".\n"
+ "Returns as the `" [<$variant>] "` variant.\n"
"\n"
"# Errors\n"
- "Will return Err if it's not a " [<$variant:lower>] "."
+ "Will return Err if it's not the `" [<$variant>] "` variant."
]
- pub fn [<$variant:lower>](self) -> Result<[<$variant Ptr>]<Interface>, SomePtrError>
+ pub fn [<$variant:snake>](self) -> Result<[<$variant Ptr>]<Interface>, [<$enum Error>]>
{
- if let SomePtr::$variant(ptr) = self {
+ if let $enum::$variant(ptr) = self {
return Ok(ptr);
}
- Err(SomePtrError::WrongPtrType {
+ Err([<$enum Error>]::WrongPtrType {
expected: stringify!($variant),
found: self.into()
})
@@ -59,14 +50,60 @@ macro_rules! create_as_variant_fn {
};
}
+/// Some smart pointer.
+#[derive(strum_macros::IntoStaticStr)]
+pub enum SomePtr<Interface>
+where
+ Interface: 'static + ?Sized,
+{
+ /// A smart pointer to a interface in the transient scope.
+ Transient(TransientPtr<Interface>),
+
+ /// A smart pointer to a interface in the singleton scope.
+ Singleton(SingletonPtr<Interface>),
+
+ /// A smart pointer to a factory.
+ #[cfg(feature = "factory")]
+ Factory(FactoryPtr<Interface>),
+}
+
impl<Interface> SomePtr<Interface>
where
Interface: 'static + ?Sized,
{
- create_as_variant_fn!(Transient);
+ create_as_variant_fn!(SomePtr, Transient);
+
+ create_as_variant_fn!(SomePtr, Singleton);
+
+ #[cfg(feature = "factory")]
+ create_as_variant_fn!(SomePtr, Factory);
+}
+
+/// Some threadsafe smart pointer.
+#[derive(strum_macros::IntoStaticStr)]
+pub enum SomeThreadsafePtr<Interface>
+where
+ Interface: 'static + ?Sized,
+{
+ /// A smart pointer to a interface in the transient scope.
+ Transient(TransientPtr<Interface>),
+
+ /// A smart pointer to a interface in the singleton scope.
+ ThreadsafeSingleton(ThreadsafeSingletonPtr<Interface>),
+
+ /// A smart pointer to a factory.
+ #[cfg(feature = "factory")]
+ ThreadsafeFactory(ThreadsafeFactoryPtr<Interface>),
+}
+
+impl<Interface> SomeThreadsafePtr<Interface>
+where
+ Interface: 'static + ?Sized,
+{
+ create_as_variant_fn!(SomeThreadsafePtr, Transient);
- create_as_variant_fn!(Singleton);
+ create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeSingleton);
#[cfg(feature = "factory")]
- create_as_variant_fn!(Factory);
+ create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeFactory);
}