Skip to content

Commit

Permalink
Merge pull request #143 from Wodann/feature/clone-struct-ref
Browse files Browse the repository at this point in the history
feat(runtime): add cloning of StructRef
  • Loading branch information
Wodann authored Apr 29, 2020
2 parents dfd26e8 + b51212e commit f1b49f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/mun_runtime/src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Runtime>>,
Expand Down
21 changes: 21 additions & 0 deletions crates/mun_runtime/tests/marshalling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
field_name: &str,
) {
assert_eq!(s1.get::<T>(field_name), s2.get::<T>(field_name));
s1.set(field_name, data.1).unwrap();
assert_eq!(s1.get::<T>(field_name), s2.get::<T>(field_name));
s1.replace(field_name, data.0).unwrap();
assert_eq!(s1.get::<T>(field_name), s2.get::<T>(field_name));
}

// Verify that StructRef::clone returns a `StructRef` to the same memory
let mut foo = baz.get::<StructRef>("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::<StructRef>("0").unwrap();

// Specify invalid return type
Expand Down

0 comments on commit f1b49f2

Please sign in to comment.