Allow non-handle types in generic position via IntoJsGeneric#5167
Open
guybedford wants to merge 1 commit into
Open
Allow non-handle types in generic position via IntoJsGeneric#5167guybedford wants to merge 1 commit into
guybedford wants to merge 1 commit into
Conversation
8e36205 to
4d133dc
Compare
bbdc4a8 to
1351bb9
Compare
`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.
0808892 to
29e1bb8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Small extension to the generics traits so imported interfaces can use
String,u32, etc. as generic type arguments. Builds on the existingIntoJsGeneric/JsCanonmachinery — adds the inverse direction and a handful of leaf impls.FromJsGenericnext toIntoJsGeneric.ref_to_jstoIntoJsGenericfor root-level&Textern args.IntoJsGeneric::JsCanonbound toErasableGenericso container shapes (Option<T::JsCanon>,Vec<T::JsCanon>) typecheck.IntoJsGeneric<JsCanon = Self::Canon>+FromJsGeneric+'statictoJsGenericas supertraits so existingT: JsGenericbounds keep working unchanged.to_js/from_js; root-level&Troutes throughref_to_js. For identity impls this inlines to the same transmute as before — wasm output for existingJsGenericcallers is unchanged.String/&str/ numeric primitives in the lossless f64 range /bool, withJsCanon = JsValue.()canons toUndefinedso 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 leafJsCanontoJsString/Number/Booleanis 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 smallCellfixture with generic getters/setters exercised atT = String,u32,i32,f64,bool, plus aset_value_ref<T>(&T)case covering the root-&Tpath. Guide updated atguide/src/reference/working-with-generics.md.