Question about ABI in WASM

When developing WASM in Rust, we could use #[repr(C)] to handle structures like enum and struct to have C-like layouts. We could also use std::ffi::CString to have C-like strings.

For other common Rust types, like Option, Result, Vec, and String, we use #![feature(wasm_abi)] to handle them. But the ABI of these types seems not consistent in different Rust versions.We've tried using different nightly versions of Rust and have different layouts.

Do we have any way to have consistent ABI across different Rust versions? Thanks!

Option, Result, Vec and String don't have a stable layout. (except for Option<&T>, Option<&mut T>, Option<NonNull<T>> and Option<NonZero*>) You will need to define your own types with a stable layout and convert between them.


Nope. Types that don't have a #[repr(...)] attribute explicitly don't infer any guarantees around ABI stability at all.

If you want a stable ABI, look into something like the abi_stable crate which provides #[repr(C)] adapters.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.