#![allow(clippy::module_name_repetitions)] //! Smart pointer type aliases. use std::rc::Rc; use paste::paste; use crate::errors::ptr::SomePtrError; /// A smart pointer for a interface in the transient scope. pub type TransientPtr = Box; /// A smart pointer to a interface in the singleton scope. pub type SingletonPtr = Rc; /// A smart pointer to a factory. #[cfg(feature = "factory")] pub type FactoryPtr = Rc; /// Some smart pointer. #[derive(strum_macros::IntoStaticStr)] pub enum SomePtr where Interface: 'static + ?Sized, { /// A smart pointer to a interface in the transient scope. Transient(TransientPtr), /// A smart pointer to a interface in the singleton scope. Singleton(SingletonPtr), /// A smart pointer to a factory. #[cfg(feature = "factory")] Factory(FactoryPtr), } macro_rules! create_as_variant_fn { ($variant: ident) => { paste! { #[doc = "Returns as " [<$variant:lower>] ".\n" "\n" "# Errors\n" "Will return Err if it's not a " [<$variant:lower>] "." ] pub fn [<$variant:lower>](self) -> Result<[<$variant Ptr>], SomePtrError> { if let SomePtr::$variant(ptr) = self { return Ok(ptr); } Err(SomePtrError::WrongPtrType { expected: stringify!($variant), found: self.into() }) } } }; } impl SomePtr where Interface: 'static + ?Sized, { create_as_variant_fn!(Transient); create_as_variant_fn!(Singleton); #[cfg(feature = "factory")] create_as_variant_fn!(Factory); }