@@ -115,6 +115,13 @@ impl Field {
115
115
let tag = self . tag ;
116
116
117
117
match self . kind {
118
+ Kind :: Plain ( DefaultValue :: SmallString ) => {
119
+ quote ! {
120
+ if !#ident. is_empty( ) {
121
+ #encode_fn( #tag, & #ident, buf) ;
122
+ }
123
+ }
124
+ }
118
125
Kind :: Plain ( ref default) => {
119
126
let default = default. typed ( ) ;
120
127
quote ! {
@@ -169,6 +176,15 @@ impl Field {
169
176
let tag = self . tag ;
170
177
171
178
match self . kind {
179
+ Kind :: Plain ( DefaultValue :: SmallString ) => {
180
+ quote ! {
181
+ if !#ident. is_empty( ) {
182
+ #encoded_len_fn( #tag, & #ident)
183
+ } else {
184
+ 0
185
+ }
186
+ }
187
+ }
172
188
Kind :: Plain ( ref default) => {
173
189
let default = default. typed ( ) ;
174
190
quote ! {
@@ -193,7 +209,7 @@ impl Field {
193
209
Kind :: Plain ( ref default) | Kind :: Required ( ref default) => {
194
210
let default = default. typed ( ) ;
195
211
match self . ty {
196
- Ty :: String | Ty :: Bytes ( ..) => quote ! ( #ident. clear( ) ) ,
212
+ Ty :: String | Ty :: SmallString | Ty :: Bytes ( ..) => quote ! ( #ident. clear( ) ) ,
197
213
_ => quote ! ( #ident = #default ) ,
198
214
}
199
215
}
@@ -397,6 +413,7 @@ pub enum Ty {
397
413
Sfixed64 ,
398
414
Bool ,
399
415
String ,
416
+ SmallString ,
400
417
Bytes ( BytesTy ) ,
401
418
Enumeration ( Path ) ,
402
419
}
@@ -441,6 +458,7 @@ impl Ty {
441
458
Meta :: Path ( ref name) if name. is_ident ( "sfixed64" ) => Ty :: Sfixed64 ,
442
459
Meta :: Path ( ref name) if name. is_ident ( "bool" ) => Ty :: Bool ,
443
460
Meta :: Path ( ref name) if name. is_ident ( "string" ) => Ty :: String ,
461
+ Meta :: Path ( ref name) if name. is_ident ( "smallstring" ) => Ty :: SmallString ,
444
462
Meta :: Path ( ref name) if name. is_ident ( "bytes" ) => Ty :: Bytes ( BytesTy :: Vec ) ,
445
463
Meta :: NameValue ( MetaNameValue {
446
464
ref path,
@@ -486,6 +504,7 @@ impl Ty {
486
504
"sfixed64" => Ty :: Sfixed64 ,
487
505
"bool" => Ty :: Bool ,
488
506
"string" => Ty :: String ,
507
+ "smallstring" => Ty :: SmallString ,
489
508
"bytes" => Ty :: Bytes ( BytesTy :: Vec ) ,
490
509
s if s. len ( ) > enumeration_len && & s[ ..enumeration_len] == "enumeration" => {
491
510
let s = & s[ enumeration_len..] . trim ( ) ;
@@ -522,6 +541,7 @@ impl Ty {
522
541
Ty :: Sfixed64 => "sfixed64" ,
523
542
Ty :: Bool => "bool" ,
524
543
Ty :: String => "string" ,
544
+ Ty :: SmallString => "smallstring" ,
525
545
Ty :: Bytes ( ..) => "bytes" ,
526
546
Ty :: Enumeration ( ..) => "enum" ,
527
547
}
@@ -531,6 +551,7 @@ impl Ty {
531
551
pub fn rust_type ( & self ) -> TokenStream {
532
552
match self {
533
553
Ty :: String => quote ! ( :: prost:: alloc:: string:: String ) ,
554
+ Ty :: SmallString => quote ! ( :: compact_str:: CompactString ) ,
534
555
Ty :: Bytes ( ty) => ty. rust_type ( ) ,
535
556
_ => self . rust_ref_type ( ) ,
536
557
}
@@ -553,6 +574,7 @@ impl Ty {
553
574
Ty :: Sfixed64 => quote ! ( i64 ) ,
554
575
Ty :: Bool => quote ! ( bool ) ,
555
576
Ty :: String => quote ! ( & str ) ,
577
+ Ty :: SmallString => quote ! ( & str ) ,
556
578
Ty :: Bytes ( ..) => quote ! ( & [ u8 ] ) ,
557
579
Ty :: Enumeration ( ..) => quote ! ( i32 ) ,
558
580
}
@@ -567,7 +589,7 @@ impl Ty {
567
589
568
590
/// Returns false if the scalar type is length delimited (i.e., `string` or `bytes`).
569
591
pub fn is_numeric ( & self ) -> bool {
570
- !matches ! ( self , Ty :: String | Ty :: Bytes ( ..) )
592
+ !matches ! ( self , Ty :: String | Ty :: SmallString | Ty :: Bytes ( ..) )
571
593
}
572
594
}
573
595
@@ -609,6 +631,7 @@ pub enum DefaultValue {
609
631
U64 ( u64 ) ,
610
632
Bool ( bool ) ,
611
633
String ( String ) ,
634
+ SmallString ,
612
635
Bytes ( Vec < u8 > ) ,
613
636
Enumeration ( TokenStream ) ,
614
637
Path ( Path ) ,
@@ -773,6 +796,7 @@ impl DefaultValue {
773
796
774
797
Ty :: Bool => DefaultValue :: Bool ( false ) ,
775
798
Ty :: String => DefaultValue :: String ( String :: new ( ) ) ,
799
+ Ty :: SmallString => DefaultValue :: SmallString ,
776
800
Ty :: Bytes ( ..) => DefaultValue :: Bytes ( Vec :: new ( ) ) ,
777
801
Ty :: Enumeration ( ref path) => DefaultValue :: Enumeration ( quote ! ( #path:: default ( ) ) ) ,
778
802
}
@@ -784,6 +808,9 @@ impl DefaultValue {
784
808
quote ! ( :: prost:: alloc:: string:: String :: new( ) )
785
809
}
786
810
DefaultValue :: String ( ref value) => quote ! ( #value. into( ) ) ,
811
+ DefaultValue :: SmallString => {
812
+ quote ! ( :: compact_str:: CompactString :: default ( ) )
813
+ }
787
814
DefaultValue :: Bytes ( ref value) if value. is_empty ( ) => {
788
815
quote ! ( :: core:: default :: Default :: default ( ) )
789
816
}
@@ -799,6 +826,8 @@ impl DefaultValue {
799
826
pub fn typed ( & self ) -> TokenStream {
800
827
if let DefaultValue :: Enumeration ( _) = * self {
801
828
quote ! ( #self as i32 )
829
+ } else if let DefaultValue :: SmallString = * self {
830
+ quote ! ( Default :: default ( ) )
802
831
} else {
803
832
quote ! ( #self )
804
833
}
@@ -816,6 +845,7 @@ impl ToTokens for DefaultValue {
816
845
DefaultValue :: U64 ( value) => value. to_tokens ( tokens) ,
817
846
DefaultValue :: Bool ( value) => value. to_tokens ( tokens) ,
818
847
DefaultValue :: String ( ref value) => value. to_tokens ( tokens) ,
848
+ DefaultValue :: SmallString => "" . to_tokens ( tokens) ,
819
849
DefaultValue :: Bytes ( ref value) => {
820
850
let byte_str = LitByteStr :: new ( value, Span :: call_site ( ) ) ;
821
851
tokens. append_all ( quote ! ( #byte_str as & [ u8 ] ) ) ;
0 commit comments