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
pub trait Storage: Sized + 'static {
    /// If Self::Tx implements clone, clone it.  Otherwise use Option::take
    fn take_or_clone(res: &mut Option<Self>) -> Option<Self>;

    fn clone_slot(res: &mut Option<Self>) -> Option<Self>
    where
        Self: Clone,
    {
        res.as_ref().map(|t| t.clone())
    }

    fn take_slot(res: &mut Option<Self>) -> Option<Self> {
        res.take()
    }
}

#[macro_export]
macro_rules! impl_storage_take {
    ( $name:ty ) => {
        impl $crate::Storage for $name {
            fn take_or_clone(res: &mut Option<Self>) -> Option<Self> {
                Self::take_slot(res)
            }
        }
    };
}

#[macro_export]
macro_rules! impl_channel_take {
    ( $name:ty ) => {
        impl<T: Send + 'static> $crate::Storage for $name {
            fn take_or_clone(res: &mut Option<Self>) -> Option<Self> {
                Self::take_slot(res)
            }
        }
    };
}

#[macro_export]
macro_rules! impl_storage_clone {
    ( $name:ty ) => {
        impl $crate::Storage for $name {
            fn take_or_clone(res: &mut Option<Self>) -> Option<Self> {
                Self::clone_slot(res)
            }
        }
    };
}

#[macro_export]
macro_rules! impl_channel_clone {
    ( $name:ty ) => {
        impl<T: Send + 'static> $crate::Storage for $name {
            fn take_or_clone(res: &mut Option<Self>) -> Option<Self> {
                Self::clone_slot(res)
            }
        }
    };
}