I have a few Copy types, and I really appreciate the handiness of them being Copy.
I would like one of those types to contain a mutable field, using Interior Mutability as in cell::Cell. There are probably other solutions for the problem I am trying to solve, but Interior Mutability is cheap. I like cheap.
However, it turns out that cell::Cell is not Copy, and from the comments is unlikely to become Copy as the maintainers fear this would be error-prone.
A comment by the OP is my current beacon of hope:
It's not the absolute end of the world if it doesn't for my own data structures (since I can just make my own version of
Cell)
Though I do not see how they purport to achieve this feat.
The core issue I have is that UnsafeCell:
- is mandatory to implement Interior Mutability,
- is not
Copy.
Which seems to close the door to any hope of implementing a CopyCell, unless I am missing a trick (a way to unsafe impl the Copy trait?).
MCVE:
#[derive(Clone, Copy)]
struct HelloWorld(Cell<u32>);
| 3 | #[derive(Clone, Copy)] | ^^^^ 4 | struct HelloWorld(Cell<u32>); | ---------- this field does not implement `Copy`
What should I replace Cell with for HelloWorld to be Copy?
Note: at the moment, the only way I can see of achieving the desired outcome is to use a &Cell<T>; with all the lifetimes implications.
Cell, but not the value inside it, right ? - TigranCelldoesn't provide shared ownership (unlikeRcorArc) so in order to copy aCell, you must copy the value inside it. - Francis GagnéCopyboth. For what it's worth my usecase is for aCell<u32>(which is why&Cellis irksome, as it triples the memory requirement). - Matthieu M.