Skip to main content

microcad_lang_base/
rc.rs

1// Copyright © 2024-2026 The µcad authors <info@microcad.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
9use std::cell::RefCell;
10
11/// Just a short cut definition
12#[derive(Deref, DerefMut)]
13pub struct RcMut<T>(Rc<RefCell<T>>);
14
15impl<T> RcMut<T> {
16    /// Create new instance
17    pub fn new(t: T) -> Self {
18        Self(Rc::new(RefCell::new(t)))
19    }
20}
21
22impl<T> Clone for RcMut<T> {
23    fn clone(&self) -> Self {
24        Self(self.0.clone())
25    }
26}
27
28impl<T> From<T> for RcMut<T> {
29    fn from(value: T) -> Self {
30        RcMut::new(value)
31    }
32}
33
34impl<T: std::fmt::Debug> std::fmt::Debug for RcMut<T> {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        f.debug_tuple("RcMut").field(&self.0.borrow()).finish()
37    }
38}