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:
Anthony Ramine 2016-05-13 14:23:09 +02:00
parent 0b3ab875f4
commit 45c9aa7487
3 changed files with 32 additions and 49 deletions

View file

@ -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 {