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>
This commit is contained in:
Samson 2024-08-25 15:58:09 +02:00 committed by GitHub
parent 6357998ede
commit 88d8770214
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 15 deletions

View file

@ -38,7 +38,7 @@ pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream {
quote! ( quote! (
#s2 #s2
impl crate::dom::bindings::inheritance::HasParent for #name { impl crate::HasParent for #name {
type Parent = #ty; type Parent = #ty;
/// This is used in a type assertion to ensure that /// This is used in a type assertion to ensure that
/// the source and webidls agree as to what the parent type is /// the source and webidls agree as to what the parent type is

View file

@ -40,19 +40,19 @@ fn expand_dom_object(input: syn::DeriveInput) -> proc_macro2::TokenStream {
unsafe fn to_jsval(&self, unsafe fn to_jsval(&self,
cx: *mut js::jsapi::JSContext, cx: *mut js::jsapi::JSContext,
rval: js::rust::MutableHandleValue) { 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) 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] #[inline]
fn reflector(&self) -> &crate::dom::bindings::reflector::Reflector { fn reflector(&self) -> &crate::Reflector {
self.#first_field_name.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) { unsafe fn init_reflector(&self, obj: *mut js::jsapi::JSObject) {
self.#first_field_name.init_reflector(obj); 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(); let mut generics = input.generics.clone();
generics.params.push(parse_quote!( generics.params.push(parse_quote!(
__T: crate::dom::bindings::reflector::DomObject __T: crate::DomObject
)); ));
let (impl_generics, _, where_clause) = generics.split_for_impl(); let (impl_generics, _, where_clause) = generics.split_for_impl();

View file

@ -96,14 +96,14 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream {
#[allow(dead_code)] #[allow(dead_code)]
struct Invalid0; struct Invalid0;
// forbids JSTraceable // forbids JSTraceable
impl<T> NoTraceOnJSTraceable<Invalid0> for T where T: ?Sized + crate::dom::bindings::trace::JSTraceable {} impl<T> NoTraceOnJSTraceable<Invalid0> for T where T: ?Sized + crate::JSTraceable {}
#[allow(dead_code)] #[allow(dead_code)]
struct Invalid2; struct Invalid2;
// forbids HashMap<JSTraceble, _> // forbids HashMap<JSTraceble, _>
impl<K, V, S> NoTraceOnJSTraceable<Invalid2> for std::collections::HashMap<K, V, S> impl<K, V, S> NoTraceOnJSTraceable<Invalid2> for std::collections::HashMap<K, V, S>
where 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, S: std::hash::BuildHasher,
{ {
} }
@ -114,7 +114,7 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream {
impl<K, V, S> NoTraceOnJSTraceable<Invalid3> for std::collections::HashMap<K, V, S> impl<K, V, S> NoTraceOnJSTraceable<Invalid3> for std::collections::HashMap<K, V, S>
where where
K: std::cmp::Eq + std::hash::Hash, K: std::cmp::Eq + std::hash::Hash,
V: crate::dom::bindings::trace::JSTraceable, V: crate::JSTraceable,
S: std::hash::BuildHasher, S: std::hash::BuildHasher,
{ {
} }
@ -123,7 +123,7 @@ fn assert_not_impl_traceable(ty: &syn::Type) -> proc_macro2::TokenStream {
struct Invalid4; struct Invalid4;
// forbids BTreeMap<_, JSTraceble> // forbids BTreeMap<_, JSTraceble>
impl<K, V> NoTraceOnJSTraceable<Invalid4> for std::collections::BTreeMap<K, V> where impl<K, V> NoTraceOnJSTraceable<Invalid4> for std::collections::BTreeMap<K, V> 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<K, V> NoTraceOnJSTraceable<Invalid5> for std::collections::BTreeMap<K, V> impl<K, V> NoTraceOnJSTraceable<Invalid5> for std::collections::BTreeMap<K, V>
where where
K: std::cmp::Eq + std::hash::Hash, 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; return None;
} else if attr.path().is_ident("custom_trace") { } else if attr.path().is_ident("custom_trace") {
return Some(quote!(<dyn crate::dom::bindings::trace::CustomTraceable>::trace(#binding, tracer);)); return Some(quote!(<dyn crate::CustomTraceable>::trace(#binding, tracer);));
} }
} }
Some(quote!(#binding.trace(tracer);)) Some(quote!(#binding.trace(tracer);))
@ -171,18 +171,18 @@ fn js_traceable_derive(s: synstructure::Structure) -> proc_macro2::TokenStream {
let ident = &param.ident; let ident = &param.ident;
where_clause where_clause
.predicates .predicates
.push(parse_quote!(#ident: crate::dom::bindings::trace::JSTraceable)) .push(parse_quote!(#ident: crate::JSTraceable))
} }
let tokens = quote! { let tokens = quote! {
#asserts #asserts
#[allow(unsafe_code)] #[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] #[inline]
#[allow(unused_variables, unused_imports)] #[allow(unused_variables, unused_imports)]
unsafe fn trace(&self, tracer: *mut js::jsapi::JSTracer) { unsafe fn trace(&self, tracer: *mut js::jsapi::JSTracer) {
use crate::dom::bindings::trace::JSTraceable; use crate::JSTraceable;
match *self { match *self {
#match_body #match_body
} }

View file

@ -53,6 +53,7 @@ pub trait Castable: IDLInterface + DomObject + Sized {
} }
} }
#[allow(missing_docs)]
pub trait HasParent { pub trait HasParent {
type Parent; type Parent;
fn as_parent(&self) -> &Self::Parent; fn as_parent(&self) -> &Self::Parent;

View file

@ -96,3 +96,10 @@ mod window_named_properties;
pub use init::init; pub use init::init;
pub use script_runtime::JSEngineSetup; 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};