blob: 44fc15c59063208913912a67c6cc2d971cf92c70 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#![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<Interface> = Box<Interface>;
/// A smart pointer to a interface in the singleton scope.
pub type SingletonPtr<Interface> = Rc<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>),
}
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>]<Interface>, SomePtrError>
{
if let SomePtr::$variant(ptr) = self {
return Ok(ptr);
}
Err(SomePtrError::WrongPtrType {
expected: stringify!($variant),
found: self.into()
})
}
}
};
}
impl<Interface> SomePtr<Interface>
where
Interface: 'static + ?Sized,
{
create_as_variant_fn!(Transient);
create_as_variant_fn!(Singleton);
#[cfg(feature = "factory")]
create_as_variant_fn!(Factory);
}
|