Skip to content

Commit

Permalink
Simplify RootedReference and make it specifically about slicesIt's no…
Browse files Browse the repository at this point in the history
…w called DomSlice<T>.
  • Loading branch information
nox committed Mar 11, 2019
1 parent 1744a42 commit 4d527b2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -5899,7 +5899,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'crate::dom::bindings::root::Dom',
'crate::dom::bindings::root::DomRoot',
'crate::dom::bindings::root::OptionalHeapSetter',
'crate::dom::bindings::root::RootedReference',
'crate::dom::bindings::root::DomSlice',
'crate::dom::bindings::utils::AsVoidPtr',
'crate::dom::bindings::utils::DOMClass',
'crate::dom::bindings::utils::DOMJSClass',
Expand Down
29 changes: 17 additions & 12 deletions components/script/dom/bindings/root.rs
Expand Up @@ -256,18 +256,23 @@ pub unsafe fn trace_roots(tracer: *mut JSTracer) {
});
}

/// Get a reference out of a rooted value.
pub trait RootedReference<'root> {
/// The type of the reference.
type Ref: 'root;
/// Obtain a reference out of the rooted value.
fn r(&'root self) -> Self::Ref;
}

impl<'root, T: JSTraceable + DomObject + 'root> RootedReference<'root> for [Dom<T>] {
type Ref = &'root [&'root T];
fn r(&'root self) -> &'root [&'root T] {
unsafe { mem::transmute(self) }
/// Get a slice of references to DOM objects.
pub trait DomSlice<T>
where
T: JSTraceable + DomObject,
{
/// Returns the slice of `T` references.
fn r(&self) -> &[&T];
}

impl<T> DomSlice<T> for [Dom<T>]
where
T: JSTraceable + DomObject,
{
#[inline]
fn r(&self) -> &[&T] {
let _ = mem::transmute::<Dom<T>, &T>;
unsafe { &*(self as *const [Dom<T>] as *const [&T]) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/document.rs
Expand Up @@ -29,7 +29,7 @@ use crate::dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementType
use crate::dom::bindings::num::Finite;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom, RootedReference};
use crate::dom::bindings::root::{Dom, DomRoot, DomSlice, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::xmlname::XMLName::InvalidXMLName;
use crate::dom::bindings::xmlname::{
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/event.rs
Expand Up @@ -10,7 +10,7 @@ use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{DomRoot, MutNullableDom, RootedReference};
use crate::dom::bindings::root::{DomRoot, DomSlice, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::document::Document;
use crate::dom::eventtarget::{CompiledEventListener, EventTarget, ListenerPhase};
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/gamepadbuttonlist.rs
Expand Up @@ -5,7 +5,7 @@
use crate::dom::bindings::codegen::Bindings::GamepadButtonListBinding;
use crate::dom::bindings::codegen::Bindings::GamepadButtonListBinding::GamepadButtonListMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot, RootedReference};
use crate::dom::bindings::root::{Dom, DomRoot, DomSlice};
use crate::dom::gamepadbutton::GamepadButton;
use crate::dom::globalscope::GlobalScope;
use dom_struct::dom_struct;
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/node.rs
Expand Up @@ -21,7 +21,7 @@ use crate::dom::bindings::inheritance::{Castable, CharacterDataTypeId, ElementTy
use crate::dom::bindings::inheritance::{EventTargetTypeId, HTMLElementTypeId, NodeTypeId};
use crate::dom::bindings::inheritance::{SVGElementTypeId, SVGGraphicsElementTypeId};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom, MutNullableDom, RootedReference};
use crate::dom::bindings::root::{Dom, DomRoot, DomSlice, LayoutDom, MutNullableDom};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::xmlname::namespace_from_domstring;
use crate::dom::characterdata::{CharacterData, LayoutCharacterDataHelpers};
Expand Down

0 comments on commit 4d527b2

Please sign in to comment.