生锈补品和prost_types转换

发布于 2025-01-28 02:18:49 字数 861 浏览 2 评论 0原文

我正在使用 tonic framework,rust grpc服务器实现。在原始文件中生成的锈蚀代码中,我有一个具有一个字段的结构:

#[prost(message, optional, tag="3")]
pub data: ::core::option::Option<::prost_types::Value>,

从Protobuff字段生成:

google.protobuf.Value data = 3; 

我似乎找不到一种方法data type > PROST_TYPES :: value通过转换我拥有的结构。我正在做类似的事情:

prost_types::Value::try_from(myOwnsStructVar)

但是它不起作用。任何人都使用PROST_TYPES lib之前,并且知道如何将/转换为prost_types :: value

myownsstructvar是一种类型结构。我需要将其转换为prost_types :: struct,这样我就可以做:

prost_types::value::Kind::StructValue(myOwnsStructVarAfterConversiontToProstStruct)

I'm using tonic framework, a rust grpc server implementation. In the generated rust code from the proto file, I have a struct which has a field:

#[prost(message, optional, tag="3")]
pub data: ::core::option::Option<::prost_types::Value>,

generated from a protobuff field:

google.protobuf.Value data = 3; 

I can't seem to find a way to init data which is type prost_types::Value by converting a struct I have. I'm doing something like:

prost_types::Value::try_from(myOwnsStructVar)

But it does not work. Anyone have used prost_types lib before and know how to encode/convert to prost_types::Value

myOwnsStructVar is a type struct. I need to convert it to prost_types::Struct So then I can do:

prost_types::value::Kind::StructValue(myOwnsStructVarAfterConversiontToProstStruct)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇俏 2025-02-04 02:18:50

仅通过查看 docs ,我们可以看到<,我们可以看到<,我们可以看到<代码>值是一个带有单个公共字段类型的结构,该结构是类型option&lt; kint&gt;的结构。文档说,如果此字段为,则表示错误。如果您查看 > ,显然这是存储实际数据的地方。因此,如果您想初始化类型value的内容,那么您想执行以下操作,以and of 类型的适当变体替换为用例:

Value {
    kind: Some(Kind::NumberValue(10.0f64))
}

原因:原因您的try_from解决方案无效的是,除了默认的毯子实现之外,没有其他tryfrom实现value

Just from looking at the docs, we can see that Value is a struct with a single public field kind which is of type Option<Kind>. The docs say that if this field is None then that indicates an error. If you look at the docs for Kind, then it's apparent this is where the actual data is stored. So if you want to initialize something of type Value then you'd want to do something like the following, substituting in the appropriate variant of Kind for your use case:

Value {
    kind: Some(Kind::NumberValue(10.0f64))
}

The reason that your try_from solution didn't work is because there are no TryFrom implementations for Value other than the default blanket implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文