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

Generalise RootedVec::<JS<T>>::r as [JS<T>]::r #11171

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Generalise RootedReference

It now becomes RootedReference<'root> and includes an associated type for
the return type of its 'r' method.

This removes the need for OptionalRootedReference.
  • Loading branch information
nox committed May 16, 2016
commit a8a4054c4333a43b1d0c1601a5d25f245e053851
@@ -6,6 +6,7 @@

use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::global_root_from_object;
use dom::bindings::js::RootedReference;
use dom::bindings::reflector::Reflectable;
use js::jsapi::GetGlobalForObjectCrossCompartment;
use js::jsapi::JSAutoCompartment;
@@ -5569,7 +5569,6 @@ def __init__(self, config, prefix, webIDLFile):
'dom::bindings::interface::{ConstantSpec, NonNullJSNative}',
'dom::bindings::interface::ConstantVal::{IntVal, UintVal}',
'dom::bindings::js::{JS, Root, RootedReference}',
'dom::bindings::js::{OptionalRootedReference}',
'dom::bindings::reflector::{Reflectable}',
'dom::bindings::utils::{DOMClass, DOMJSClass, Prefable}',
'dom::bindings::utils::{DOM_PROTO_UNFORGEABLE_HOLDER_SLOT, JSCLASS_DOM_GLOBAL}',
@@ -10,7 +10,7 @@
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::conversions::root_from_object;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::window::{self, ScriptHelpers};
use dom::workerglobalscope::WorkerGlobalScope;
@@ -265,10 +265,9 @@ impl<'a> GlobalRef<'a> {
}
}

