Skip to content

Commit 504dcd1

Browse files
authored
Disallow the value 0 in from_handle methods in the Rust bindings. (#1318)
The canonical ABI for its part [guarantees] the value 0 will not be used as a table index. Should users of the Rust bindings be able to construct handle types with the value 0? If we say yes, then handle types act as if they have an internal `Option` type that gets implicitly unwrapped when passed to an import. However, in this PR I propose to say no, and prohibit zero in handle types. It seems tidier to say that code depending on optionality should explicitly use `Option` for itself, which will then help avoid traping, by construction. And even if we can't do niche optimizations today due to the `AtomicU32`, it is theoretically possible we could do something with `NonZeroU32` in the future. [guarantees]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md#table-state
1 parent a9d8e9a commit 504dcd1

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

crates/guest-rust/src/pre_wit_bindgen_0_20_0.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub unsafe trait RustResource: WasmResource {
147147
impl<T: WasmResource> Resource<T> {
148148
#[doc(hidden)]
149149
pub unsafe fn from_handle(handle: u32) -> Self {
150-
debug_assert!(handle != u32::MAX);
150+
debug_assert!(handle != 0 && handle != u32::MAX);
151151
Self {
152152
handle: AtomicU32::new(handle),
153153
_marker: marker::PhantomData,

crates/rust/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ pub unsafe trait WasmResource {
664664
impl<T: WasmResource> Resource<T> {
665665
#[doc(hidden)]
666666
pub unsafe fn from_handle(handle: u32) -> Self {
667-
debug_assert!(handle != u32::MAX);
667+
debug_assert!(handle != 0 && handle != u32::MAX);
668668
Self {
669669
handle: AtomicU32::new(handle),
670670
_marker: marker::PhantomData,

0 commit comments

Comments
 (0)