Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce trait Castable #8041

Merged
merged 5 commits into from Oct 21, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Prev

Document the new inheritance machinery (fixes #8125)

  • Loading branch information
nox committed Oct 21, 2015
commit d0e022b64e7b708e6fbe7348526c49a3665361ba
@@ -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(_)) => ...,
//! ...,
//! }
//! ```
//!
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.