Skip to content

Commit

Permalink
Introduce a ToCssWithGuard trait
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Mar 19, 2017
1 parent 3ae2ecb commit fe4e70c
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 54 deletions.
4 changes: 4 additions & 0 deletions components/script/dom/cssconditionrule.rs
Expand Up @@ -12,6 +12,7 @@ use dom::csssupportsrule::CSSSupportsRule;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::SharedRwLock;
use style::stylesheets::CssRules as StyleCssRules;

#[dom_struct]
Expand All @@ -27,6 +28,9 @@ impl CSSConditionRule {
}
}

pub fn shared_lock(&self) -> &SharedRwLock {
self.cssgroupingrule.shared_lock()
}
}

impl CSSConditionRuleMethods for CSSConditionRule {
Expand Down
5 changes: 3 additions & 2 deletions components/script/dom/cssfontfacerule.rs
Expand Up @@ -12,8 +12,8 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::ToCssWithGuard;
use style::font_face::FontFaceRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSFontFaceRule {
Expand Down Expand Up @@ -47,6 +47,7 @@ impl SpecificCSSRule for CSSFontFaceRule {
}

fn get_css(&self) -> DOMString {
self.fontfacerule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.fontfacerule.read().to_css_string(&guard).into()
}
}
5 changes: 5 additions & 0 deletions components/script/dom/cssgroupingrule.rs
Expand Up @@ -14,6 +14,7 @@ use dom::cssstylesheet::CSSStyleSheet;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::SharedRwLock;
use style::stylesheets::CssRules as StyleCssRules;

#[dom_struct]
Expand All @@ -40,6 +41,10 @@ impl CSSGroupingRule {
parent_stylesheet,
RulesSource::Rules(self.rules.clone())))
}

pub fn shared_lock(&self) -> &SharedRwLock {
self.cssrule.shared_lock()
}
}

impl CSSGroupingRuleMethods for CSSGroupingRule {
Expand Down
5 changes: 3 additions & 2 deletions components/script/dom/cssimportrule.rs
Expand Up @@ -12,8 +12,8 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::ImportRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSImportRule {
Expand Down Expand Up @@ -49,6 +49,7 @@ impl SpecificCSSRule for CSSImportRule {
}

fn get_css(&self) -> DOMString {
self.import_rule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.import_rule.read().to_css_string(&guard).into()
}
}
5 changes: 3 additions & 2 deletions components/script/dom/csskeyframesrule.rs
Expand Up @@ -21,8 +21,8 @@ use servo_atoms::Atom;
use std::sync::Arc;
use style::keyframes::{Keyframe, KeyframeSelector};
use style::parser::ParserContextExtraData;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::KeyframesRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSKeyframesRule {
Expand Down Expand Up @@ -134,7 +134,8 @@ impl SpecificCSSRule for CSSKeyframesRule {
}

fn get_css(&self) -> DOMString {
self.keyframesrule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.keyframesrule.read().to_css_string(&guard).into()
}

fn deparent_children(&self) {
Expand Down
8 changes: 5 additions & 3 deletions components/script/dom/cssmediarule.rs
Expand Up @@ -17,12 +17,13 @@ use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::media_queries::parse_media_query_list;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::MediaRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSMediaRule {
cssrule: CSSConditionRule,
cssconditionrule: CSSConditionRule,
#[ignore_heap_size_of = "Arc"]
mediarule: Arc<RwLock<MediaRule>>,
medialist: MutNullableJS<MediaList>,
Expand All @@ -33,7 +34,7 @@ impl CSSMediaRule {
-> CSSMediaRule {
let list = mediarule.read().rules.clone();
CSSMediaRule {
cssrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
cssconditionrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
mediarule: mediarule,
medialist: MutNullableJS::new(None),
}
Expand Down Expand Up @@ -76,7 +77,8 @@ impl SpecificCSSRule for CSSMediaRule {
}

fn get_css(&self) -> DOMString {
self.mediarule.read().to_css_string().into()
let guard = self.cssconditionrule.shared_lock().read();
self.mediarule.read().to_css_string(&guard).into()
}
}

Expand Down
5 changes: 3 additions & 2 deletions components/script/dom/cssnamespacerule.rs
Expand Up @@ -13,8 +13,8 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::NamespaceRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSNamespaceRule {
Expand Down Expand Up @@ -62,6 +62,7 @@ impl SpecificCSSRule for CSSNamespaceRule {
}

fn get_css(&self) -> DOMString {
self.namespacerule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.namespacerule.read().to_css_string(&guard).into()
}
}
5 changes: 5 additions & 0 deletions components/script/dom/cssrule.rs
Expand Up @@ -20,6 +20,7 @@ use dom::cssviewportrule::CSSViewportRule;
use dom::window::Window;
use dom_struct::dom_struct;
use std::cell::Cell;
use style::shared_lock::SharedRwLock;
use style::stylesheets::CssRule as StyleCssRule;


Expand Down Expand Up @@ -103,6 +104,10 @@ impl CSSRule {
pub fn parent_stylesheet(&self) -> &CSSStyleSheet {
&self.parent_stylesheet
}

pub fn shared_lock(&self) -> &SharedRwLock {
&self.parent_stylesheet.style_stylesheet().shared_lock
}
}

impl CSSRuleMethods for CSSRule {
Expand Down
5 changes: 3 additions & 2 deletions components/script/dom/cssstylerule.rs
Expand Up @@ -14,8 +14,8 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::StyleRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSStyleRule {
Expand Down Expand Up @@ -51,7 +51,8 @@ impl SpecificCSSRule for CSSStyleRule {
}

fn get_css(&self) -> DOMString {
self.stylerule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.stylerule.read().to_css_string(&guard).into()
}
}

Expand Down
8 changes: 5 additions & 3 deletions components/script/dom/csssupportsrule.rs
Expand Up @@ -16,13 +16,14 @@ use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::parser::ParserContext;
use style::shared_lock::ToCssWithGuard;
use style::stylesheets::SupportsRule;
use style::supports::SupportsCondition;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSSupportsRule {
cssrule: CSSConditionRule,
cssconditionrule: CSSConditionRule,
#[ignore_heap_size_of = "Arc"]
supportsrule: Arc<RwLock<SupportsRule>>,
}
Expand All @@ -32,7 +33,7 @@ impl CSSSupportsRule {
-> CSSSupportsRule {
let list = supportsrule.read().rules.clone();
CSSSupportsRule {
cssrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
cssconditionrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
supportsrule: supportsrule,
}
}
Expand Down Expand Up @@ -75,6 +76,7 @@ impl SpecificCSSRule for CSSSupportsRule {
}

fn get_css(&self) -> DOMString {
self.supportsrule.read().to_css_string().into()
let guard = self.cssconditionrule.shared_lock().read();
self.supportsrule.read().to_css_string(&guard).into()
}
}
5 changes: 3 additions & 2 deletions components/script/dom/cssviewportrule.rs
Expand Up @@ -12,8 +12,8 @@ use dom::window::Window;
use dom_struct::dom_struct;
use parking_lot::RwLock;
use std::sync::Arc;
use style::shared_lock::ToCssWithGuard;
use style::viewport::ViewportRule;
use style_traits::ToCss;

#[dom_struct]
pub struct CSSViewportRule {
Expand Down Expand Up @@ -46,6 +46,7 @@ impl SpecificCSSRule for CSSViewportRule {
}

fn get_css(&self) -> DOMString {
self.viewportrule.read().to_css_string().into()
let guard = self.cssrule.shared_lock().read();
self.viewportrule.read().to_css_string(&guard).into()
}
}
8 changes: 4 additions & 4 deletions components/style/font_face.rs
Expand Up @@ -14,6 +14,7 @@ use computed_values::font_family::FamilyName;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
#[cfg(feature = "gecko")] use cssparser::UnicodeRange;
use parser::{ParserContext, log_css_error, Parse};
use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use std::iter;
use style_traits::{ToCss, OneOrMoreCommaSeparated};
Expand Down Expand Up @@ -230,11 +231,10 @@ macro_rules! font_face_descriptors {
}
}

impl ToCss for FontFaceRule {
impl ToCssWithGuard for FontFaceRule {
// Serialization of FontFaceRule is not specced.
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write,
{
fn to_css<W>(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write {
dest.write_str("@font-face {\n")?;
$(
dest.write_str(concat!(" ", $m_name, ": "))?;
Expand Down
17 changes: 17 additions & 0 deletions components/style/shared_lock.rs
Expand Up @@ -160,3 +160,20 @@ mod compile_time_assert {
impl<'a> Marker2 for SharedRwLockReadGuard<'a> {} // Assert SharedRwLockReadGuard: !Copy
impl<'a> Marker2 for SharedRwLockWriteGuard<'a> {} // Assert SharedRwLockWriteGuard: !Copy
}

/// Like ToCss, but with a lock guard given by the caller.
pub trait ToCssWithGuard {
/// Serialize `self` in CSS syntax, writing to `dest`, using the given lock guard.
fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write;

/// Serialize `self` in CSS syntax using the given lock guard and return a string.
///
/// (This is a convenience wrapper for `to_css` and probably should not be overridden.)
#[inline]
fn to_css_string(&self, guard: &SharedRwLockReadGuard) -> String {
let mut s = String::new();
self.to_css(guard, &mut s).unwrap();
s
}
}

0 comments on commit fe4e70c

Please sign in to comment.