@@ -2,8 +2,10 @@ use std::any::{Any, TypeId};
2
2
use std:: fmt:: { self , Debug , Formatter , Write } ;
3
3
use std:: iter;
4
4
use std:: mem;
5
+ use std:: ptr;
5
6
use std:: sync:: Arc ;
6
7
8
+ use comemo:: Prehashed ;
7
9
use ecow:: { eco_format, eco_vec, EcoString , EcoVec } ;
8
10
9
11
use super :: { Content , ElemFunc , Element , Introspector , Label , Location , Vt } ;
@@ -15,7 +17,7 @@ use crate::util::pretty_array_like;
15
17
16
18
/// A list of style properties.
17
19
#[ derive( Default , PartialEq , Clone , Hash ) ]
18
- pub struct Styles ( EcoVec < Style > ) ;
20
+ pub struct Styles ( EcoVec < Prehashed < Style > > ) ;
19
21
20
22
impl Styles {
21
23
/// Create a new, empty style list.
@@ -34,7 +36,7 @@ impl Styles {
34
36
/// style map, `self` contributes the outer values and `value` is the inner
35
37
/// one.
36
38
pub fn set ( & mut self , style : impl Into < Style > ) {
37
- self . 0 . push ( style. into ( ) ) ;
39
+ self . 0 . push ( Prehashed :: new ( style. into ( ) ) ) ;
38
40
}
39
41
40
42
/// Remove the style that was last set.
@@ -51,20 +53,22 @@ impl Styles {
51
53
/// Apply one outer styles. Like [`chain_one`](StyleChain::chain_one), but
52
54
/// in-place.
53
55
pub fn apply_one ( & mut self , outer : Style ) {
54
- self . 0 . insert ( 0 , outer) ;
56
+ self . 0 . insert ( 0 , Prehashed :: new ( outer) ) ;
55
57
}
56
58
57
59
/// Apply a slice of outer styles.
58
- pub fn apply_slice ( & mut self , outer : & [ Style ] ) {
60
+ pub fn apply_slice ( & mut self , outer : & [ Prehashed < Style > ] ) {
59
61
self . 0 = outer. iter ( ) . cloned ( ) . chain ( mem:: take ( self ) . 0 . into_iter ( ) ) . collect ( ) ;
60
62
}
61
63
62
64
/// Add an origin span to all contained properties.
63
65
pub fn spanned ( mut self , span : Span ) -> Self {
64
66
for entry in self . 0 . make_mut ( ) {
65
- if let Style :: Property ( property) = entry {
66
- property. span = Some ( span) ;
67
- }
67
+ entry. update ( |entry| {
68
+ if let Style :: Property ( property) = entry {
69
+ property. span = Some ( span) ;
70
+ }
71
+ } ) ;
68
72
}
69
73
self
70
74
}
@@ -73,7 +77,7 @@ impl Styles {
73
77
/// styles for the given element.
74
78
pub fn interruption < T : Element > ( & self ) -> Option < Option < Span > > {
75
79
let func = T :: func ( ) ;
76
- self . 0 . iter ( ) . find_map ( |entry| match entry {
80
+ self . 0 . iter ( ) . find_map ( |entry| match & * * entry {
77
81
Style :: Property ( property) => property. is_of ( func) . then_some ( property. span ) ,
78
82
Style :: Recipe ( recipe) => recipe. is_of ( func) . then_some ( Some ( recipe. span ) ) ,
79
83
} )
@@ -82,7 +86,7 @@ impl Styles {
82
86
83
87
impl From < Style > for Styles {
84
88
fn from ( entry : Style ) -> Self {
85
- Self ( eco_vec ! [ entry] )
89
+ Self ( eco_vec ! [ Prehashed :: new ( entry) ] )
86
90
}
87
91
}
88
92
@@ -566,7 +570,7 @@ cast_from_value! {
566
570
#[ derive( Default , Clone , Copy , Hash ) ]
567
571
pub struct StyleChain < ' a > {
568
572
/// The first link of this chain.
569
- head : & ' a [ Style ] ,
573
+ head : & ' a [ Prehashed < Style > ] ,
570
574
/// The remaining links in the chain.
571
575
tail : Option < & ' a Self > ,
572
576
}
@@ -590,14 +594,6 @@ impl<'a> StyleChain<'a> {
590
594
}
591
595
}
592
596
593
- /// Make the given style the first link of the this chain.
594
- pub fn chain_one < ' b > ( & ' b self , style : & ' b Style ) -> StyleChain < ' b > {
595
- StyleChain {
596
- head : std:: slice:: from_ref ( style) ,
597
- tail : Some ( self ) ,
598
- }
599
- }
600
-
601
597
/// Cast the first value for the given property in the chain.
602
598
pub fn get < T : Cast > (
603
599
self ,
@@ -733,16 +729,6 @@ impl<'a> StyleChain<'a> {
733
729
fn pop ( & mut self ) {
734
730
* self = self . tail . copied ( ) . unwrap_or_default ( ) ;
735
731
}
736
-
737
- /// Whether two style chains contain the same pointers.
738
- fn ptr_eq ( self , other : Self ) -> bool {
739
- std:: ptr:: eq ( self . head , other. head )
740
- && match ( self . tail , other. tail ) {
741
- ( Some ( a) , Some ( b) ) => std:: ptr:: eq ( a, b) ,
742
- ( None , None ) => true ,
743
- _ => false ,
744
- }
745
- }
746
732
}
747
733
748
734
impl Debug for StyleChain < ' _ > {
@@ -756,13 +742,18 @@ impl Debug for StyleChain<'_> {
756
742
757
743
impl PartialEq for StyleChain < ' _ > {
758
744
fn eq ( & self , other : & Self ) -> bool {
759
- self . ptr_eq ( * other) || crate :: util:: hash128 ( self ) == crate :: util:: hash128 ( other)
745
+ ptr:: eq ( self . head , other. head )
746
+ && match ( self . tail , other. tail ) {
747
+ ( Some ( a) , Some ( b) ) => ptr:: eq ( a, b) ,
748
+ ( None , None ) => true ,
749
+ _ => false ,
750
+ }
760
751
}
761
752
}
762
753
763
754
/// An iterator over the entries in a style chain.
764
755
struct Entries < ' a > {
765
- inner : std:: slice:: Iter < ' a , Style > ,
756
+ inner : std:: slice:: Iter < ' a , Prehashed < Style > > ,
766
757
links : Links < ' a > ,
767
758
}
768
759
@@ -787,7 +778,7 @@ impl<'a> Iterator for Entries<'a> {
787
778
struct Links < ' a > ( Option < StyleChain < ' a > > ) ;
788
779
789
780
impl < ' a > Iterator for Links < ' a > {
790
- type Item = & ' a [ Style ] ;
781
+ type Item = & ' a [ Prehashed < Style > ] ;
791
782
792
783
fn next ( & mut self ) -> Option < Self :: Item > {
793
784
let StyleChain { head, tail } = self . 0 ?;
0 commit comments