//# publish module 0x42.DeepCopy { import 0x1.vector; struct Config has copy, drop, store { i: u64 } struct Nested has copy, drop, store { c: Self.Config } public test_struct_shallow() { let c1: Self.Config; let c2: Self.Config; label b0: c1 = Config { i: 0 }; c2 = copy(c1); // mutate c1.i to 1 *(&mut (&mut c1).Config::i) = 1; // c2.i should still be 0 assert(*&(&c2).Config::i == 0, 77); // mutate c2.i to 2 *(&mut (&mut c2).Config::i) = 2; // c1.i should still be 1 assert(*&(&c1).Config::i == 1, 78); return; } public test_struct_deep() { let n1: Self.Nested; let n2: Self.Nested; label b0: n1 = Nested { c: Config { i: 0 } }; n2 = copy(n1); // mutate n1.c.i to 1 *(&mut (&mut (&mut n1).Nested::c).Config::i) = 1; // n2.c.i should still be 0 assert(*&(&(&n2).Nested::c).Config::i == 0, 79); // n1.c.i is 1 assert(*&(&(&n1).Nested::c).Config::i == 1, 80); // mutate n2.c.i to 2 *(&mut (&mut (&mut n2).Nested::c).Config::i) = 2; // n1.c.i should still be 1 assert(*&(&(&n1).Nested::c).Config::i == 1, 81); // n2.c.i is 2 assert(*&(&(&n2).Nested::c).Config::i == 2, 82); return; } public test_vector() { let v1: vector; let v2: vector; label b0: v1 = vector.empty(); vector.push_back(&mut v1, 0); v2 = copy(v1); *vector.borrow_mut(&mut v1, 0) = 1; assert(*vector.borrow(&v2, 0) == 0, 83); *vector.borrow_mut(&mut v2, 0) = 2; assert(*vector.borrow(&v1, 0) == 1, 84); return; } public test_vector_to_struct() { let v1: vector; let v2: vector; let c1: Self.Config; let c2: Self.Config; let r1: &mut Self.Config; let r2: &mut Self.Config; let ri1: &mut u64; label b0: c1 = Config { i: 0 }; c2 = copy(c1); *(&mut (&mut c2).Config::i) = 1; v1 = vector.empty(); vector.push_back(&mut v1, move(c1)); vector.push_back(&mut v1, move(c2)); v2 = copy(v1); r1 = vector.borrow_mut(&mut v1, 0); r2 = vector.borrow_mut(&mut v2, 0); assert(*©(r1).Config::i == 0, 90); assert(*©(r1).Config::i == *&move(r2).Config::i, 91); r2 = vector.borrow_mut(&mut v2, 1); assert(*©(r2).Config::i == 1, 91); assert(*©(r1).Config::i != *©(r2).Config::i, 91); *&mut copy(r1).Config::i = 1; assert(*©(r1).Config::i == *©(r2).Config::i, 92); return; } } //# run import 0x42.DeepCopy; main() { label b0: DeepCopy.test_struct_shallow(); DeepCopy.test_struct_deep(); DeepCopy.test_vector(); DeepCopy.test_vector_to_struct(); return; }