From 21f3f145eb4e9d6312e91cc507be4afb4bde7081 Mon Sep 17 00:00:00 2001 From: Wodann Date: Wed, 29 Apr 2020 00:43:47 +0200 Subject: [PATCH] feat(runtime): add cloning of StructRef --- crates/mun_runtime/src/struct.rs | 1 + crates/mun_runtime/tests/marshalling.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/mun_runtime/src/struct.rs b/crates/mun_runtime/src/struct.rs index d140746b4..7d88082ea 100644 --- a/crates/mun_runtime/src/struct.rs +++ b/crates/mun_runtime/src/struct.rs @@ -27,6 +27,7 @@ impl RawStruct { /// Type-agnostic wrapper for interoperability with a Mun struct. /// TODO: Handle destruction of `struct(value)` +#[derive(Clone)] pub struct StructRef { handle: GcRootPtr, runtime: Rc>, diff --git a/crates/mun_runtime/tests/marshalling.rs b/crates/mun_runtime/tests/marshalling.rs index c83218df6..6af996738 100644 --- a/crates/mun_runtime/tests/marshalling.rs +++ b/crates/mun_runtime/tests/marshalling.rs @@ -430,6 +430,27 @@ fn marshal_struct() { test_shallow_copy(&mut foo, &foo2, &int_data, "a"); test_shallow_copy(&mut foo, &foo2, &bool_data, "b"); + fn test_clone< + T: Copy + std::fmt::Debug + PartialEq + ArgumentReflection + ReturnTypeReflection, + >( + s1: &mut StructRef, + s2: &StructRef, + data: &TestData, + field_name: &str, + ) { + assert_eq!(s1.get::(field_name), s2.get::(field_name)); + s1.set(field_name, data.1).unwrap(); + assert_eq!(s1.get::(field_name), s2.get::(field_name)); + s1.replace(field_name, data.0).unwrap(); + assert_eq!(s1.get::(field_name), s2.get::(field_name)); + } + + // Verify that StructRef::clone returns a `StructRef` to the same memory + let mut foo = baz.get::("0").unwrap(); + let foo2 = foo.clone(); + test_clone(&mut foo, &foo2, &int_data, "a"); + test_clone(&mut foo, &foo2, &bool_data, "b"); + let mut bar = qux.get::("0").unwrap(); // Specify invalid return type