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
//! Possible [`std::rc`] replacements.

use core::fmt;
use std::rc::Rc as StdRc;

use crate::marker::Forget;

#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rc<T> {
    inner: StdRc<T>,
}

impl<T> Rc<T> {
    /// Constructs a new `Rc<T>`.
    pub fn new(x: T) -> Self
    where
        T: Forget,
    {
        Rc {
            inner: StdRc::new(x),
        }
    }

    /// Constructs a new `Rc<T>`, where `T` is an unforgettable type.
    ///
    /// # Safety
    ///
    /// `T` must not take ownership over itself.
    pub unsafe fn new_unchecked(x: T) -> Self {
        Rc {
            inner: StdRc::new(x),
        }
    }
}

impl<T> Clone for Rc<T> {
    fn clone(&self) -> Self {
        Rc {
            inner: StdRc::clone(&self.inner),
        }
    }
}

impl<T> AsRef<T> for Rc<T> {
    fn as_ref(&self) -> &T {
        StdRc::as_ref(&self.inner)
    }
}

impl<T> core::borrow::Borrow<T> for Rc<T> {
    fn borrow(&self) -> &T {
        &self.inner
    }
}

impl<T> std::ops::Deref for Rc<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> fmt::Display for Rc<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl<T> fmt::Debug for Rc<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.inner, f)
    }
}

impl<T> fmt::Pointer for Rc<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Pointer::fmt(&self.inner, f)
    }
}