From 4dfe84c4d1feae8c48fa57ca5b89798b62d8d7ab Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Mon, 13 Dec 2021 00:33:07 +0000 Subject: [PATCH 1/2] Fix wasm_importtype_new's Rust signature to not assume boxed vectors Fixes #2718 --- lib/c-api/src/wasm_c_api/types/import.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/types/import.rs b/lib/c-api/src/wasm_c_api/types/import.rs index 5e075ece7a9..dc2e891d77e 100644 --- a/lib/c-api/src/wasm_c_api/types/import.rs +++ b/lib/c-api/src/wasm_c_api/types/import.rs @@ -14,13 +14,13 @@ wasm_declare_boxed_vec!(importtype); #[no_mangle] pub extern "C" fn wasm_importtype_new( - module: Option>, - name: Option>, + module: Option<&mut wasm_name_t>, + name: Option<&mut wasm_name_t>, extern_type: Option>, ) -> Option> { Some(Box::new(wasm_importtype_t { - name: *name?, - module: *module?, + name: name?.take().into(), + module: module?.take().into(), extern_type: *extern_type?, })) } From 0f963929664c7edc1e1c65748f49af365c085458 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Mon, 13 Dec 2021 10:59:34 +0000 Subject: [PATCH 2/2] Update C API development docs --- lib/c-api/src/wasm_c_api/DEVELOPMENT.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/DEVELOPMENT.md b/lib/c-api/src/wasm_c_api/DEVELOPMENT.md index 76a56234a32..048a82cacb3 100644 --- a/lib/c-api/src/wasm_c_api/DEVELOPMENT.md +++ b/lib/c-api/src/wasm_c_api/DEVELOPMENT.md @@ -21,14 +21,19 @@ This ownership property translates well in Rust. We have decided to use the `Box` type to represent an owned pointer. `Box` drops its content when it's dropped. +However, vectors (such as `wasm_name_t`) are a special case: for those, `own` +means that the ownership of contents of the vector are transfered, not the +allocation of the vector type itself. Hence these are represented using +`&mut T` instead. + Consequently, apart from other patterns, the code above can be written as follows in Rust: ```rust #[no_mangle] pub extern "C" fn wasm_importtype_new( - module: Box, - name: Box, + module: &mut wasm_name_t, + name: &mut wasm_name_t, extern_type: Box, ) -> Box { … @@ -81,13 +86,13 @@ translates into Rust as: ```rust #[no_mangle] pub extern "C" fn wasm_importtype_new( - module: Option>, - name: Option>, + module: Option<&mut wasm_name_t>, + name: Option<&mut wasm_name_t>, extern_type: Option>, ) -> Option> { Some(Box::new(wasm_importtype_t { - name: name?, - module: module?, + name: name?.take().into(), + module: module?.take().into(), extern_type: extern_type?, })) }