生锈补品和prost_types转换
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅通过查看 docs ,我们可以看到<,我们可以看到<,我们可以看到<代码>值是一个带有单个公共字段
类型
的结构,该结构是类型option&lt; kint&gt;
的结构。文档说,如果此字段为无
,则表示错误。如果您查看 > ,显然这是存储实际数据的地方。因此,如果您想初始化类型value
的内容,那么您想执行以下操作,以and of
类型
的适当变体替换为用例:原因:原因您的
try_from
解决方案无效的是,除了默认的毯子实现之外,没有其他tryfrom
实现value
。Just from looking at the docs, we can see that
Value
is a struct with a single public fieldkind
which is of typeOption<Kind>
. The docs say that if this field isNone
then that indicates an error. If you look at the docs forKind
, then it's apparent this is where the actual data is stored. So if you want to initialize something of typeValue
then you'd want to do something like the following, substituting in the appropriate variant ofKind
for your use case:The reason that your
try_from
solution didn't work is because there are noTryFrom
implementations forValue
other than the default blanket implementation.