You can use a wrapper struct to add your own methods to Rc<RefCell<...>>:
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Default, Clone)]
pub struct Handle<T>(Rc<RefCell<T>>);
#[derive(Debug, Default, Clone)]
pub struct Main {
pub child: Handle<MainChild>,
pub count: i32,
}
impl Main {
pub fn create() -> Handle<Self> {
let main = Main {
child: MainChild::create(),
count: 0,
};
Handle(Rc::new(RefCell::new(main)))
}
}
impl Handle<Main> {
pub fn render(& self) {
// use std::borrow::BorrowMut;
println!("{:?}", self);
println!("{:?}", self.0.borrow().child);
// println!("{:?}", self.child.borrow_mut());
self.0.borrow().child.render();
}
}
#[derive(Debug, Default, Clone)]
pub struct MainChild {
pub count: i32,
}
impl MainChild {
pub fn create() -> Handle<Self> {
let child = MainChild { count: 0 };
Handle(Rc::new(RefCell::new(child)))
}
}
impl Handle<MainChild> {
pub fn render(& self) {
self.0.borrow_mut().count += 1;
println!("{:?}", self);
}
}
pub fn main() {
let main = Main::create();
main.render();
}