tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Implement various From and TryFrom for MetadataValue<Ascii>

Open adamchalmers opened this issue 3 years ago • 2 comments
trafficstars

Feature Request

Crates

  • tonic

Motivation

There's many places where the docs say "This function is intended to be replaced in the future by a TryFrom implementation once the trait is stabilized in std" e.g. MetadataValue::from_str. I assume these originally came from copying the Hyper HeaderValue methods, which have the same message, e.g. HeaderValue::from_str.

Integers can be infallibly encoded into ASCII, and so they can be infallibly used as metadata values. But right now there's no API for this -- you have to turn the integer into a string, then use the fallible MetadataValue::from_str method.

Proposal

MetadataValue should impl the same From and TryFrom methods that hyper::HeaderValue does, i.e.

  • From: i16, i32, i64, isize, u16, u32, u64, usize
  • TryFrom: String, &String, &str

Then we should add a deprecation to the special "try_from" or "from" methods that currently exist, which these new From/TryFrom impls replace.

I'm happy to implement this if y'all like the idea.

Alternatives

We could leave the current methods in place, but that's a bit less convenient for programmers.

adamchalmers avatar Apr 27 '22 20:04 adamchalmers

Yeah, I would totally accept a PR for this. I think we should go the route of adding in the new APIs and deprecate the old ones. I think if http also has the same thing we can also do a PR for that. Feel free to tag me in the PR and ill take a look. Thanks!

LucioFranco avatar Apr 28 '22 16:04 LucioFranco

OK, this is confusing, but I think MetadataValue actually does impl From for all the numeric types, using the same from_integers! macro impls that the http crate does. This expands into generating a bunch of From impls for all the numeric types.

The problem is that they don't show up in Rustdoc. I think because of the VE generic type.

adamchalmers avatar Apr 29 '22 16:04 adamchalmers