impl GlobalRoot {
/// Obtain a safe reference to the global object that cannot outlive the
/// lifetime of this root.
pub fn r(&self) -> GlobalRef {
impl<'root> RootedReference<'root> for GlobalRoot {
type Ref = GlobalRef<'root>;
fn r(&'root self) -> GlobalRef<'root> {
match *self {
GlobalRoot::Window(ref window) => GlobalRef::Window(window.r()),
GlobalRoot::Worker(ref worker) => GlobalRef::Worker(worker.r()),
@@ -94,6 +94,13 @@ impl<T: Reflectable> JS<T> {
}
}

impl<'root, T: Reflectable + 'root> RootedReference<'root> for JS<T> {
type Ref = &'root T;
fn r(&'root self) -> &'root T {
&self
}
}

impl<T: Reflectable> Deref for JS<T> {
type Target = T;

@@ -441,36 +448,18 @@ impl<T: Reflectable> LayoutJS<T> {
}
}

/// Get an `Option<&T>` out of an `Option<Root<T>>`
pub trait RootedReference<T> {
/// Obtain a safe optional reference to the wrapped JS owned-value that
/// cannot outlive the lifetime of this root.
fn r(&self) -> Option<&T>;
}

impl<T: Reflectable> RootedReference<T> for Option<Root<T>> {
fn r(&self) -> Option<&T> {
self.as_ref().map(|root| root.r())
}
}

/// Get an `Option<&T> out of an `Option<JS<T>>`
impl<T: Reflectable> RootedReference<T> for Option<JS<T>> {
fn r(&self) -> Option<&T> {
self.as_ref().map(|inner| &**inner)
}
}

/// Get an `Option<Option<&T>>` out of an `Option<Option<Root<T>>>`
pub trait OptionalRootedReference<T> {
/// Obtain a safe optional optional reference to the wrapped JS owned-value
/// that cannot outlive the lifetime of this root.
fn r(&self) -> Option<Option<&T>>;
/// 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<T: Reflectable> OptionalRootedReference<T> for Option<Option<Root<T>>> {
fn r(&self) -> Option<Option<&T>> {
self.as_ref().map(|inner| inner.r())
impl<'root, T: RootedReference<'root> + 'root> RootedReference<'root> for Option<T> {
type Ref = Option<T::Ref>;
fn r(&'root self) -> Option<T::Ref> {
self.as_ref().map(RootedReference::r)
}
}

@@ -595,11 +584,12 @@ impl<T: Reflectable> Root<T> {
pub fn from_ref(unrooted: &T) -> Root<T> {
Root::new(unsafe { NonZero::new(&*unrooted) })
}
}

/// Obtain a safe reference to the wrapped JS owned-value that cannot
/// outlive the lifetime of this root.
pub fn r(&self) -> &T {
&**self
impl<'root, T: Reflectable + 'root> RootedReference<'root> for Root<T> {
type Ref = &'root T;
fn r(&'root self) -> &'root T {
&self
}
}

@@ -23,7 +23,7 @@
//! is removed.

use core::nonzero::NonZero;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::bindings::trace::trace_reflector;
use js::jsapi::JSTracer;
@@ -34,7 +34,7 @@ use canvas_traits::{CompositionOrBlending, LineCapStyle, LineJoinStyle, Repetiti
use cssparser::RGBA;
use devtools_traits::CSSError;
use devtools_traits::WorkerId;
use dom::bindings::js::{JS, Root};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::bindings::utils::WindowProxyHandler;
@@ -509,9 +509,9 @@ impl<T: JSTraceable> RootedVec<T> {
}
}

impl<T: JSTraceable + Reflectable> RootedVec<JS<T>> {
/// Obtain a safe slice of references that can't outlive that RootedVec.
pub fn r(&self) -> &[&T] {
impl<'root, T: JSTraceable + Reflectable + 'root> RootedReference<'root> for RootedVec<JS<T>> {
type Ref = &'root [&'root T];
fn r(&'root self) -> &'root [&'root T] {
unsafe { mem::transmute(&self.v[..]) }
}
}
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use dom::bindings::codegen::UnionTypes::BlobOrString;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use encoding::all::UTF_8;
use encoding::types::{EncoderTrap, Encoding};
@@ -9,7 +9,7 @@ use dom::bindings::codegen::Bindings::BluetoothBinding::{BluetoothScanFilter, Bl
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothdevice::BluetoothDevice;
@@ -5,7 +5,7 @@
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
use dom::bindings::js::{JS, MutHeap, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
@@ -12,7 +12,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::{Network, Type};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
@@ -13,7 +13,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::{Type, Network};
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::ByteString;
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
@@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::Bluetoot
use dom::bindings::error::Error::Type;
use dom::bindings::error::{Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothdevice::BluetoothDevice;
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::Bluetoo
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::js::{JS, MutHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bluetoothcharacteristicproperties::BluetoothCharacteristicProperties;
use dom::bluetoothdevice::BluetoothDevice;
@@ -22,7 +22,7 @@ use dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern;
use dom::bindings::error::{Error, Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, Root};
use dom::bindings::js::{JS, LayoutJS, Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle};
@@ -7,7 +7,7 @@ use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::ConsoleBinding;
use dom::bindings::codegen::Bindings::ConsoleBinding::ConsoleMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use std::collections::HashMap;
use time::{Timespec, get_time};
@@ -11,7 +11,7 @@ use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::error::ErrorResult;
use dom::bindings::global::{GlobalRef, global_root_from_context};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{Root, RootCollection};
use dom::bindings::js::{Root, RootCollection, RootedReference};
use dom::bindings::refcounted::LiveDOMReferences;
use dom::bindings::reflector::Reflectable;
use dom::bindings::structuredclone::StructuredCloneData;
@@ -23,8 +23,7 @@ use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::RootedReference;
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root};
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::DOMQuadBinding::{DOMQuadInit, DOMQuadMetho
use dom::bindings::codegen::Bindings::DOMRectReadOnlyBinding::{DOMRectInit};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{Root, JS};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::dompoint::DOMPoint;
use dom::domrect::DOMRect;
@@ -14,7 +14,7 @@ use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::UnionTypes::EventOrString;
use dom::bindings::error::{Error, Fallible, report_pending_exception};
use dom::bindings::inheritance::{Castable, EventTargetTypeId};
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::element::Element;
use dom::errorevent::ErrorEvent;
@@ -9,7 +9,7 @@ use dom::bindings::codegen::Bindings::FileReaderBinding::{self, FileReaderConsta
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::blob::{Blob, DataSlice};
@@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::FormDataBinding::FormDataMethods;
use dom::bindings::codegen::UnionTypes::BlobOrUSVString;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::USVString;
use dom::blob::Blob;
@@ -6,7 +6,7 @@ use dom::attr::Attr;
use dom::bindings::codegen::Bindings::HTMLFieldSetElementBinding;
use dom::bindings::codegen::Bindings::HTMLFieldSetElementBinding::HTMLFieldSetElementMethods;
use dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::document::Document;
use dom::element::{AttributeMutation, Element};
use dom::htmlcollection::{CollectionFilter, HTMLCollection};
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::HTMLFormControlsCollectionBinding;
use dom::bindings::codegen::Bindings::HTMLFormControlsCollectionBinding::HTMLFormControlsCollectionMethods;
use dom::bindings::codegen::UnionTypes::RadioNodeListOrElement;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::element::Element;
use dom::htmlcollection::{CollectionFilter, HTMLCollection};
@@ -5,7 +5,7 @@
use dom::bindings::codegen::Bindings::NavigatorBinding;
use dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflector, Reflectable, reflect_dom_object};
use dom::bluetooth::Bluetooth;
use dom::mimetypearray::MimeTypeArray;
@@ -9,7 +9,7 @@ use document_loader::LoadType;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::ServoHTMLParserBinding;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::trace::JSTraceable;
@@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::StorageBinding::StorageMethods;
use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::browsingcontext::IterableContext;
@@ -17,7 +17,7 @@ use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSeq
use dom::bindings::codegen::UnionTypes::{StringOrUnsignedLong, StringOrBoolean, UnsignedLongOrBoolean};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::{ByteString, USVString};
@@ -6,7 +6,7 @@ use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::URLBinding::{self, URLMethods};
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::str::USVString;
use dom::urlhelper::UrlHelper;
@@ -7,7 +7,7 @@ use canvas_traits::CanvasMsg;
use dom::bindings::codegen::Bindings::WebGLProgramBinding;
use dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants as constants;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::webglactiveinfo::WebGLActiveInfo;
use dom::webglobject::WebGLObject;
@@ -11,7 +11,7 @@ use dom::bindings::conversions::{ToJSValConvertible, array_buffer_view_data, arr
use dom::bindings::conversions::{array_buffer_view_to_vec_checked, array_buffer_view_to_vec};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root};
use dom::bindings::js::{JS, LayoutJS, MutNullableHeap, Root, RootedReference};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::htmlcanvaselement::HTMLCanvasElement;
@@ -12,7 +12,7 @@ use dom::bindings::conversions::{ToJSValConvertible};
use dom::bindings::error::{Error, Fallible, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::bindings::str::{USVString, is_token};
@@ -14,7 +14,7 @@ use dom::bindings::codegen::Bindings::WindowBinding::{self, FrameRequestCallback
use dom::bindings::error::{Error, Fallible, report_pending_exception};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::num::Finite;
use dom::bindings::reflector::Reflectable;
use dom::bindings::utils::{GlobalStaticData, WindowProxyHandler};
@@ -9,7 +9,7 @@ use dom::bindings::codegen::Bindings::WorkerBinding::WorkerMethods;
use dom::bindings::error::{Error, ErrorResult, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::bindings::structuredclone::StructuredCloneData;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.