@@ -206,12 +206,6 @@ pub trait EncodeLabelSet {
206
206
fn encode ( & self , encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > ;
207
207
}
208
208
209
- impl < ' a > From < text:: LabelSetEncoder < ' a > > for LabelSetEncoder < ' a > {
210
- fn from ( e : text:: LabelSetEncoder < ' a > ) -> Self {
211
- Self ( LabelSetEncoderInner :: Text ( e) )
212
- }
213
- }
214
-
215
209
/// Encoder for a label set.
216
210
#[ derive( Debug ) ]
217
211
pub struct LabelSetEncoder < ' a > ( LabelSetEncoderInner < ' a > ) ;
@@ -223,6 +217,12 @@ enum LabelSetEncoderInner<'a> {
223
217
Protobuf ( protobuf:: LabelSetEncoder < ' a > ) ,
224
218
}
225
219
220
+ impl < ' a > From < text:: LabelSetEncoder < ' a > > for LabelSetEncoder < ' a > {
221
+ fn from ( e : text:: LabelSetEncoder < ' a > ) -> Self {
222
+ Self ( LabelSetEncoderInner :: Text ( e) )
223
+ }
224
+ }
225
+
226
226
#[ cfg( feature = "protobuf" ) ]
227
227
impl < ' a > From < protobuf:: LabelSetEncoder < ' a > > for LabelSetEncoder < ' a > {
228
228
fn from ( e : protobuf:: LabelSetEncoder < ' a > ) -> Self {
@@ -237,6 +237,42 @@ impl LabelSetEncoder<'_> {
237
237
}
238
238
}
239
239
240
+ impl < T : EncodeLabel , const N : usize > EncodeLabelSet for [ T ; N ] {
241
+ fn encode ( & self , encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
242
+ self . as_ref ( ) . encode ( encoder)
243
+ }
244
+ }
245
+
246
+ impl < T : EncodeLabel > EncodeLabelSet for & [ T ] {
247
+ fn encode ( & self , mut encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
248
+ if self . is_empty ( ) {
249
+ return Ok ( ( ) ) ;
250
+ }
251
+
252
+ for label in self . iter ( ) {
253
+ label. encode ( encoder. encode_label ( ) ) ?
254
+ }
255
+
256
+ Ok ( ( ) )
257
+ }
258
+ }
259
+
260
+ impl < T : EncodeLabel > EncodeLabelSet for Vec < T > {
261
+ fn encode ( & self , encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
262
+ self . as_slice ( ) . encode ( encoder)
263
+ }
264
+ }
265
+
266
+ /// Uninhabited type to represent the lack of a label set for a metric
267
+ #[ derive( Debug ) ]
268
+ pub enum NoLabelSet { }
269
+
270
+ impl EncodeLabelSet for NoLabelSet {
271
+ fn encode ( & self , _encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
272
+ Ok ( ( ) )
273
+ }
274
+ }
275
+
240
276
/// An encodable label.
241
277
pub trait EncodeLabel {
242
278
/// Encode oneself into the given encoder.
@@ -247,10 +283,6 @@ pub trait EncodeLabel {
247
283
#[ derive( Debug ) ]
248
284
pub struct LabelEncoder < ' a > ( LabelEncoderInner < ' a > ) ;
249
285
250
- /// Uninhabited type to represent the lack of a label set for a metric
251
- #[ derive( Debug ) ]
252
- pub enum NoLabelSet { }
253
-
254
286
#[ derive( Debug ) ]
255
287
enum LabelEncoderInner < ' a > {
256
288
Text ( text:: LabelEncoder < ' a > ) ,
@@ -283,6 +315,21 @@ impl LabelEncoder<'_> {
283
315
}
284
316
}
285
317
318
+ impl < K : EncodeLabelKey , V : EncodeLabelValue > EncodeLabel for ( K , V ) {
319
+ fn encode ( & self , mut encoder : LabelEncoder ) -> Result < ( ) , std:: fmt:: Error > {
320
+ let ( key, value) = self ;
321
+
322
+ let mut label_key_encoder = encoder. encode_label_key ( ) ?;
323
+ key. encode ( & mut label_key_encoder) ?;
324
+
325
+ let mut label_value_encoder = label_key_encoder. encode_label_value ( ) ?;
326
+ value. encode ( & mut label_value_encoder) ?;
327
+ label_value_encoder. finish ( ) ?;
328
+
329
+ Ok ( ( ) )
330
+ }
331
+ }
332
+
286
333
/// An encodable label key.
287
334
pub trait EncodeLabelKey {
288
335
/// Encode oneself into the given encoder.
@@ -330,52 +377,6 @@ impl<'a> LabelKeyEncoder<'a> {
330
377
)
331
378
}
332
379
}
333
- impl < T : EncodeLabel , const N : usize > EncodeLabelSet for [ T ; N ] {
334
- fn encode ( & self , encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
335
- self . as_ref ( ) . encode ( encoder)
336
- }
337
- }
338
-
339
- impl < T : EncodeLabel > EncodeLabelSet for & [ T ] {
340
- fn encode ( & self , mut encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
341
- if self . is_empty ( ) {
342
- return Ok ( ( ) ) ;
343
- }
344
-
345
- for label in self . iter ( ) {
346
- label. encode ( encoder. encode_label ( ) ) ?
347
- }
348
-
349
- Ok ( ( ) )
350
- }
351
- }
352
-
353
- impl < T : EncodeLabel > EncodeLabelSet for Vec < T > {
354
- fn encode ( & self , encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
355
- self . as_slice ( ) . encode ( encoder)
356
- }
357
- }
358
-
359
- impl EncodeLabelSet for NoLabelSet {
360
- fn encode ( & self , _encoder : LabelSetEncoder ) -> Result < ( ) , std:: fmt:: Error > {
361
- Ok ( ( ) )
362
- }
363
- }
364
-
365
- impl < K : EncodeLabelKey , V : EncodeLabelValue > EncodeLabel for ( K , V ) {
366
- fn encode ( & self , mut encoder : LabelEncoder ) -> Result < ( ) , std:: fmt:: Error > {
367
- let ( key, value) = self ;
368
-
369
- let mut label_key_encoder = encoder. encode_label_key ( ) ?;
370
- key. encode ( & mut label_key_encoder) ?;
371
-
372
- let mut label_value_encoder = label_key_encoder. encode_label_value ( ) ?;
373
- value. encode ( & mut label_value_encoder) ?;
374
- label_value_encoder. finish ( ) ?;
375
-
376
- Ok ( ( ) )
377
- }
378
- }
379
380
380
381
impl EncodeLabelKey for & str {
381
382
fn encode ( & self , encoder : & mut LabelKeyEncoder ) -> Result < ( ) , std:: fmt:: Error > {
@@ -433,6 +434,13 @@ pub trait EncodeLabelValue {
433
434
#[ derive( Debug ) ]
434
435
pub struct LabelValueEncoder < ' a > ( LabelValueEncoderInner < ' a > ) ;
435
436
437
+ #[ derive( Debug ) ]
438
+ enum LabelValueEncoderInner < ' a > {
439
+ Text ( text:: LabelValueEncoder < ' a > ) ,
440
+ #[ cfg( feature = "protobuf" ) ]
441
+ Protobuf ( protobuf:: LabelValueEncoder < ' a > ) ,
442
+ }
443
+
436
444
impl < ' a > From < text:: LabelValueEncoder < ' a > > for LabelValueEncoder < ' a > {
437
445
fn from ( e : text:: LabelValueEncoder < ' a > ) -> Self {
438
446
LabelValueEncoder ( LabelValueEncoderInner :: Text ( e) )
@@ -446,11 +454,10 @@ impl<'a> From<protobuf::LabelValueEncoder<'a>> for LabelValueEncoder<'a> {
446
454
}
447
455
}
448
456
449
- #[ derive( Debug ) ]
450
- enum LabelValueEncoderInner < ' a > {
451
- Text ( text:: LabelValueEncoder < ' a > ) ,
452
- #[ cfg( feature = "protobuf" ) ]
453
- Protobuf ( protobuf:: LabelValueEncoder < ' a > ) ,
457
+ impl std:: fmt:: Write for LabelValueEncoder < ' _ > {
458
+ fn write_str ( & mut self , s : & str ) -> std:: fmt:: Result {
459
+ for_both_mut ! ( self , LabelValueEncoderInner , e, e. write_str( s) )
460
+ }
454
461
}
455
462
456
463
impl LabelValueEncoder < ' _ > {
@@ -460,12 +467,6 @@ impl LabelValueEncoder<'_> {
460
467
}
461
468
}
462
469
463
- impl std:: fmt:: Write for LabelValueEncoder < ' _ > {
464
- fn write_str ( & mut self , s : & str ) -> std:: fmt:: Result {
465
- for_both_mut ! ( self , LabelValueEncoderInner , e, e. write_str( s) )
466
- }
467
- }
468
-
469
470
impl EncodeLabelValue for & str {
470
471
fn encode ( & self , encoder : & mut LabelValueEncoder ) -> Result < ( ) , std:: fmt:: Error > {
471
472
encoder. write_str ( self ) ?;
@@ -536,6 +537,12 @@ where
536
537
}
537
538
}
538
539
540
+ impl EncodeLabelValue for bool {
541
+ fn encode ( & self , encoder : & mut LabelValueEncoder ) -> Result < ( ) , std:: fmt:: Error > {
542
+ encoder. write_str ( if * self { "true" } else { "false" } )
543
+ }
544
+ }
545
+
539
546
macro_rules! impl_encode_label_value_for_integer {
540
547
( $( $t: ident) ,* ) => { $(
541
548
impl EncodeLabelValue for $t {
@@ -678,6 +685,19 @@ enum CounterValueEncoderInner<'a> {
678
685
Protobuf ( protobuf:: CounterValueEncoder < ' a > ) ,
679
686
}
680
687
688
+ impl < ' a > From < text:: CounterValueEncoder < ' a > > for CounterValueEncoder < ' a > {
689
+ fn from ( e : text:: CounterValueEncoder < ' a > ) -> Self {
690
+ CounterValueEncoder ( CounterValueEncoderInner :: Text ( e) )
691
+ }
692
+ }
693
+
694
+ #[ cfg( feature = "protobuf" ) ]
695
+ impl < ' a > From < protobuf:: CounterValueEncoder < ' a > > for CounterValueEncoder < ' a > {
696
+ fn from ( e : protobuf:: CounterValueEncoder < ' a > ) -> Self {
697
+ CounterValueEncoder ( CounterValueEncoderInner :: Protobuf ( e) )
698
+ }
699
+ }
700
+
681
701
impl CounterValueEncoder < ' _ > {
682
702
fn encode_f64 ( & mut self , v : f64 ) -> Result < ( ) , std:: fmt:: Error > {
683
703
for_both_mut ! ( self , CounterValueEncoderInner , e, e. encode_f64( v) )
@@ -718,19 +738,6 @@ impl EncodeExemplarValue for u32 {
718
738
}
719
739
}
720
740
721
- impl < ' a > From < text:: CounterValueEncoder < ' a > > for CounterValueEncoder < ' a > {
722
- fn from ( e : text:: CounterValueEncoder < ' a > ) -> Self {
723
- CounterValueEncoder ( CounterValueEncoderInner :: Text ( e) )
724
- }
725
- }
726
-
727
- #[ cfg( feature = "protobuf" ) ]
728
- impl < ' a > From < protobuf:: CounterValueEncoder < ' a > > for CounterValueEncoder < ' a > {
729
- fn from ( e : protobuf:: CounterValueEncoder < ' a > ) -> Self {
730
- CounterValueEncoder ( CounterValueEncoderInner :: Protobuf ( e) )
731
- }
732
- }
733
-
734
741
/// Encoder for an exemplar value.
735
742
#[ derive( Debug ) ]
736
743
pub struct ExemplarValueEncoder < ' a > ( ExemplarValueEncoderInner < ' a > ) ;
@@ -742,12 +749,6 @@ enum ExemplarValueEncoderInner<'a> {
742
749
Protobuf ( protobuf:: ExemplarValueEncoder < ' a > ) ,
743
750
}
744
751
745
- impl ExemplarValueEncoder < ' _ > {
746
- fn encode ( & mut self , v : f64 ) -> Result < ( ) , std:: fmt:: Error > {
747
- for_both_mut ! ( self , ExemplarValueEncoderInner , e, e. encode( v) )
748
- }
749
- }
750
-
751
752
impl < ' a > From < text:: ExemplarValueEncoder < ' a > > for ExemplarValueEncoder < ' a > {
752
753
fn from ( e : text:: ExemplarValueEncoder < ' a > ) -> Self {
753
754
ExemplarValueEncoder ( ExemplarValueEncoderInner :: Text ( e) )
@@ -760,3 +761,9 @@ impl<'a> From<protobuf::ExemplarValueEncoder<'a>> for ExemplarValueEncoder<'a> {
760
761
ExemplarValueEncoder ( ExemplarValueEncoderInner :: Protobuf ( e) )
761
762
}
762
763
}
764
+
765
+ impl ExemplarValueEncoder < ' _ > {
766
+ fn encode ( & mut self , v : f64 ) -> Result < ( ) , std:: fmt:: Error > {
767
+ for_both_mut ! ( self , ExemplarValueEncoderInner , e, e. encode( v) )
768
+ }
769
+ }
0 commit comments