Skip to content

Commit

Permalink
Merge #2719
Browse files Browse the repository at this point in the history
2719: Fix wasm_importtype_new's Rust signature to not assume boxed vectors r=ptitSeb a=Amanieu

Fixes #2718

Co-authored-by: Amanieu d'Antras <[email protected]>
  • Loading branch information
bors[bot] and Amanieu authored Dec 13, 2021
2 parents 60c58af + 0f96392 commit f839af0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
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.
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

0 comments on commit f839af0

Please sign in to comment.