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
use std::rc::Rc;

pub trait IntoRc<T: ?Sized> {
    fn into_rc(self) -> Rc<T>;
}

impl<T: ?Sized> IntoRc<T> for Rc<T> {
    fn into_rc(self) -> Rc<T> {
        self
    }
}

impl<T: ?Sized> IntoRc<T> for &Rc<T> {
    fn into_rc(self) -> Rc<T> {
        Rc::clone(self)
    }
}

impl<T> IntoRc<T> for T {
    fn into_rc(self) -> Rc<T> {
        Rc::new(self)
    }
}

pub trait TakeRc<T: ?Sized> {
    fn take_rc(self) -> Rc<T>;
}

impl<T: ?Sized, R: IntoRc<T>, F: FnOnce() -> R> TakeRc<T> for F {
    fn take_rc(self) -> Rc<T> {
        self().into_rc()
    }
}

impl<T: ?Sized> TakeRc<T> for Rc<T> {
    fn take_rc(self) -> Rc<T> {
        self
    }
}

// // Do we need to impl TakeInitialRc for common types ?
// macro_rules! impl_take_for_types {
//     ($($t:ty),+ $(,)?) => {
//         $(

//             impl TakeInitialRc<$t> for $t {
//                 fn take_initial_rc(self) -> Rc<$t> {
//                     Rc::new(self)
//                 }
//             }
//         )+
//     };
// }

// impl_take_for_types! {
//     String,
//     usize,
// }

pub trait IntoOptionalRc<T: ?Sized> {
    fn into_optional_rc(self) -> Option<Rc<T>>;
}

impl<T: ?Sized> IntoOptionalRc<T> for Rc<T> {
    fn into_optional_rc(self) -> Option<Rc<T>> {
        Some(self)
    }
}

impl<T> IntoOptionalRc<T> for Option<T> {
    fn into_optional_rc(self) -> Option<Rc<T>> {
        self.map(|v| Rc::new(v))
    }
}

impl<T: ?Sized> IntoOptionalRc<T> for Option<Rc<T>> {
    fn into_optional_rc(self) -> Option<Rc<T>> {
        self
    }
}

impl<T> IntoOptionalRc<T> for T {
    fn into_optional_rc(self) -> Option<Rc<T>> {
        Some(Rc::new(self))
    }
}