Skip to content

Commit df8da56

Browse files
committed
Add PartialEq/Eq to many js-sys types
This commit adds `#[derive(PartialEq, Eq)]` to many types throughout `js-sys`. These types are basically all based on `Object`, which means that `Object.is` can be used for `PartialEq` and the `Eq` requirements are upheld. The macro has also been updated to internally store the deref target instead of unconditionally storing `JsValue`, allowing `#[derive]` to work a bit better in these situations.
1 parent 018b9b4 commit df8da56

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

crates/backend/src/codegen.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,24 @@ impl ToTokens for ast::ImportType {
578578
let const_name = format!("__wbg_generated_const_{}", rust_name);
579579
let const_name = Ident::new(&const_name, Span::call_site());
580580
let instanceof_shim = Ident::new(&self.instanceof_shim, Span::call_site());
581+
582+
let internal_obj = match self.extends.first() {
583+
Some(target) => {
584+
quote! { #target }
585+
}
586+
None => {
587+
quote! { wasm_bindgen::JsValue }
588+
}
589+
};
590+
581591
(quote! {
582592
#[allow(bad_style)]
583593
#(#attrs)*
584594
#[doc = #doc_comment]
585595
#[repr(transparent)]
586596
#[allow(clippy::all)]
587597
#vis struct #rust_name {
588-
obj: wasm_bindgen::JsValue,
598+
obj: #internal_obj
589599
}
590600

591601
#[allow(bad_style)]
@@ -604,6 +614,15 @@ impl ToTokens for ast::ImportType {
604614
}
605615
}
606616

617+
impl core::ops::Deref for #rust_name {
618+
type Target = #internal_obj;
619+
620+
#[inline]
621+
fn deref(&self) -> &#internal_obj {
622+
&self.obj
623+
}
624+
}
625+
607626
impl IntoWasmAbi for #rust_name {
608627
type Abi = <JsValue as IntoWasmAbi>::Abi;
609628

@@ -629,7 +648,7 @@ impl ToTokens for ast::ImportType {
629648
#[inline]
630649
unsafe fn from_abi(js: Self::Abi, extra: &mut Stack) -> Self {
631650
#rust_name {
632-
obj: JsValue::from_abi(js, extra),
651+
obj: JsValue::from_abi(js, extra).into(),
633652
}
634653
}
635654
}
@@ -656,7 +675,7 @@ impl ToTokens for ast::ImportType {
656675
unsafe fn ref_from_abi(js: Self::Abi, extra: &mut Stack) -> Self::Anchor {
657676
let tmp = <JsValue as RefFromWasmAbi>::ref_from_abi(js, extra);
658677
core::mem::ManuallyDrop::new(#rust_name {
659-
obj: core::mem::ManuallyDrop::into_inner(tmp),
678+
obj: core::mem::ManuallyDrop::into_inner(tmp).into(),
660679
})
661680
}
662681
}
@@ -665,20 +684,20 @@ impl ToTokens for ast::ImportType {
665684
impl From<JsValue> for #rust_name {
666685
#[inline]
667686
fn from(obj: JsValue) -> #rust_name {
668-
#rust_name { obj }
687+
#rust_name { obj: obj.into() }
669688
}
670689
}
671690

672691
impl AsRef<JsValue> for #rust_name {
673692
#[inline]
674-
fn as_ref(&self) -> &JsValue { &self.obj }
693+
fn as_ref(&self) -> &JsValue { self.obj.as_ref() }
675694
}
676695

677696

678697
impl From<#rust_name> for JsValue {
679698
#[inline]
680699
fn from(obj: #rust_name) -> JsValue {
681-
obj.obj
700+
obj.obj.into()
682701
}
683702
}
684703

@@ -703,7 +722,7 @@ impl ToTokens for ast::ImportType {
703722

704723
#[inline]
705724
fn unchecked_from_js(val: JsValue) -> Self {
706-
#rust_name { obj: val }
725+
#rust_name { obj: val.into() }
707726
}
708727

709728
#[inline]
@@ -719,22 +738,6 @@ impl ToTokens for ast::ImportType {
719738
})
720739
.to_tokens(tokens);
721740

722-
let deref_target = match self.extends.first() {
723-
Some(target) => quote! { #target },
724-
None => quote! { JsValue },
725-
};
726-
(quote! {
727-
#[allow(clippy::all)]
728-
impl core::ops::Deref for #rust_name {
729-
type Target = #deref_target;
730-
731-
#[inline]
732-
fn deref(&self) -> &#deref_target {
733-
self.as_ref()
734-
}
735-
}
736-
})
737-
.to_tokens(tokens);
738741
for superclass in self.extends.iter() {
739742
(quote! {
740743
#[allow(clippy::all)]

0 commit comments

Comments
 (0)