diff options
Diffstat (limited to 'components/script/dom/bindings')
-rw-r--r-- | components/script/dom/bindings/conversions.rs | 27 | ||||
-rw-r--r-- | components/script/dom/bindings/inheritance.rs | 34 | ||||
-rw-r--r-- | components/script/dom/bindings/js.rs | 3 |
3 files changed, 37 insertions, 27 deletions
diff --git a/components/script/dom/bindings/conversions.rs b/components/script/dom/bindings/conversions.rs index 710a5715fa4..c8f3b986623 100644 --- a/components/script/dom/bindings/conversions.rs +++ b/components/script/dom/bindings/conversions.rs @@ -34,6 +34,7 @@ use core::nonzero::NonZero; use dom::bindings::error::throw_type_error; +use dom::bindings::inheritance::Castable; use dom::bindings::js::Root; use dom::bindings::num::Finite; use dom::bindings::str::{ByteString, USVString}; @@ -107,32 +108,6 @@ pub trait IDLInterface { fn derives(&'static DOMClass) -> bool; } -/// A trait to hold the cast functions of IDL interfaces that either derive -/// or are derived from other interfaces. -pub trait Castable: IDLInterface + Reflectable + Sized { - /// Check whether a DOM object implements one of its deriving interfaces. - fn is<T>(&self) -> bool where T: DerivedFrom<Self> { - let class = unsafe { - get_dom_class(self.reflector().get_jsobject().get()).unwrap() - }; - T::derives(class) - } - - /// Cast a DOM object upwards to one of the interfaces it derives from. - fn upcast<T>(&self) -> &T where T: Castable, Self: DerivedFrom<T> { - unsafe { mem::transmute(self) } - } - - /// Cast a DOM object downwards to one of the interfaces it might implement. - fn downcast<T>(&self) -> Option<&T> where T: DerivedFrom<Self> { - if self.is::<T>() { - Some(unsafe { mem::transmute(self) }) - } else { - None - } - } -} - /// A trait to mark an IDL interface as deriving from another one. #[rustc_on_unimplemented = "The IDL interface `{Self}` is not derived from `{T}`."] pub trait DerivedFrom<T: Castable>: Castable {} diff --git a/components/script/dom/bindings/inheritance.rs b/components/script/dom/bindings/inheritance.rs new file mode 100644 index 00000000000..5878c44c5f3 --- /dev/null +++ b/components/script/dom/bindings/inheritance.rs @@ -0,0 +1,34 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use dom::bindings::conversions::get_dom_class; +use dom::bindings::conversions::{DerivedFrom, IDLInterface}; +use dom::bindings::utils::Reflectable; +use std::mem; + +/// A trait to hold the cast functions of IDL interfaces that either derive +/// or are derived from other interfaces. +pub trait Castable: IDLInterface + Reflectable + Sized { + /// Check whether a DOM object implements one of its deriving interfaces. + fn is<T>(&self) -> bool where T: DerivedFrom<Self> { + let class = unsafe { + get_dom_class(self.reflector().get_jsobject().get()).unwrap() + }; + T::derives(class) + } + + /// Cast a DOM object upwards to one of the interfaces it derives from. + fn upcast<T>(&self) -> &T where T: Castable, Self: DerivedFrom<T> { + unsafe { mem::transmute(self) } + } + + /// Cast a DOM object downwards to one of the interfaces it might implement. + fn downcast<T>(&self) -> Option<&T> where T: DerivedFrom<Self> { + if self.is::<T>() { + Some(unsafe { mem::transmute(self) }) + } else { + None + } + } +} diff --git a/components/script/dom/bindings/js.rs b/components/script/dom/bindings/js.rs index a6af7680a96..388a8ba81ef 100644 --- a/components/script/dom/bindings/js.rs +++ b/components/script/dom/bindings/js.rs @@ -24,7 +24,8 @@ //! use core::nonzero::NonZero; -use dom::bindings::conversions::{Castable, DerivedFrom}; +use dom::bindings::conversions::DerivedFrom; +use dom::bindings::inheritance::Castable; use dom::bindings::trace::JSTraceable; use dom::bindings::trace::trace_reflector; use dom::bindings::utils::{Reflectable, Reflector}; |