Can't Convert SystemTime to Prost 0.10/Protobuf Timestamp Type

491 views Asked by At

Can somebody tell me what is happening with the types in tonic and prost? Only a month or two ago, my team had a working build that used a protobuf definition that contained a timestamp. We're compiling them with tonic_build using well known types. Something changed with the types moving from prost 0.7 to 0.10 and now I get this error.

timestamp: Some(timestamp.into()),
                          ^^^^ the trait `From<SystemTime>` is not implemented for `protobuf::Timestamp`

So I used a few lines to convert the type to what I thought was the correct type from the protobuf crate and then got this.

timestamp: Some(timestamp.into()),
                          ^^^^ the trait `From<protobuf::well_known_types::Timestamp>` is not implemented for `protobuf::Timestamp`

I tried importing Timestamp from the protobuf crate, but that type doesn't exist in the root and it is apparently different from protobuf::well_known_types::Timestamp and I can't find what crate or version this type is coming from.

And then finally, if I allow tonic_build to use prost_types, I get this error instead.

timestamp: Some(timestamp.into()),
                          ^^^^ the trait `From<protobuf::well_known_types::Timestamp>` is not implemented for `prost_types::Timestamp`

I am able to import that type, but the documentation on it is confusing, so I haven't figured out how to convert the original SystemTime into the prost_type::Timestamp.

In short, I keep looking but I can't find the correct Timestamp type. I read somewhere that having the correct versions of these crates so they line up correctly is important. Can someone help me figure out how to find the correct type and crate versions?

Thank you so much.

1

There are 1 answers

0
Mika Vatanen On BEST ANSWER

The conversion functionality has moved into prost-types crate.

For reference, see https://docs.rs/prost-types/0.10.1/prost_types/struct.Timestamp.html#impl-From%3CSystemTime%3E and https://github.com/tokio-rs/prost/tree/master/prost-types

timestamp: Some(prost_types::Timestamp::from(timestamp))