Repository: servo/servo
File: components/shared/layout/wrapper_traits.rs (Rust)

Code before:
        self.secondary.or(self.primary)
    }

    /// Return a possibly nested [`PseudoElementChain`]. Currently only `::before` and
    /// `::after` only support nesting. If the primary [`PseudoElement`] on the chain is
    /// not `::before` or `::after` a single element chain is returned for the given
    /// [`PseudoElement`].
    pub fn with_pseudo(&self, pseudo_element: PseudoElement) -> Self {
        match self.primary {
            Some(primary) if primary.is_before_or_after() => Self {
                primary: self.primary,
                secondary: Some(pseudo_element),
            },
            _ => {
                assert!(self.secondary.is_none());
                Self::unnested(pseudo_element)
            },
        }
    }

    pub fn without_innermost(&self) -> Option<Self> {
        let primary = self.primary?;
        Some(match self.secondary {
            Some(_) => Self::unnested(primary),
            None => Self::default(),
        })
    }

    pub fn is_empty(&self) -> bool {
        self.primary.is_none()
    }
}

/// A selection shared between script and layout. This selection is managed by the DOM
/// node that maintains it, and can be modified from script. Once modified, layout is
/// expected to reflect the new selection visual on the next display list update.
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
pub struct ScriptSelection {
    /// The range of this selection in the DOM node that manages it.
    pub range: TextByteRange,
    /// The character range of this selection in the DOM node that manages it.
    pub character_range: Range<usize>,
    /// Whether or not this selection is enabled. Selections may be disabled
    /// when their node loses focus.
    pub enabled: bool,
}

pub type SharedSelection = std::sync::Arc<AtomicRefCell<ScriptSelection>>;


Review comment (suggestion):
```suggestion
        self.secondary.and_then(|| Self::unnested(primary)).unwrap_or_default();
```

Code after:
        self.secondary.or(self.primary)
    }

    /// Return a possibly nested [`PseudoElementChain`]. Currently only `::before` and
    /// `::after` only support nesting. If the primary [`PseudoElement`] on the chain is
    /// not `::before` or `::after` a single element chain is returned for the given
    /// [`PseudoElement`].
    pub fn with_pseudo(&self, pseudo_element: PseudoElement) -> Self {
        match self.primary {
            Some(primary) if primary.is_before_or_after() => Self {
                primary: self.primary,
                secondary: Some(pseudo_element),
            },
            _ => {
                assert!(self.secondary.is_none());
                Self::unnested(pseudo_element)
            },
        }
    }

    pub fn without_innermost(&self) -> Option<Self> {
        let primary = self.primary?;
        Some(
            self.secondary
                .map_or_else(Self::default, |_| Self::unnested(primary)),
        )
    }

    pub fn is_empty(&self) -> bool {
        self.primary.is_none()
    }
}

/// A selection shared between script and layout. This selection is managed by the DOM
/// node that maintains it, and can be modified from script. Once modified, layout is
/// expected to reflect the new selection visual on the next display list update.
#[derive(Clone, Debug, Default, MallocSizeOf, PartialEq)]
pub struct ScriptSelection {
    /// The range of this selection in the DOM node that manages it.
    pub range: TextByteRange,
    /// The character range of this selection in the DOM node that manages it.
    pub character_range: Range<usize>,
    /// Whether or not this selection is enabled. Selections may be disabled
    /// when their node loses focus.
    pub enabled: bool,
}

pub type SharedSelection = std::sync::Arc<AtomicRefCell<ScriptSelection>>;