Description
Proposal
Problem statement
A typical pattern of interior mutability in single-thread contexts is to use Cell<T>
for T: Copy
, and RefCell<T>
for T: !Copy
. Then the default choice for an interiorly mutable Rc<T>
seems to be wrapping it to a RefCell<Rc<T>>
.
However, the borrow sign in an RefCell<Rc<T>>
is a waste because Rc<T>
can be cloned as cheaply as incrementing the borrow sign. However, if we wrap the Rc<T>
into a Cell<Rc<T>>
instead, we can not get its value since Cell::get
requires T: Copy
.
Motivating examples or use cases
Suppose we are going to implement a binary tree with both children and parent references in a single-thread context. There are usually two ways to implement:
use std::{cell::{Cell, RefCell}, rc::{Rc, Weak}};
mod ref_cell_v1 {
struct BinaryTree<T> {
parent: RefCell<Option<Weak<BinaryTree<T>>>>,
left: RefCell<Option<Rc<BinaryTree<T>>>>,
right: RefCell<Option<Rc<BinaryTree<T>>>>,
depth: Cell<usize>,
value: RefCell<T>,
}
}
mod ref_cell_v2 {
struct BinaryTree<T> {
parent: Option<Weak<RefCell<BinaryTree<T>>>>,
left: Option<Rc<RefCell<BinaryTree<T>>>>,
right: Option<Rc<RefCell<BinaryTree<T>>>>,
depth: usize,
value: T,
}
}
Version ref_cell_v1
has overheads of 4 borrow signs and the version ref_cell_v2
has 3 borrow signs.
(If T: Copy
, ref_cell_v1
can reduce 1 borrow sign by switching to value: Cell<T>
instead.)
Solution sketch
These overheads can be avoided by using Cell
to wrap the Option<Rc>
and Option<Weak>
references, but requires Cell::get
for these types to read their values.
mod cell {
struct BinaryTree<T> {
parent: Cell<Option<Weak<BinaryTree<T>>>>,
left: Cell<Option<Rc<BinaryTree<T>>>>,
right: Cell<Option<Rc<BinaryTree<T>>>>,
depth: Cell<usize>,
value: RefCell<T>,
}
}
Now in this cell
version, there is only one overhead borrow sign in value: RefCell<T>
.
The user can add a Cell::get
for Rc
-like types via a trait. But it is impossible to write a trait that supports both Rc
-like types and Copy
types, because it would be a conflict to have both an implementation of Cell<T>
where T: Copy
and that where T: ShallowCopy
.
Alternatives
use std::{cell::Cell, rc::{Rc, Weak}};
trait CellGet {
type Target;
fn get(&self) -> Self::Target;
}
impl<T> CellGet for Cell<Rc<T>> {
type Target = Rc<T>;
fn get(&self) -> Self::Target {
Clone::clone(unsafe {&*self.as_ptr()})
}
}
impl<T> CellGet for Cell<Weak<T>> {
type Target = Weak<T>;
fn get(&self) -> Self::Target {
Clone::clone(unsafe {&*self.as_ptr()})
}
}
impl<T> CellGet for Cell<Option<Rc<T>>> {
type Target = Option<Rc<T>>;
fn get(&self) -> Self::Target {
Clone::clone(unsafe {&*self.as_ptr()})
}
}
impl<T> CellGet for Cell<Option<Weak<T>>> {
type Target = Option<Weak<T>>;
fn get(&self) -> Self::Target {
Clone::clone(unsafe {&*self.as_ptr()})
}
}
Links and related work
None
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.