Skip to content

Extend interior mutability (e.g. Cell) for better composition with other types. #1106

@eddyb

Description

@eddyb

A couple of ideas were outlined in this reddit comment.

1: Implementing the API for Cell<Rc<T>> and Cell<Vec<T>>: requires a lot of unsafe.

There might be a way to provide mutable access to the contents of Cell via Cell::with_mut(|mut_ref| {...}) and have the compiler prove that the closure cannot reach the Cell::with_mut entry point, but it's not something we can express right now.

2: Turning &mut T into multiple aliases with Cell semantics:

impl<T: Copy> Cell<T> {
    fn from_mut(x: &mut T) -> &Cell<T> {
        unsafe { mem::transmute(x) }
    }
}

I am not certain this is UB-free, but if it isn't, I still expect @glaebhoerl's CellRef<'a, T> suggestion to work.

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions