Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wasm_importtype_new's Rust signature to not assume boxed vectors #2719

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/c-api/src/wasm_c_api/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ This ownership property translates well in Rust. We have decided to
use the `Box<T>` type to represent an owned pointer. `Box<T>` 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.
Comment on lines +24 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this extra doc. Super useful!


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<wasm_name_t>,
name: Box<wasm_name_t>,
module: &mut wasm_name_t,
name: &mut wasm_name_t,
extern_type: Box<wasm_externtype_t>,
) -> Box<wasm_importtype_t> {
Expand Down Expand Up @@ -81,13 +86,13 @@ translates into Rust as:
```rust
#[no_mangle]
pub extern "C" fn wasm_importtype_new(
module: Option<Box<wasm_name_t>>,
name: Option<Box<wasm_name_t>>,
module: Option<&mut wasm_name_t>,
name: Option<&mut wasm_name_t>,
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_importtype_t>> {
Some(Box::new(wasm_importtype_t {
name: name?,
module: module?,
name: name?.take().into(),
module: module?.take().into(),
extern_type: extern_type?,
}))
}
Expand Down
8 changes: 4 additions & 4 deletions lib/c-api/src/wasm_c_api/types/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ wasm_declare_boxed_vec!(importtype);

#[no_mangle]
pub extern "C" fn wasm_importtype_new(
module: Option<Box<wasm_name_t>>,
name: Option<Box<wasm_name_t>>,
module: Option<&mut wasm_name_t>,
name: Option<&mut wasm_name_t>,
extern_type: Option<Box<wasm_externtype_t>>,
) -> Option<Box<wasm_importtype_t>> {
Some(Box::new(wasm_importtype_t {
name: *name?,
module: *module?,
name: name?.take().into(),
module: module?.take().into(),
extern_type: *extern_type?,
}))
}
Expand Down