mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
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.
This commit is contained in:
parent
0b3ab875f4
commit
45c9aa7487
3 changed files with 32 additions and 49 deletions
|
@ -5505,9 +5505,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
|
|||
'dom::bindings::iterable::Iterable',
|
||||
'dom::bindings::iterable::IteratorType',
|
||||
'dom::bindings::js::JS',
|
||||
'dom::bindings::js::OptionalRootedReference',
|
||||
'dom::bindings::js::Root',
|
||||
'dom::bindings::js::RootedRcReference',
|
||||
'dom::bindings::js::RootedReference',
|
||||
'dom::bindings::namespace::NamespaceObjectClass',
|
||||
'dom::bindings::namespace::create_namespace_object',
|
||||
|
|
|
@ -87,6 +87,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;
|
||||
|
||||
|
@ -446,54 +453,25 @@ impl<T: Reflectable> LayoutJS<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Get an `&T` out of a `Rc<T>`
|
||||
pub trait RootedRcReference<T> {
|
||||
/// Obtain a safe reference to the wrapped non-JS owned value.
|
||||
fn r(&self) -> &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> RootedRcReference<T> for Rc<T> {
|
||||
fn r(&self) -> &T {
|
||||
&*self
|
||||
impl<'root, T: Reflectable + 'root> RootedReference<'root> for Rc<T> {
|
||||
type Ref = &'root T;
|
||||
fn r(&'root self) -> &'root T {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<Rc<T>> {
|
||||
fn r(&self) -> Option<&T> {
|
||||
self.as_ref().map(|root| &**root)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> RootedReference<T> for Option<Root<T>> {
|
||||
fn r(&self) -> Option<&T> {
|
||||
self.as_ref().map(|root| &**root)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>>;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -617,6 +595,13 @@ impl<T: Reflectable> Root<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'root, T: Reflectable + 'root> RootedReference<'root> for Root<T> {
|
||||
type Ref = &'root T;
|
||||
fn r(&'root self) -> &'root T {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Reflectable> Deref for Root<T> {
|
||||
type Target = T;
|
||||
fn deref(&self) -> &T {
|
||||
|
|
|
@ -36,7 +36,7 @@ use devtools_traits::CSSError;
|
|||
use devtools_traits::WorkerId;
|
||||
use dom::abstractworker::SharedRt;
|
||||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::js::{JS, Root};
|
||||
use dom::bindings::js::{JS, Root, RootedReference};
|
||||
use dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||
use dom::bindings::reflector::{Reflectable, Reflector};
|
||||
use dom::bindings::str::{DOMString, USVString};
|
||||
|
@ -553,9 +553,9 @@ impl<'a, T: JSTraceable + Reflectable> RootedVec<'a, JS<T>> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, T: JSTraceable + Reflectable> RootedVec<'a, JS<T>> {
|
||||
/// Obtain a safe slice of references that can't outlive that RootedVec.
|
||||
pub fn r(&self) -> &[&T] {
|
||||
impl<'a, 'root, T: JSTraceable + Reflectable + 'root> RootedReference<'root> for RootedVec<'a, JS<T>> {
|
||||
type Ref = &'root [&'root T];
|
||||
fn r(&'root self) -> &'root [&'root T] {
|
||||
unsafe { mem::transmute(&self[..]) }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue