@@ -7,6 +7,7 @@ use super::key::MetadataKey;
7
7
8
8
use bytes:: Bytes ;
9
9
use http:: header:: HeaderValue ;
10
+ use std:: convert:: TryFrom ;
10
11
use std:: error:: Error ;
11
12
use std:: hash:: { Hash , Hasher } ;
12
13
use std:: marker:: PhantomData ;
@@ -86,9 +87,6 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
86
87
/// For Binary metadata values this method cannot fail. See also the Binary
87
88
/// only version of this method `from_bytes`.
88
89
///
89
- /// This function is intended to be replaced in the future by a `TryFrom`
90
- /// implementation once the trait is stabilized in std.
91
- ///
92
90
/// # Examples
93
91
///
94
92
/// ```
@@ -105,11 +103,9 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
105
103
/// assert!(val.is_err());
106
104
/// ```
107
105
#[ inline]
106
+ #[ deprecated = "Use TryFrom instead" ]
108
107
pub fn try_from_bytes ( src : & [ u8 ] ) -> Result < Self , InvalidMetadataValueBytes > {
109
- VE :: from_bytes ( src) . map ( |value| MetadataValue {
110
- inner : value,
111
- phantom : PhantomData ,
112
- } )
108
+ Self :: try_from ( src)
113
109
}
114
110
115
111
/// Attempt to convert a `Bytes` buffer to a `MetadataValue`.
@@ -122,15 +118,10 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
122
118
/// error is returned. In use cases where the input is not base64 encoded,
123
119
/// use `from_bytes`; if the value has to be encoded it's not possible to
124
120
/// share the memory anyways.
125
- ///
126
- /// This function is intended to be replaced in the future by a `TryFrom`
127
- /// implementation once the trait is stabilized in std.
128
121
#[ inline]
122
+ #[ deprecated = "Use TryFrom instead" ]
129
123
pub fn from_shared ( src : Bytes ) -> Result < Self , InvalidMetadataValueBytes > {
130
- VE :: from_shared ( src) . map ( |value| MetadataValue {
131
- inner : value,
132
- phantom : PhantomData ,
133
- } )
124
+ Self :: try_from ( src)
134
125
}
135
126
136
127
/// Convert a `Bytes` directly into a `MetadataValue` without validating.
@@ -282,6 +273,161 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
282
273
}
283
274
}
284
275
276
+ /// Attempt to convert a byte slice to a `MetadataValue`.
277
+ ///
278
+ /// For Ascii metadata values, If the argument contains invalid metadata
279
+ /// value bytes, an error is returned. Only byte values between 32 and 255
280
+ /// (inclusive) are permitted, excluding byte 127 (DEL).
281
+ ///
282
+ /// For Binary metadata values this method cannot fail. See also the Binary
283
+ /// only version of this method `from_bytes`.
284
+ ///
285
+ /// # Examples
286
+ ///
287
+ /// ```
288
+ /// # use tonic::metadata::*;
289
+ /// let val = AsciiMetadataValue::try_from_bytes(b"hello\xfa").unwrap();
290
+ /// assert_eq!(val, &b"hello\xfa"[..]);
291
+ /// ```
292
+ ///
293
+ /// An invalid value
294
+ ///
295
+ /// ```
296
+ /// # use tonic::metadata::*;
297
+ /// let val = AsciiMetadataValue::try_from_bytes(b"\n");
298
+ /// assert!(val.is_err());
299
+ /// ```
300
+ impl < ' a , VE : ValueEncoding > TryFrom < & ' a [ u8 ] > for MetadataValue < VE > {
301
+ type Error = InvalidMetadataValueBytes ;
302
+
303
+ #[ inline]
304
+ fn try_from ( src : & [ u8 ] ) -> Result < Self , Self :: Error > {
305
+ VE :: from_bytes ( src) . map ( |value| MetadataValue {
306
+ inner : value,
307
+ phantom : PhantomData ,
308
+ } )
309
+ }
310
+ }
311
+
312
+ /// Attempt to convert a byte slice to a `MetadataValue`.
313
+ ///
314
+ /// For Ascii metadata values, If the argument contains invalid metadata
315
+ /// value bytes, an error is returned. Only byte values between 32 and 255
316
+ /// (inclusive) are permitted, excluding byte 127 (DEL).
317
+ ///
318
+ /// For Binary metadata values this method cannot fail. See also the Binary
319
+ /// only version of this method `from_bytes`.
320
+ ///
321
+ /// # Examples
322
+ ///
323
+ /// ```
324
+ /// # use tonic::metadata::*;
325
+ /// let val = AsciiMetadataValue::try_from_bytes(b"hello\xfa").unwrap();
326
+ /// assert_eq!(val, &b"hello\xfa"[..]);
327
+ /// ```
328
+ ///
329
+ /// An invalid value
330
+ ///
331
+ /// ```
332
+ /// # use tonic::metadata::*;
333
+ /// let val = AsciiMetadataValue::try_from_bytes(b"\n");
334
+ /// assert!(val.is_err());
335
+ /// ```
336
+ impl < ' a , VE : ValueEncoding , const N : usize > TryFrom < & ' a [ u8 ; N ] > for MetadataValue < VE > {
337
+ type Error = InvalidMetadataValueBytes ;
338
+
339
+ #[ inline]
340
+ fn try_from ( src : & [ u8 ; N ] ) -> Result < Self , Self :: Error > {
341
+ Self :: try_from ( src. as_ref ( ) )
342
+ }
343
+ }
344
+
345
+ /// Attempt to convert a `Bytes` buffer to a `MetadataValue`.
346
+ ///
347
+ /// For `MetadataValue<Ascii>`, if the argument contains invalid metadata
348
+ /// value bytes, an error is returned. Only byte values between 32 and 255
349
+ /// (inclusive) are permitted, excluding byte 127 (DEL).
350
+ ///
351
+ /// For `MetadataValue<Binary>`, if the argument is not valid base64, an
352
+ /// error is returned. In use cases where the input is not base64 encoded,
353
+ /// use `from_bytes`; if the value has to be encoded it's not possible to
354
+ /// share the memory anyways.
355
+ impl < VE : ValueEncoding > TryFrom < Bytes > for MetadataValue < VE > {
356
+ type Error = InvalidMetadataValueBytes ;
357
+
358
+ #[ inline]
359
+ fn try_from ( src : Bytes ) -> Result < Self , Self :: Error > {
360
+ VE :: from_shared ( src) . map ( |value| MetadataValue {
361
+ inner : value,
362
+ phantom : PhantomData ,
363
+ } )
364
+ }
365
+ }
366
+
367
+ /// Attempt to convert a Vec of bytes to a `MetadataValue`.
368
+ ///
369
+ /// For `MetadataValue<Ascii>`, if the argument contains invalid metadata
370
+ /// value bytes, an error is returned. Only byte values between 32 and 255
371
+ /// (inclusive) are permitted, excluding byte 127 (DEL).
372
+ ///
373
+ /// For `MetadataValue<Binary>`, if the argument is not valid base64, an
374
+ /// error is returned. In use cases where the input is not base64 encoded,
375
+ /// use `from_bytes`; if the value has to be encoded it's not possible to
376
+ /// share the memory anyways.
377
+ impl < VE : ValueEncoding > TryFrom < Vec < u8 > > for MetadataValue < VE > {
378
+ type Error = InvalidMetadataValueBytes ;
379
+
380
+ #[ inline]
381
+ fn try_from ( src : Vec < u8 > ) -> Result < Self , Self :: Error > {
382
+ Self :: try_from ( src. as_slice ( ) )
383
+ }
384
+ }
385
+
386
+ /// Attempt to convert a string to a `MetadataValue<Ascii>`.
387
+ ///
388
+ /// If the argument contains invalid metadata value characters, an error is
389
+ /// returned. Only visible ASCII characters (32-127) are permitted. Use
390
+ /// `from_bytes` to create a `MetadataValue` that includes opaque octets
391
+ /// (128-255).
392
+ impl < ' a > TryFrom < & ' a str > for MetadataValue < Ascii > {
393
+ type Error = InvalidMetadataValue ;
394
+
395
+ #[ inline]
396
+ fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
397
+ s. parse ( )
398
+ }
399
+ }
400
+
401
+ /// Attempt to convert a string to a `MetadataValue<Ascii>`.
402
+ ///
403
+ /// If the argument contains invalid metadata value characters, an error is
404
+ /// returned. Only visible ASCII characters (32-127) are permitted. Use
405
+ /// `from_bytes` to create a `MetadataValue` that includes opaque octets
406
+ /// (128-255).
407
+ impl < ' a > TryFrom < & ' a String > for MetadataValue < Ascii > {
408
+ type Error = InvalidMetadataValue ;
409
+
410
+ #[ inline]
411
+ fn try_from ( s : & ' a String ) -> Result < Self , Self :: Error > {
412
+ s. parse ( )
413
+ }
414
+ }
415
+
416
+ /// Attempt to convert a string to a `MetadataValue<Ascii>`.
417
+ ///
418
+ /// If the argument contains invalid metadata value characters, an error is
419
+ /// returned. Only visible ASCII characters (32-127) are permitted. Use
420
+ /// `from_bytes` to create a `MetadataValue` that includes opaque octets
421
+ /// (128-255).
422
+ impl TryFrom < String > for MetadataValue < Ascii > {
423
+ type Error = InvalidMetadataValue ;
424
+
425
+ #[ inline]
426
+ fn try_from ( s : String ) -> Result < Self , Self :: Error > {
427
+ s. parse ( )
428
+ }
429
+ }
430
+
285
431
// is_empty is defined in the generic impl block above
286
432
#[ allow( clippy:: len_without_is_empty) ]
287
433
impl MetadataValue < Ascii > {
@@ -292,9 +438,6 @@ impl MetadataValue<Ascii> {
292
438
/// `from_bytes` to create a `MetadataValue` that includes opaque octets
293
439
/// (128-255).
294
440
///
295
- /// This function is intended to be replaced in the future by a `TryFrom`
296
- /// implementation once the trait is stabilized in std.
297
- ///
298
441
/// # Examples
299
442
///
300
443
/// ```
@@ -311,14 +454,10 @@ impl MetadataValue<Ascii> {
311
454
/// assert!(val.is_err());
312
455
/// ```
313
456
#[ allow( clippy:: should_implement_trait) ]
457
+ #[ deprecated = "Use TryFrom or FromStr instead" ]
314
458
#[ inline]
315
459
pub fn from_str ( src : & str ) -> Result < Self , InvalidMetadataValue > {
316
- HeaderValue :: from_str ( src)
317
- . map ( |value| MetadataValue {
318
- inner : value,
319
- phantom : PhantomData ,
320
- } )
321
- . map_err ( |_| InvalidMetadataValue :: new ( ) )
460
+ src. parse ( )
322
461
}
323
462
324
463
/// Converts a MetadataKey into a MetadataValue<Ascii>.
@@ -330,8 +469,9 @@ impl MetadataValue<Ascii> {
330
469
///
331
470
/// ```
332
471
/// # use tonic::metadata::*;
472
+ /// # use std::convert::TryFrom;
333
473
/// let val = AsciiMetadataValue::from_key::<Ascii>("accept".parse().unwrap());
334
- /// assert_eq!(val, AsciiMetadataValue::try_from_bytes (b"accept").unwrap());
474
+ /// assert_eq!(val, AsciiMetadataValue::try_from (b"accept").unwrap());
335
475
/// ```
336
476
#[ inline]
337
477
pub fn from_key < KeyVE : ValueEncoding > ( key : MetadataKey < KeyVE > ) -> Self {
@@ -402,8 +542,8 @@ impl MetadataValue<Binary> {
402
542
/// ```
403
543
#[ inline]
404
544
pub fn from_bytes ( src : & [ u8 ] ) -> Self {
405
- // Only the Ascii version of try_from_bytes can fail.
406
- Self :: try_from_bytes ( src) . unwrap ( )
545
+ // Only the Ascii version of try_from can fail.
546
+ Self :: try_from ( src) . unwrap ( )
407
547
}
408
548
}
409
549
@@ -501,7 +641,7 @@ mod from_metadata_value_tests {
501
641
502
642
assert_eq ! (
503
643
map. get( "accept" ) . unwrap( ) ,
504
- AsciiMetadataValue :: try_from_bytes ( b"hello-world" ) . unwrap( )
644
+ AsciiMetadataValue :: try_from ( b"hello-world" ) . unwrap( )
505
645
) ;
506
646
}
507
647
}
@@ -511,7 +651,12 @@ impl FromStr for MetadataValue<Ascii> {
511
651
512
652
#[ inline]
513
653
fn from_str ( s : & str ) -> Result < MetadataValue < Ascii > , Self :: Err > {
514
- MetadataValue :: < Ascii > :: from_str ( s)
654
+ HeaderValue :: from_str ( s)
655
+ . map ( |value| MetadataValue {
656
+ inner : value,
657
+ phantom : PhantomData ,
658
+ } )
659
+ . map_err ( |_| InvalidMetadataValue :: new ( ) )
515
660
}
516
661
}
517
662
@@ -730,7 +875,7 @@ fn test_debug() {
730
875
] ;
731
876
732
877
for & ( value, expected) in cases {
733
- let val = AsciiMetadataValue :: try_from_bytes ( value. as_bytes ( ) ) . unwrap ( ) ;
878
+ let val = AsciiMetadataValue :: try_from ( value. as_bytes ( ) ) . unwrap ( ) ;
734
879
let actual = format ! ( "{:?}" , val) ;
735
880
assert_eq ! ( expected, actual) ;
736
881
}
@@ -760,7 +905,7 @@ fn test_is_empty() {
760
905
761
906
#[ test]
762
907
fn test_from_shared_base64_encodes ( ) {
763
- let value = BinaryMetadataValue :: from_shared ( Bytes :: from_static ( b"Hello" ) ) . unwrap ( ) ;
908
+ let value = BinaryMetadataValue :: try_from ( Bytes :: from_static ( b"Hello" ) ) . unwrap ( ) ;
764
909
assert_eq ! ( value. as_encoded_bytes( ) , b"SGVsbG8" ) ;
765
910
}
766
911
0 commit comments