Auto merge of #9664 - danlrobertson:i9654, r=KiChjang

Implement RootedReference<T> for Option<JS<T>>

A basic implementation of `RootedReference for Option<JS<T>>` based off of
other implementations of `RootedReference` for `Option` wrapped types.

Really I just wanted an excuse to read more in `bindings` 😄 Let me know
if you have any comments or critiques.

Fixes  #9654

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.svg" height="40" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9664)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-02-17 17:54:15 +05:30
commit ab381cf951
3 changed files with 10 additions and 3 deletions

View file

@ -444,6 +444,13 @@ impl<T: Reflectable> RootedReference<T> for Option<Root<T>> {
}
}
/// 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

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::conversions::{ToJSValConvertible, root_from_handleobject};
use dom::bindings::js::{JS, Root};
use dom::bindings::js::{JS, Root, RootedReference};
use dom::bindings::proxyhandler::{fill_property_descriptor, get_property_descriptor};
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::bindings::utils::WindowProxyHandler;
@ -78,7 +78,7 @@ impl BrowsingContext {
}
pub fn frame_element(&self) -> Option<&Element> {
self.frame_element.as_ref().map(|element| &**element)
self.frame_element.r()
}
pub fn window_proxy(&self) -> *mut JSObject {

View file

@ -812,7 +812,7 @@ impl Activatable for HTMLInputElement {
// We want to restore state only if the element had been changed in the first place
if cache.was_mutable {
let name = self.get_radio_group_name();
match cache.checked_radio.as_ref().map(|t| &*t) {
match cache.checked_radio.r() {
Some(o) => {
// Avoiding iterating through the whole tree here, instead
// we can check if the conditions for radio group siblings apply