tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.78k stars 489 forks source link

Generate `From<>` cast automatically for `protobuf::one` #980

Open gwbres opened 6 months ago

gwbres commented 6 months ago

Hello,

It would be great if the code generator would auto implement From<> anytime it encounters a protobuf::oneof.

I find myself implementing From<> all the time to simplify how to use the auto generated code.

If I understand correctly, anytime we encounter protobuf::oneof we get an object of this form:

pub struct Object {
    #[prost(oneof = "inner::Value", tags = xxx)]
    pub value: ::core:option:Option<inner::Value>,
}

So I wind up writing casts like this all the time

impl From<inner::Value::X> for Object {
   fn from(value: inner::Value::X) -> Object {
      Object {
           value: Some(v),
      }
   }
}