Skip to content

Allow non-handle types in generic position via IntoJsGeneric#5167

Open
guybedford wants to merge 1 commit into
wasm-bindgen:mainfrom
guybedford:pr1-from-js-generic
Open

Allow non-handle types in generic position via IntoJsGeneric#5167
guybedford wants to merge 1 commit into
wasm-bindgen:mainfrom
guybedford:pr1-from-js-generic

Conversation

@guybedford

Copy link
Copy Markdown
Contributor

Small extension to the generics traits so imported interfaces can use String, u32, etc. as generic type arguments. Builds on the existing IntoJsGeneric / JsCanon machinery — adds the inverse direction and a handful of leaf impls.

  • Add FromJsGeneric next to IntoJsGeneric.
  • Add ref_to_js to IntoJsGeneric for root-level &T extern args.
  • Weaken IntoJsGeneric::JsCanon bound to ErasableGeneric so container shapes (Option<T::JsCanon>, Vec<T::JsCanon>) typecheck.
  • Add IntoJsGeneric<JsCanon = Self::Canon> + FromJsGeneric + 'static to JsGeneric as supertraits so existing T: JsGeneric bounds keep working unchanged.
  • Generic owned-arg/return codegen routes through to_js / from_js; root-level &T routes through ref_to_js. For identity impls this inlines to the same transmute as before — wasm output for existing JsGeneric callers is unchanged.
  • Leaf impls for String / &str / numeric primitives in the lossless f64 range / bool, with JsCanon = JsValue. () canons to Undefined so closure return positions participate.
  • Option<T>, Vec<T>, Box<[T]> distribute the new traits element-wise.

Reference (&T / &mut T) paths beyond root-level args, and BigInt-range integer types, are out of scope. Refining leaf JsCanon to JsString / Number / Boolean is blocked by the orphan rule today and waits on a future js-sys-into-core consolidation; a note documents this.

Tests: tests/wasm/generics.{rs,js} gains a small Cell fixture with generic getters/setters exercised at T = String, u32, i32, f64, bool, plus a set_value_ref<T>(&T) case covering the root-&T path. Guide updated at guide/src/reference/working-with-generics.md.

@guybedford guybedford force-pushed the pr1-from-js-generic branch 2 times, most recently from 8e36205 to 4d133dc Compare May 26, 2026 00:54
@guybedford guybedford force-pushed the pr1-from-js-generic branch 2 times, most recently from bbdc4a8 to 1351bb9 Compare May 26, 2026 05:42
`String`, `u32`, etc. as generic type arguments. Builds on the existing
`IntoJsGeneric` / `JsCanon` machinery — adds the inverse direction and
a handful of leaf impls.

* Add `FromJsGeneric` next to `IntoJsGeneric`.
* Add `ref_to_js` to `IntoJsGeneric` for root-level `&T` extern args.
* Weaken `IntoJsGeneric::JsCanon` bound to `ErasableGeneric` so container
  shapes (`Option<T::JsCanon>`, `Vec<T::JsCanon>`) typecheck.
* Add `IntoJsGeneric<JsCanon = Self::Canon>` + `FromJsGeneric` + `'static`
  to `JsGeneric` as supertraits so existing `T: JsGeneric` bounds keep
  working unchanged; the doubled `Canon` associated type propagates the
  canon-erasure invariant to use sites without explicit `where` clauses.
* Generic owned-arg/return codegen routes through `to_js` / `from_js`;
  root-level `&T` routes through `ref_to_js`. For identity impls this
  inlines to the same transmute as before — wasm output for existing
  `JsGeneric` callers is unchanged.
* Leaf impls for `String` / `&str` / numeric primitives in the lossless
  f64 range / `bool`, with `JsCanon = JsValue`. `()` canons to `Undefined`
  so closure return positions participate.
* `Option<T>`, `Vec<T>`, `Box<[T]>` distribute the new traits element-wise.

Reference (`&T` / `&mut T`) paths beyond root-level args, and BigInt-range
integer types (`i64` / `u64` / `i128` / `u128` / `isize` / `usize`), are
out of scope here. Refining leaf `JsCanon` to `JsString` / `Number` /
`Boolean` is blocked by the orphan rule today and waits on a future
js-sys-into-core consolidation; a note documents this.

Tests: `tests/wasm/generics.{rs,js}` gains a small `Cell` fixture with
generic getters/setters exercised at `T = String`, `u32`, `i32`, `f64`,
`bool`, plus a `set_value_ref<T>(&T)` case covering the root-`&T` path.
Guide updated at `guide/src/reference/working-with-generics.md` with
the three-trait split and roles table. `cargo build --workspace` clean;
existing UI test baseline unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant