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
pub mod __impl {
    use std::rc::Rc;

    pub trait CheckIsRc {
        type Output;
        fn check_is_rc(&self) -> Self::Output;
    }

    pub struct IsRc;

    impl IsRc {
        #[inline]
        pub fn auto_wrap_rc<T>(self, v: Rc<T>) -> Rc<T> {
            v
        }
    }

    pub struct NotRc;

    impl NotRc {
        #[inline]
        pub fn auto_wrap_rc<T>(self, v: T) -> Rc<T> {
            Rc::new(v)
        }
    }

    impl<T: ?Sized> CheckIsRc for Rc<T> {
        type Output = IsRc;

        #[inline]
        fn check_is_rc(&self) -> Self::Output {
            IsRc
        }
    }

    impl<T> CheckIsRc for &T {
        type Output = NotRc;

        fn check_is_rc(&self) -> Self::Output {
            NotRc
        }
    }
}

#[macro_export]
macro_rules! auto_wrap_rc {
    ($e:expr) => {{
        use $crate::__private::auto_wrap_rc::__impl::CheckIsRc;
        match $e {
            ref v => v.check_is_rc().auto_wrap_rc($e),
        }
    }};
}

#[cfg(test)]
mod tests {
    use std::rc::Rc;

    #[test]
    fn auto_wrap_rc() {
        let a = crate::auto_wrap_rc!(1);
        let _: Rc<i32> = a;

        let b = crate::auto_wrap_rc!(Rc::new(true));
        let _: Rc<bool> = b;

        let c = String::new();
        let c = crate::auto_wrap_rc!(c);
        let _: Rc<String> = c;

        let d = Rc::new(String::new());
        let d = crate::auto_wrap_rc!(d);
        let _: Rc<String> = d;
    }

    #[test]
    fn nested() {
        let a = crate::auto_wrap_rc!(crate::auto_wrap_rc!(String::new()));
        let _: Rc<String> = a;
    }
}