I've implemented a version of Rc::make_mut that works with Rc<[T]>. It does not look like rust code and I am not confident that it is safe (wrong alignment?). I've ran valgrind on it, and was ok.
Can you help me with this code?
I think this function is interesting for the general public. The function can be written in terms of fn clone_content<T>(&Rc<T>) -> Rc<T>. Do you think it can be proposed to be included in std?
This code is safe in practice. One problem I see is that T can be a SIMD type, which must be aligned to 16 bytes, and these two Cells will always give it an offset of 8 bytes on 32-bit systems. You should just copy-paste the definition of RcBox and use it. Even then, this isn't guaranteed to work forever because the representation of fat pointers isn't guaranteed to stay the same, which is another problem.
I'm not sure this should be included in std. Could you give a use case?
Here's a workaround for the first borrow error:
pub fn my_make_mut<T: Clone + Debug>(this: &mut Rc<[T]>) -> &mut [T] {
if Rc::is_unique(this) {
Rc::get_mut(this).unwrap()
} else {
let r = clone_content(this);
*this = r;
Rc::get_mut(this).unwrap()
}
}