diff options
author | Manish Goregaokar <manishsmail@gmail.com> | 2018-06-29 10:34:09 -0700 |
---|---|---|
committer | Manish Goregaokar <manishsmail@gmail.com> | 2018-07-03 09:39:29 -0700 |
commit | ad198993b127ef87f129add8336f8bb7dfd30e4d (patch) | |
tree | db1756c2241abbd068d81e4b07a4d481a55b5c73 /components/dom_struct/lib.rs | |
parent | e2fca1b228ff20d54b67e0d1fa502b694b5c290e (diff) | |
download | servo-ad198993b127ef87f129add8336f8bb7dfd30e4d.tar.gz servo-ad198993b127ef87f129add8336f8bb7dfd30e4d.zip |
Assert that DOM structs have the correct first field
DOM structs embed their parent type as their first field. This
introduces a `.parent()` method to the DOM struct that returns its first
field, and codegens a type assert that ensures that `.parent()` returns
the parent struct.
This generates:
On `#[dom_struct]`:
```rust
impl HasParent for Type {
type Parent = ParentType;
fn as_parent(&self) -> ParentType {
&self.first_field
}
}
```
In the codegen files:
```rust
impl Type {
fn __assert_parent_type(&self) {
let _: &ParentType = self.as_parent();
}
}
````
Diffstat (limited to 'components/dom_struct/lib.rs')
-rw-r--r-- | components/dom_struct/lib.rs | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/components/dom_struct/lib.rs b/components/dom_struct/lib.rs index 8eacafddeaf..a9d9204c534 100644 --- a/components/dom_struct/lib.rs +++ b/components/dom_struct/lib.rs @@ -7,7 +7,12 @@ extern crate proc_macro; -use proc_macro::{TokenStream, quote}; +#[macro_use] +extern crate quote; +extern crate syn; + +use proc_macro::TokenStream; +use syn::*; #[proc_macro_attribute] pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream { @@ -23,5 +28,38 @@ pub fn dom_struct(args: TokenStream, input: TokenStream) -> TokenStream { // Work around https://github.com/rust-lang/rust/issues/46489 let attributes: TokenStream = attributes.to_string().parse().unwrap(); - attributes.into_iter().chain(input.into_iter()).collect() + + let output: TokenStream = attributes.into_iter().chain(input.into_iter()).collect(); + + let item: Item = syn::parse(output).unwrap(); + + if let Item::Struct(s) = item { + let s2 = s.clone(); + if s.generics.params.len() > 0 { + return quote!(#s2).into(); + } + if let Fields::Named(ref f) = s.fields { + let f = f.named.first().expect("Must have at least one field").into_value(); + let ident = f.ident.as_ref().expect("Must have named fields"); + let name = &s.ident; + let ty = &f.ty; + + quote! ( + #s2 + + impl ::dom::bindings::inheritance::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 + fn as_parent(&self) -> &#ty { + &self.#ident + } + } + ).into() + } else { + panic!("#[dom_struct] only applies to structs with named fields"); + } + } else { + panic!("#[dom_struct] only applies to structs"); + } } |