microcad_lang/
rc.rs

1// Copyright © 2024-2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4//! Short-cut definition of `Rc<std::cell::RefCell<T>>` and `Rc<T>`
5
6use derive_more::{Deref, DerefMut};
7pub use std::rc::Rc;
8
9#[cfg(feature = "debug-cell")]
10use debug_cell::RefCell;
11
12#[cfg(not(feature = "debug-cell"))]
13use std::cell::RefCell;
14
15/// Just a short cut definition
16#[derive(Deref, DerefMut)]
17pub struct RcMut<T>(Rc<RefCell<T>>);
18
19impl<T> RcMut<T> {
20    /// Create new instance
21    pub fn new(t: T) -> Self {
22        Self(Rc::new(RefCell::new(t)))
23    }
24}
25
26impl<T> Clone for RcMut<T> {
27    fn clone(&self) -> Self {
28        Self(self.0.clone())
29    }
30}
31
32impl<T> From<T> for RcMut<T> {
33    fn from(value: T) -> Self {
34        RcMut::new(value)
35    }
36}
37
38impl<T: std::fmt::Debug> std::fmt::Debug for RcMut<T> {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_tuple("RcMut").field(&self.0.borrow()).finish()
41    }
42}