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();
    }
}
````
This commit is contained in:
Manish Goregaokar 2018-06-29 10:34:09 -07:00
parent e2fca1b228
commit ad198993b1
6 changed files with 109 additions and 9 deletions

View file

@ -41,3 +41,8 @@ pub trait Castable: IDLInterface + DomObject + Sized {
}
}
}
pub trait HasParent {
type Parent;
fn as_parent(&self) -> &Self::Parent;
}