Feedback wanted: rcstruct - transparent `Struct(Rc<RefCell<Struct>>)` newtypes

I'm working on a project (GUI) that could use some ownership lenience, so I ended up with this pattern:

struct GUIInternal { .. }

impl GUIInternal {
    fn do_stuff(&mut self, ..) -> Result {
        :
    }

    :
}

pub struct GUI(Rc<RefCell<StructInternal>>);

impl GUI {
    pub fn do_stuff(&self, ..) -> Result {
        self.0.borrow_mut().do_stuff(..)
    }

    :
}

As I don't want to keep wrapping this manually for multiple methods, I looked into using macros to generate the outer structure. The result looks like this:

rcstruct::rcstruct! {
    pub struct GUI { .. }

    impl {
        pub fn do_stuff(&mut self, ..) -> Result {
            :
        }

        :
    }
}

For a longer example, see rcstruct — Rust proc macro helper // Lib.rs.

I've published this as rcstruct = "0.1.0" to gather feedback. Any feedback is welcome, but specifically:

  • Is this worth doing, or is there an easier way to solve this without resorting to a macro? (Constraints: Wrapper needs to be Clone and have interior mutability. Both inner and outer structure are only ever going to be accessed by a single thread.)
  • Is there already another crate that does this or something similar to this that I could have a look at?

In any case, it's been a fun trip into proc_macro land :slight_smile: – now looking forward to your comments!

1 Like

I don't know the right answer to this, but I also find myself constantly writing:

struct FooInternal {}
pub struct Foo(Rc<RefCell<FooInternal>>);

struct BarInternal {}]
pub struct Bar(Rc<BarInternal>) {}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.