From 88d87702147b296de230c120e636fe97f8466e96 Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:58:09 +0200 Subject: [PATCH] Use global exports from derives (#33169) * pub reexport *Traceable Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * reexport `HasParent` for derives Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * reexport DomObject, Reflector, MutDomObject Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * fmt Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> * Update lib.rs Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> * Update lib.rs Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> * Update lib.rs Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> Signed-off-by: Samson <16504129+sagudev@users.noreply.github.com> --- components/dom_struct/lib.rs | 2 +- components/domobject_derive/lib.rs | 10 +++++----- components/jstraceable_derive/lib.rs | 18 +++++++++--------- components/script/dom/bindings/inheritance.rs | 1 + components/script/lib.rs | 7 +++++++ 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/components/dom_struct/lib.rs b/components/dom_struct/lib.rs index 31b41459ebb..2d9dac7bd1b 100644 --- a/components/dom_struct/lib.rs +++ b/components/dom_struct/lib.rs @@ -38,7 +38,7 @@ pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream { quote! ( #s2 - impl crate::dom::bindings::inheritance::HasParent for #name { + impl crate::HasParent for #name { type Parent = #ty; /// This is used in a type assertion to ensure that /// the source and webidls agree as to what the parent type is diff --git a/components/domobject_derive/lib.rs b/components/domobject_derive/lib.rs index 81c9b89be79..b49b78ffc3e 100644 --- a/components/domobject_derive/lib.rs +++ b/components/domobject_derive/lib.rs @@ -40,19 +40,19 @@ fn expand_dom_object(input: syn::DeriveInput) -> proc_macro2::TokenStream { unsafe fn to_jsval(&self, cx: *mut js::jsapi::JSContext, rval: js::rust::MutableHandleValue) { - let object = crate::dom::bindings::reflector::DomObject::reflector(self).get_jsobject(); + let object = crate::DomObject::reflector(self).get_jsobject(); object.to_jsval(cx, rval) } } - impl #impl_generics crate::dom::bindings::reflector::DomObject for #name #ty_generics #where_clause { + impl #impl_generics crate::DomObject for #name #ty_generics #where_clause { #[inline] - fn reflector(&self) -> &crate::dom::bindings::reflector::Reflector { + fn reflector(&self) -> &crate::Reflector { self.#first_field_name.reflector() } } - impl #impl_generics crate::dom::bindings::reflector::MutDomObject for #name #ty_generics #where_clause { + impl #impl_generics crate::MutDomObject for #name #ty_generics #where_clause { unsafe fn init_reflector(&self, obj: *mut js::jsapi::JSObject) { self.#first_field_name.init_reflector(obj); } @@ -74,7 +74,7 @@ fn expand_dom_object(input: syn::DeriveInput) -> proc_macro2::TokenStream { let mut generics = input.generics.clone(); generics.params.push(parse_quote!( - __T: crate::dom::bindings::reflector::DomObject + __T: crate::DomObject )); let (impl_generics, _, where_clause) = generics.split_for_impl(); diff --git a/components/jstraceable_derive/lib.rs b/components/jstraceable_derive/lib.rs index 34de9c13574..39284d9b7d3 100644 --- a/components/jstraceable_derive/lib.rs +++ b/components/jstraceable_derive/lib.rs @@ -96,14 +96,14 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream { #[allow(dead_code)] struct Invalid0; // forbids JSTraceable - impl NoTraceOnJSTraceable for T where T: ?Sized + crate::dom::bindings::trace::JSTraceable {} + impl NoTraceOnJSTraceable for T where T: ?Sized + crate::JSTraceable {} #[allow(dead_code)] struct Invalid2; // forbids HashMap impl NoTraceOnJSTraceable for std::collections::HashMap where - K: crate::dom::bindings::trace::JSTraceable + std::cmp::Eq + std::hash::Hash, + K: crate::JSTraceable + std::cmp::Eq + std::hash::Hash, S: std::hash::BuildHasher, { } @@ -114,7 +114,7 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream { impl NoTraceOnJSTraceable for std::collections::HashMap where K: std::cmp::Eq + std::hash::Hash, - V: crate::dom::bindings::trace::JSTraceable, + V: crate::JSTraceable, S: std::hash::BuildHasher, { } @@ -123,7 +123,7 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream { struct Invalid4; // forbids BTreeMap<_, JSTraceble> impl NoTraceOnJSTraceable for std::collections::BTreeMap where - K: crate::dom::bindings::trace::JSTraceable + std::cmp::Eq + std::hash::Hash + K: crate::JSTraceable + std::cmp::Eq + std::hash::Hash { } @@ -133,7 +133,7 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream { impl NoTraceOnJSTraceable for std::collections::BTreeMap where K: std::cmp::Eq + std::hash::Hash, - V: crate::dom::bindings::trace::JSTraceable, + V: crate::JSTraceable, { } @@ -157,7 +157,7 @@ fn js_traceable_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { } return None; } else if attr.path().is_ident("custom_trace") { - return Some(quote!(::trace(#binding, tracer);)); + return Some(quote!(::trace(#binding, tracer);)); } } Some(quote!(#binding.trace(tracer);)) @@ -171,18 +171,18 @@ fn js_traceable_derive(s: synstructure::Structure) -> proc_macro2::TokenStream { let ident = ¶m.ident; where_clause .predicates - .push(parse_quote!(#ident: crate::dom::bindings::trace::JSTraceable)) + .push(parse_quote!(#ident: crate::JSTraceable)) } let tokens = quote! { #asserts #[allow(unsafe_code)] - unsafe impl #impl_generics crate::dom::bindings::trace::JSTraceable for #name #ty_generics #where_clause { + unsafe impl #impl_generics crate::JSTraceable for #name #ty_generics #where_clause { #[inline] #[allow(unused_variables, unused_imports)] unsafe fn trace(&self, tracer: *mut js::jsapi::JSTracer) { - use crate::dom::bindings::trace::JSTraceable; + use crate::JSTraceable; match *self { #match_body } diff --git a/components/script/dom/bindings/inheritance.rs b/components/script/dom/bindings/inheritance.rs index 58dafb2972f..6cef7d05a31 100644 --- a/components/script/dom/bindings/inheritance.rs +++ b/components/script/dom/bindings/inheritance.rs @@ -53,6 +53,7 @@ pub trait Castable: IDLInterface + DomObject + Sized { } } +#[allow(missing_docs)] pub trait HasParent { type Parent; fn as_parent(&self) -> &Self::Parent; diff --git a/components/script/lib.rs b/components/script/lib.rs index b145e83227a..534e1917194 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -96,3 +96,10 @@ mod window_named_properties; pub use init::init; pub use script_runtime::JSEngineSetup; + +// These trait exports are public, because they are used in the DOM bindings. +// Since they are used in derive macros, +// it is useful that they are accessible at the root of the crate. +pub use crate::dom::bindings::inheritance::HasParent; +pub use crate::dom::bindings::reflector::{DomObject, MutDomObject, Reflector}; +pub use crate::dom::bindings::trace::{CustomTraceable, JSTraceable};