aboutsummaryrefslogtreecommitdiff
path: root/src/ptr.rs
blob: bcaa56679b38a32e4c9d9c98351785620572f96d (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Smart pointer type aliases.
use std::rc::Rc;
use std::sync::Arc;

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 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")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
pub type FactoryPtr<FactoryInterface> = Rc<FactoryInterface>;

/// A threadsafe smart pointer to a factory.
#[cfg(feature = "factory")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
pub type ThreadsafeFactoryPtr<FactoryInterface> = Arc<FactoryInterface>;

macro_rules! create_as_variant_fn {
    ($enum: ident, $variant: ident, $err: ident) => {
        create_as_variant_fn!($enum, $variant, $err,);
    };

    ($enum: ident, $variant: ident, $err: ident, $($attrs: meta),*) => {
        paste! {
            #[doc =
                "Returns as the `" [<$variant>] "` variant.\n"
                "\n"
                "# Errors\n"
                "Will return Err if it's not the `" [<$variant>] "` variant."
            ]
            $(#[$attrs])*
            pub fn [<$variant:snake>](self) -> Result<[<$variant Ptr>]<Interface>, $err>
            {
                if let $enum::$variant(ptr) = self {
                    return Ok(ptr);
                }

                Err($err::WrongPtrType {
                    expected: stringify!($variant),
                    found: self.into()
                })
            }
        }
    };
}

/// 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")]
    #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
    Factory(FactoryPtr<Interface>),
}

impl<Interface> SomePtr<Interface>
where
    Interface: 'static + ?Sized,
{
    create_as_variant_fn!(SomePtr, Transient, SomePtrError);

    create_as_variant_fn!(SomePtr, Singleton, SomePtrError);

    create_as_variant_fn!(
        SomePtr,
        Factory,
        SomePtrError,
        cfg(feature = "factory"),
        cfg_attr(doc_cfg, doc(cfg(feature = "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")]
    #[cfg_attr(doc_cfg, doc(cfg(feature = "factory")))]
    ThreadsafeFactory(ThreadsafeFactoryPtr<Interface>),
}

impl<Interface> SomeThreadsafePtr<Interface>
where
    Interface: 'static + ?Sized,
{
    create_as_variant_fn!(SomeThreadsafePtr, Transient, SomePtrError);

    create_as_variant_fn!(SomeThreadsafePtr, ThreadsafeSingleton, SomePtrError);

    create_as_variant_fn!(
        SomeThreadsafePtr,
        ThreadsafeFactory,
        SomePtrError,
        cfg(feature = "factory"),
        cfg_attr(doc_cfg, doc(cfg(feature = "factory")))
    );
}