If I write a struct:
struct Foo<T> { foo: T }
then Foo<T>
will be Copy
if and only if T
is Copy
.
Right now, &mut T
is never Copy
, always affine (move-only). This is because mutable aliasing in general can violate soundness, i.e. the iterator invalidation problem. But you clearly can’t violate soundness with, for example, a mutably aliased int
.
What I’m wondering is whether we could, or should, do the same thing for &mut T
as for Foo<T>
, and say that it’s Copy
iff T: Copy
. This would essentially mean allowing things which are Copy
(“plain old data”) to be mutably aliased. We already do this under controlled circumstances with Cell
.
Doing this would have obvious benefits in terms of expressiveness. What negative aspects would it have? I don’t think it would compromise soundness (sketch of a proof: otherwise Cell
would also be unsound). But it would involve losing some guarantees, e.g. right now, if you have an &mut int
, you can know that nobody else can mutate that int
but you. This would no longer be true (similar to &const
).
EDIT: Here’s a more serious problem: it would also break the guarantee for &int
, because you can borrow &mut int
to &int
. So &int
would also behave like &const int
. I’m liking this idea less now.
Has this idea been considered before?