Skip to main content

leak_playground_std/
rc.rs

1//! Possible [`std::rc`] replacements.
2
3use core::fmt;
4use std::rc::Rc as StdRc;
5
6use crate::marker::Forget;
7
8#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct Rc<T> {
10    inner: StdRc<T>,
11}
12
13impl<T> Rc<T> {
14    /// Constructs a new `Rc<T>`.
15    pub fn new(x: T) -> Self
16    where
17        T: Forget,
18    {
19        Rc {
20            inner: StdRc::new(x),
21        }
22    }
23
24    /// Constructs a new `Rc<T>`, where `T` is an unforgettable type.
25    ///
26    /// # Safety
27    ///
28    /// `T` must not take ownership over itself.
29    pub unsafe fn new_unchecked(x: T) -> Self {
30        Rc {
31            inner: StdRc::new(x),
32        }
33    }
34}
35
36impl<T> Clone for Rc<T> {
37    fn clone(&self) -> Self {
38        Rc {
39            inner: StdRc::clone(&self.inner),
40        }
41    }
42}
43
44impl<T> AsRef<T> for Rc<T> {
45    fn as_ref(&self) -> &T {
46        StdRc::as_ref(&self.inner)
47    }
48}
49
50impl<T> core::borrow::Borrow<T> for Rc<T> {
51    fn borrow(&self) -> &T {
52        &self.inner
53    }
54}
55
56impl<T> std::ops::Deref for Rc<T> {
57    type Target = T;
58
59    fn deref(&self) -> &Self::Target {
60        &self.inner
61    }
62}
63
64impl<T> fmt::Display for Rc<T>
65where
66    T: fmt::Display,
67{
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        fmt::Display::fmt(&self.inner, f)
70    }
71}
72
73impl<T> fmt::Debug for Rc<T>
74where
75    T: fmt::Debug,
76{
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        fmt::Debug::fmt(&self.inner, f)
79    }
80}
81
82impl<T> fmt::Pointer for Rc<T> {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        fmt::Pointer::fmt(&self.inner, f)
85    }
86}