gpt4 book ai didi

Rust 解包枚举的内容

转载 作者:行者123 更新时间:2023-12-03 11:28:24 25 4
gpt4 key购买 nike

Rust noob here,我一直在尝试从 grpc 调用中解压缩此枚举(尝试访问 Vec<u8> 字节),但到目前为止我不明白如何访问基础数据。
这是原型(prototype):

message PublishRequest {
string topic = 1;
oneof optional_content {
bytes content = 2;
}
}

message KafkaResponse {
bool success = 1;
oneof optional_content {
string content = 2;
}

}

service KafkaStream {
rpc Subscribe(stream PublishRequest) returns (stream KafkaResponse) {};
}
Rust 代码(我试图达到 PublishRequest.optional_content.content ):
let output = async_stream::try_stream! {
while let Some(publication) = stream.next().await {
let message = publication?;
let topic = message.topic.clone();
if consumer_created_flag == false {
stream_topic = topic.clone();
consumer = Some(create_kafka_consumer(topic));
producer = Some(create_kafka_producer());
consumer_created_flag = true;
}
let content : Vec<u8> = match message.optional_content.clone() {
Some(message_content) => vec![], //trying to access content here instead
None =>vec![],
};

let reply = bridge::KafkaResponse {
content: format!("Hello {}!", "world"),
};
yield reply.clone();
}
};
message.optional_contentstd::option::Option<bridge::publish_request::OptionalContent> 类型
底层枚举(来自 codegen):
pub mod publish_request {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum OptionalContent {
#[prost(bytes, tag = "2")]
Content(::prost::alloc::vec::Vec<u8>),
}
}

最佳答案

您可以匹配 整型 :

let content : Vec<u8> = match message.optional_content.clone() {
Some(OptionalContent::Content(message_content)) => message_content,
None =>vec![],
};
您可以查看 documentation 中的示例

关于Rust 解包枚举的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66268838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com