diff --git a/crates/typst/src/foundations/styles.rs b/crates/typst/src/foundations/styles.rs index da8447c3a..f44a320f3 100644 --- a/crates/typst/src/foundations/styles.rs +++ b/crates/typst/src/foundations/styles.rs @@ -445,17 +445,16 @@ impl<'a> StyleChain<'a> { Self { head: &root.0, tail: None } } - /// Make the given style list the first link of this chain. + /// Make the given chainable the first link of this chain. /// /// The resulting style chain contains styles from `local` as well as /// `self`. The ones from `local` take precedence over the ones from /// `self`. For folded properties `local` contributes the inner value. - pub fn chain<'b>(&'b self, local: &'b Styles) -> StyleChain<'b> { - if local.is_empty() { - *self - } else { - StyleChain { head: &local.0, tail: Some(self) } - } + pub fn chain<'b, C>(&'b self, local: &'b C) -> StyleChain<'b> + where + C: Chainable, + { + Chainable::chain(local, self) } /// Cast the first value for the given property in the chain, @@ -630,6 +629,43 @@ impl PartialEq for StyleChain<'_> { } } +/// Things that can be attached to a style chain. +pub trait Chainable { + /// Attach `self` as the first link of the chain. + fn chain<'a>(&'a self, outer: &'a StyleChain<'_>) -> StyleChain<'a>; +} + +impl Chainable for Prehashed