diff options
author | Anthony Ramine <n.oxyde@gmail.com> | 2015-10-21 13:11:15 +0200 |
---|---|---|
committer | Anthony Ramine <n.oxyde@gmail.com> | 2015-10-21 13:12:45 +0200 |
commit | d0e022b64e7b708e6fbe7348526c49a3665361ba (patch) | |
tree | 23e6d8980f3d33a9aceb41a503ea6cdd339d7cba /components/script/dom/mod.rs | |
parent | 68014af78e8e3f5de4df0f6cc4d63b99c77478f5 (diff) | |
download | servo-d0e022b64e7b708e6fbe7348526c49a3665361ba.tar.gz servo-d0e022b64e7b708e6fbe7348526c49a3665361ba.zip |
Document the new inheritance machinery (fixes #8125)
Diffstat (limited to 'components/script/dom/mod.rs')
-rw-r--r-- | components/script/dom/mod.rs | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index a61ddd76c2c..ae4524cd05e 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -54,21 +54,39 @@ //! This invariant is enforced by the lint in //! `plugins::lints::inheritance_integrity`. //! -//! The same principle applies to typeids, -//! the derived type enum should -//! use one addititional type (the parent class) because sometimes the parent -//! can be the most-derived class of an object. +//! Interfaces which either derive from or are derived by other interfaces +//! implement the `Castable` trait, which provides three methods `is::<T>()`, +//! `downcast::<T>()` and `upcast::<T>()` to cast across the type hierarchy +//! and check whether a given instance is of a given type. //! //! ```ignore -//! pub enum EventTypeId { -//! UIEvent(UIEventTypeId), -//! //others events +//! use dom::bindings::conversions::Castable; +//! use dom::element::Element; +//! use dom::htmlelement::HTMLElement; +//! use dom::htmlinputelement::HTMLInputElement; +//! +//! if let Some(elem) = node.downcast::<Element> { +//! if elem.is::<HTMLInputElement>() { +//! return elem.upcast::<HTMLElement>(); +//! } //! } +//! ``` +//! +//! Furthermore, when discriminating a given instance against multiple +//! interface types, code generation provides a convenient TypeId enum +//! which can be used to write `match` expressions instead of multiple +//! calls to `Castable::is::<T>`. The `type_id()` method of an instance is +//! provided by the farthest interface it derives from, e.g. `EventTarget` +//! for `HTMLMediaElement`. For convenience, that method is also provided +//! on the `Node` interface to avoid unnecessary upcasts to `EventTarget`. +//! +//! ```ignore +//! use dom::bindings::codegen::InheritTypes::{EventTargetTypeId, NodeTypeId}; //! -//! pub enum UIEventTypeId { -//! MouseEvent, -//! KeyboardEvent, -//! UIEvent, //<- parent of MouseEvent and KeyboardEvent +//! match *node.type_id() { +//! EventTargetTypeId::Node(NodeTypeId::CharacterData(_)) => ..., +//! EventTargetTypeId::Node(NodeTypeId::Element(_)) => ..., +//! ..., //! } //! ``` //! |