react/
take_rc.rs

1use std::rc::Rc;
2
3pub trait IntoRc<T: ?Sized> {
4    fn into_rc(self) -> Rc<T>;
5}
6
7impl<T: ?Sized> IntoRc<T> for Rc<T> {
8    fn into_rc(self) -> Rc<T> {
9        self
10    }
11}
12
13impl<T: ?Sized> IntoRc<T> for &Rc<T> {
14    fn into_rc(self) -> Rc<T> {
15        Rc::clone(self)
16    }
17}
18
19impl<T> IntoRc<T> for T {
20    fn into_rc(self) -> Rc<T> {
21        Rc::new(self)
22    }
23}
24
25pub trait TakeRc<T: ?Sized> {
26    fn take_rc(self) -> Rc<T>;
27}
28
29impl<T: ?Sized, R: IntoRc<T>, F: FnOnce() -> R> TakeRc<T> for F {
30    fn take_rc(self) -> Rc<T> {
31        self().into_rc()
32    }
33}
34
35impl<T: ?Sized> TakeRc<T> for Rc<T> {
36    fn take_rc(self) -> Rc<T> {
37        self
38    }
39}
40
41// // Do we need to impl TakeInitialRc for common types ?
42// macro_rules! impl_take_for_types {
43//     ($($t:ty),+ $(,)?) => {
44//         $(
45
46//             impl TakeInitialRc<$t> for $t {
47//                 fn take_initial_rc(self) -> Rc<$t> {
48//                     Rc::new(self)
49//                 }
50//             }
51//         )+
52//     };
53// }
54
55// impl_take_for_types! {
56//     String,
57//     usize,
58// }
59
60pub trait IntoOptionalRc<T: ?Sized> {
61    fn into_optional_rc(self) -> Option<Rc<T>>;
62}
63
64impl<T: ?Sized> IntoOptionalRc<T> for Rc<T> {
65    fn into_optional_rc(self) -> Option<Rc<T>> {
66        Some(self)
67    }
68}
69
70impl<T> IntoOptionalRc<T> for Option<T> {
71    fn into_optional_rc(self) -> Option<Rc<T>> {
72        self.map(|v| Rc::new(v))
73    }
74}
75
76impl<T: ?Sized> IntoOptionalRc<T> for Option<Rc<T>> {
77    fn into_optional_rc(self) -> Option<Rc<T>> {
78        self
79    }
80}
81
82impl<T> IntoOptionalRc<T> for T {
83    fn into_optional_rc(self) -> Option<Rc<T>> {
84        Some(Rc::new(self))
85    }
86}