@@ -690,9 +690,44 @@ pub struct FrozenModule {
690
690
pub package : bool ,
691
691
}
692
692
693
+ #[ derive( Clone ) ]
694
+ enum StringDataInner {
695
+ Static ( & ' static str ) ,
696
+ Owned ( Box < str > ) ,
697
+ }
698
+ impl StringDataInner {
699
+ fn as_str ( & self ) -> & str {
700
+ match self {
701
+ Self :: Static ( s) => s,
702
+ Self :: Owned ( ref s) => & * s,
703
+ }
704
+ }
705
+ }
706
+ impl fmt:: Debug for StringDataInner {
707
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
708
+ fmt:: Debug :: fmt ( self . as_str ( ) , f)
709
+ }
710
+ }
711
+ impl serde:: Serialize for StringDataInner {
712
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
713
+ where
714
+ S : serde:: Serializer ,
715
+ {
716
+ serializer. serialize_str ( self . as_str ( ) )
717
+ }
718
+ }
719
+ impl < ' de > serde:: Deserialize < ' de > for StringDataInner {
720
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
721
+ where
722
+ D : serde:: Deserializer < ' de > ,
723
+ {
724
+ serde:: Deserialize :: deserialize ( deserializer) . map ( Self :: Owned )
725
+ }
726
+ }
727
+
693
728
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
694
729
pub struct StringData {
695
- s : Box < str > ,
730
+ inner : StringDataInner ,
696
731
#[ serde( skip) ]
697
732
hash : OnceCell < i64 > ,
698
733
#[ serde( skip) ]
@@ -702,30 +737,33 @@ pub struct StringData {
702
737
impl StringData {
703
738
#[ inline]
704
739
pub fn as_str ( & self ) -> & str {
705
- self . as_ref ( )
740
+ self . inner . as_str ( )
706
741
}
707
742
708
743
pub fn hash_value ( & self ) -> i64 {
709
744
* self . hash . get_or_init ( || {
710
745
use std:: hash:: * ;
711
746
let mut hasher = std:: collections:: hash_map:: DefaultHasher :: new ( ) ;
712
- self . s . hash ( & mut hasher) ;
747
+ self . as_str ( ) . hash ( & mut hasher) ;
713
748
hasher. finish ( ) as i64
714
749
} )
715
750
}
716
751
717
752
pub fn char_len ( & self ) -> usize {
718
- * self . len . get_or_init ( || self . s . chars ( ) . count ( ) )
753
+ * self . len . get_or_init ( || self . as_str ( ) . chars ( ) . count ( ) )
719
754
}
720
755
721
756
pub fn into_string ( self ) -> String {
722
- self . s . into_string ( )
757
+ match self . inner {
758
+ StringDataInner :: Static ( s) => s. to_owned ( ) ,
759
+ StringDataInner :: Owned ( s) => s. into ( ) ,
760
+ }
723
761
}
724
762
}
725
763
726
764
impl PartialEq for StringData {
727
765
fn eq ( & self , other : & Self ) -> bool {
728
- self . s == other. s
766
+ self . as_str ( ) == other. as_str ( )
729
767
}
730
768
}
731
769
@@ -734,14 +772,14 @@ impl Eq for StringData {}
734
772
impl AsRef < str > for StringData {
735
773
#[ inline]
736
774
fn as_ref ( & self ) -> & str {
737
- & self . s
775
+ self . as_str ( )
738
776
}
739
777
}
740
778
741
779
impl From < Box < str > > for StringData {
742
780
fn from ( s : Box < str > ) -> Self {
743
781
StringData {
744
- s ,
782
+ inner : StringDataInner :: Owned ( s ) ,
745
783
hash : OnceCell :: new ( ) ,
746
784
len : OnceCell :: new ( ) ,
747
785
}
@@ -754,15 +792,13 @@ impl From<String> for StringData {
754
792
}
755
793
}
756
794
757
- impl From < & str > for StringData {
758
- fn from ( s : & str ) -> Self {
759
- Box :: < str > :: from ( s) . into ( )
760
- }
761
- }
762
-
763
- impl From < & String > for StringData {
764
- fn from ( s : & String ) -> Self {
765
- s. as_str ( ) . into ( )
795
+ impl From < & ' static str > for StringData {
796
+ fn from ( s : & ' static str ) -> Self {
797
+ StringData {
798
+ inner : StringDataInner :: Static ( s) ,
799
+ hash : OnceCell :: new ( ) ,
800
+ len : OnceCell :: new ( ) ,
801
+ }
766
802
}
767
803
}
768
804
0 commit comments