#![allow(dead_code, deprecated, unused_variables, unused_mut)] #![feature(clone_to_uninit)] use std::clone::CloneToUninit; use std::mem::offset_of; use std::rc::Rc; #[derive(PartialEq)] struct MyDst { label: String, contents: T, } unsafe impl CloneToUninit for MyDst { unsafe fn clone_to_uninit(&self, dest: *mut u8) { let offset_of_contents = unsafe { (&raw const self.contents).byte_offset_from_unsigned(self) }; let label = self.label.clone(); unsafe { self.contents.clone_to_uninit(dest.add(offset_of_contents)); dest.add(offset_of!(Self, label)).cast::().write(label); } } } fn main() { let first: Rc> = Rc::new(MyDst { label: String::from("hello"), contents: [1, 2, 3, 4], }); let mut second = first.clone(); for elem in Rc::make_mut(&mut second).contents.iter_mut() { *elem *= 10; } assert_eq!(first.contents, [1, 2, 3, 4]); assert_eq!(second.contents, [10, 20, 30, 40]); assert_eq!(second.label, "hello"); }