Skip to content

Commit 55fa803

Browse files
author
Adam Chalmers
committed
chore(tonic): Add TryFrom implementations for MetadataValue
1 parent 2112ecc commit 55fa803

File tree

6 files changed

+180
-35
lines changed

6 files changed

+180
-35
lines changed

examples/src/authentication/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tonic::{metadata::MetadataValue, transport::Channel, Request};
99
async fn main() -> Result<(), Box<dyn std::error::Error>> {
1010
let channel = Channel::from_static("http://[::1]:50051").connect().await?;
1111

12-
let token = MetadataValue::from_str("Bearer some-auth-token")?;
12+
let token: MetadataValue<_> = "Bearer some-auth-token".parse()?;
1313

1414
let mut client = EchoClient::with_interceptor(channel, move |mut req: Request<()>| {
1515
req.metadata_mut().insert("authorization", token.clone());

examples/src/authentication/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5959
}
6060

6161
fn check_auth(req: Request<()>) -> Result<Request<()>, Status> {
62-
let token = MetadataValue::from_str("Bearer some-secret-token").unwrap();
62+
let token: MetadataValue<_> = "Bearer some-secret-token".parse().unwrap();
6363

6464
match req.metadata().get("authorization") {
6565
Some(t) if token == t => Ok(req),

examples/src/gcp/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2222
.ok_or_else(|| "Expected a project name as the first argument.".to_string())?;
2323

2424
let bearer_token = format!("Bearer {}", token);
25-
let header_value = MetadataValue::from_str(&bearer_token)?;
25+
let header_value: MetadataValue<_> = bearer_token.parse()?;
2626

2727
let certs = tokio::fs::read("examples/data/gcp/roots.pem").await?;
2828

interop/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub async fn unimplemented_service(
342342

343343
pub async fn custom_metadata(client: &mut TestClient, assertions: &mut Vec<TestAssertion>) {
344344
let key1 = "x-grpc-test-echo-initial";
345-
let value1 = MetadataValue::from_str("test_initial_metadata_value").unwrap();
345+
let value1: MetadataValue<_> = "test_initial_metadata_value".parse().unwrap();
346346
let key2 = "x-grpc-test-echo-trailing-bin";
347347
let value2 = MetadataValue::from_bytes(&[0xab, 0xab, 0xab]);
348348

tonic/src/metadata/value.rs

Lines changed: 175 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::key::MetadataKey;
77

88
use bytes::Bytes;
99
use http::header::HeaderValue;
10+
use std::convert::TryFrom;
1011
use std::error::Error;
1112
use std::hash::{Hash, Hasher};
1213
use std::marker::PhantomData;
@@ -86,9 +87,6 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
8687
/// For Binary metadata values this method cannot fail. See also the Binary
8788
/// only version of this method `from_bytes`.
8889
///
89-
/// This function is intended to be replaced in the future by a `TryFrom`
90-
/// implementation once the trait is stabilized in std.
91-
///
9290
/// # Examples
9391
///
9492
/// ```
@@ -105,11 +103,9 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
105103
/// assert!(val.is_err());
106104
/// ```
107105
#[inline]
106+
#[deprecated = "Use TryFrom instead"]
108107
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)
113109
}
114110

115111
/// Attempt to convert a `Bytes` buffer to a `MetadataValue`.
@@ -122,15 +118,10 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
122118
/// error is returned. In use cases where the input is not base64 encoded,
123119
/// use `from_bytes`; if the value has to be encoded it's not possible to
124120
/// 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.
128121
#[inline]
122+
#[deprecated = "Use TryFrom instead"]
129123
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)
134125
}
135126

136127
/// Convert a `Bytes` directly into a `MetadataValue` without validating.
@@ -282,6 +273,161 @@ impl<VE: ValueEncoding> MetadataValue<VE> {
282273
}
283274
}
284275

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+
285431
// is_empty is defined in the generic impl block above
286432
#[allow(clippy::len_without_is_empty)]
287433
impl MetadataValue<Ascii> {
@@ -292,9 +438,6 @@ impl MetadataValue<Ascii> {
292438
/// `from_bytes` to create a `MetadataValue` that includes opaque octets
293439
/// (128-255).
294440
///
295-
/// This function is intended to be replaced in the future by a `TryFrom`
296-
/// implementation once the trait is stabilized in std.
297-
///
298441
/// # Examples
299442
///
300443
/// ```
@@ -311,14 +454,10 @@ impl MetadataValue<Ascii> {
311454
/// assert!(val.is_err());
312455
/// ```
313456
#[allow(clippy::should_implement_trait)]
457+
#[deprecated = "Use TryFrom or FromStr instead"]
314458
#[inline]
315459
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()
322461
}
323462

324463
/// Converts a MetadataKey into a MetadataValue<Ascii>.
@@ -330,8 +469,9 @@ impl MetadataValue<Ascii> {
330469
///
331470
/// ```
332471
/// # use tonic::metadata::*;
472+
/// # use std::convert::TryFrom;
333473
/// 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());
335475
/// ```
336476
#[inline]
337477
pub fn from_key<KeyVE: ValueEncoding>(key: MetadataKey<KeyVE>) -> Self {
@@ -402,8 +542,8 @@ impl MetadataValue<Binary> {
402542
/// ```
403543
#[inline]
404544
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()
407547
}
408548
}
409549

@@ -501,7 +641,7 @@ mod from_metadata_value_tests {
501641

502642
assert_eq!(
503643
map.get("accept").unwrap(),
504-
AsciiMetadataValue::try_from_bytes(b"hello-world").unwrap()
644+
AsciiMetadataValue::try_from(b"hello-world").unwrap()
505645
);
506646
}
507647
}
@@ -511,7 +651,12 @@ impl FromStr for MetadataValue<Ascii> {
511651

512652
#[inline]
513653
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())
515660
}
516661
}
517662

@@ -730,7 +875,7 @@ fn test_debug() {
730875
];
731876

732877
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();
734879
let actual = format!("{:?}", val);
735880
assert_eq!(expected, actual);
736881
}
@@ -760,7 +905,7 @@ fn test_is_empty() {
760905

761906
#[test]
762907
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();
764909
assert_eq!(value.as_encoded_bytes(), b"SGVsbG8");
765910
}
766911

tonic/src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl<T> Request<T> {
285285
///
286286
/// [the spec]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
287287
pub fn set_timeout(&mut self, deadline: Duration) {
288-
let value = MetadataValue::from_str(&duration_to_grpc_timeout(deadline)).unwrap();
288+
let value: MetadataValue<_> = duration_to_grpc_timeout(deadline).parse().unwrap();
289289
self.metadata_mut()
290290
.insert(crate::metadata::GRPC_TIMEOUT_HEADER, value);
291291
}

0 commit comments

Comments
 (0)