Skip to content

RFC: Add RcMut<T> and GcMut<T> wrappers #12724

Closed
@sfackler

Description

@sfackler

Working with mutable shared state is currently a bit gross. The crazy foo.borrow().borrow_mut().get().bar() stuff will be gone with auto-deref (yay!), but there's still the issue that a type like Rc<RefCell<T>> is gross looking and offputting to newcomers. I think a reasonable solution would be the introduction of some lightweight wrappers:

pub struct RcMut<T> {
    priv inner: Rc<RefCell<T>>
}

impl<T> RcMut<T> {
    pub fn new(t: T) -> RcMut<T> {
        RcMut { inner: Rc::new(RefCell::new(t)) }
    }

    pub fn borrow<'a>(&'a self) -> cell::Ref<'a, T> {
        self.inner.borrow().borrow()
    }

    ...
}

It's still deferring all of the heavy lifting to Rc and RefCell and should make downstream code cleaner at a pretty minimal cost.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions