Skip to content

Commit

Permalink
Use a newtype within EagerPseudoValues.
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: IIDxBJ8mqvJ
  • Loading branch information
bholley committed Jun 23, 2017
1 parent e2a26e7 commit af89c74
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion components/style/context.rs
Expand Up @@ -332,7 +332,7 @@ impl Clone for EagerPseudoCascadeInputs {
impl EagerPseudoCascadeInputs {
/// Construct inputs from previous cascade results, if any.
fn new_from_style(styles: &EagerPseudoStyles) -> Self {
EagerPseudoCascadeInputs(styles.0.as_ref().map(|styles| {
EagerPseudoCascadeInputs(styles.as_array().map(|styles| {
let mut inputs: [Option<CascadeInputs>; EAGER_PSEUDO_COUNT] = Default::default();
for i in 0..EAGER_PSEUDO_COUNT {
inputs[i] = styles[i].as_ref().map(|s| CascadeInputs::new_from_style(s));
Expand Down
42 changes: 32 additions & 10 deletions components/style/data.rs
Expand Up @@ -13,6 +13,7 @@ use properties::longhands::display::computed_value as display;
use rule_tree::StrongRuleNode;
use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, RestyleDamage};
use shared_lock::{Locked, StylesheetGuards};
use std::ops::{Deref, DerefMut};
use stylearc::Arc;

bitflags! {
Expand Down Expand Up @@ -100,22 +101,35 @@ impl RestyleData {

/// A list of styles for eagerly-cascaded pseudo-elements.
/// Lazily-allocated.
#[derive(Debug)]
pub struct EagerPseudoStyles(pub Option<Box<[Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT]>>);
#[derive(Clone, Debug)]
pub struct EagerPseudoStyles(Option<Box<EagerPseudoArray>>);

#[derive(Debug, Default)]
struct EagerPseudoArray(EagerPseudoArrayInner);
type EagerPseudoArrayInner = [Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT];

impl Deref for EagerPseudoArray {
type Target = EagerPseudoArrayInner;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for EagerPseudoArray {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

// Manually implement `Clone` here because the derived impl of `Clone` for
// array types assumes the value inside is `Copy`.
impl Clone for EagerPseudoStyles {
impl Clone for EagerPseudoArray {
fn clone(&self) -> Self {
if self.0.is_none() {
return EagerPseudoStyles(None)
}
let self_values = self.0.as_ref().unwrap();
let mut values: [Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT] = Default::default();
let mut clone = Self::default();
for i in 0..EAGER_PSEUDO_COUNT {
values[i] = self_values[i].clone();
clone[i] = self.0[i].clone();
}
EagerPseudoStyles(Some(Box::new(values)))
clone
}
}

Expand All @@ -125,6 +139,14 @@ impl EagerPseudoStyles {
self.0.is_none()
}

/// Grabs a reference to the list of styles, if they exist.
pub fn as_array(&self) -> Option<&EagerPseudoArrayInner> {
match self.0 {
None => None,
Some(ref x) => Some(&x.0),
}
}

/// Returns a reference to the style for a given eager pseudo, if it exists.
pub fn get(&self, pseudo: &PseudoElement) -> Option<&Arc<ComputedValues>> {
debug_assert!(pseudo.is_eager());
Expand Down

0 comments on commit af89c74

Please sign in to comment.