Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can it support Ref or Cow variants? #1205

Open
immno opened this issue Dec 13, 2024 · 0 comments
Open

Can it support Ref or Cow variants? #1205

immno opened this issue Dec 13, 2024 · 0 comments

Comments

@immno
Copy link

immno commented Dec 13, 2024

I have a data structure similar to the following:

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MeasureResultProto {
    #[prost(int64, tag = "1")]
    pub id: i64,
    #[prost(int64, tag = "2")]
    pub time: i64,
    #[prost(message, optional, tag = "3")]
    pub data: ::core::option::Option<super::common::DataValueProto>,
    #[prost(message, optional, tag = "4")]
    pub filter: ::core::option::Option<super::common::DataValueProto>,
}

DataValueProto is a complex structure, which may be a basic type or json:

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DataValueProto {
    #[prost(oneof = "data_value_proto::ValueProto", tags = "1, 2, 3, 4, 5")]
    pub value_proto: ::core::option::Option<data_value_proto::ValueProto>,
}
/// Nested message and enum types in `DataValueProto`.
pub mod data_value_proto {
    #[allow(clippy::derive_partial_eq_without_eq)]
    #[derive(Clone, PartialEq, ::prost::Oneof)]
    pub enum ValueProto {
        #[prost(bool, tag = "1")]
        Bool(bool),
        #[prost(int64, tag = "2")]
        Long(i64),
        #[prost(double, tag = "3")]
        Double(f64),
        #[prost(string, tag = "4")]
        String(::prost::alloc::string::String),
        #[prost(string, tag = "5")]
        Json(::prost::alloc::string::String),
    }
}

Can I generate a Ref or Cow structure? DataValueProto will be used in many places. I don't want to clone it all the time. I just want to use encode_to_vec to convert it to Vec<u8>.

#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MeasureResultProtoRef<'a> {
    #[prost(int64, tag = "1")]
    pub id: i64,
    #[prost(int64, tag = "2")]
    pub time: i64,
    #[prost(message, optional, tag = "3")]
    pub data: &'a ::core::option::Option<super::common::DataValueProto>,
    #[prost(message, optional, tag = "4")]
    pub filter: &'a ::core::option::Option<super::common::DataValueProto>,
}

This can save a lot of Clone performance. My idea is very similar to #1202 , who implemented it through Cow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant