[英]Compiler Unaware of Provided Methods of Trait
I'm using the prost
crate in a very "hello world" way, with a basic ProtoBuf file:我以一种非常“hello world”的方式使用 prost crate,带有一个基本的
prost
文件:
syntax = "proto3";
package foo;
message Foo {
string mystring = 1;
}
I can verify the types that prost produces at compile time by inspecting ./target/PROJECT/build/PROJECT-{some hash}/out/foo.rs
:我可以通过检查
./target/PROJECT/build/PROJECT-{some hash}/out/foo.rs
来验证 prost 在编译时生成的类型:
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Foo {
#[prost(string, tag="1")]
pub mystring: ::prost::alloc::string::String,
}
See that the prost::Message
trait gets derived?看到
prost::Message
特性得到派生了吗? Its docs claim I have tons of functions at my disposal for this trait, but only the two required ones, encoded_len()
and clear()
, can be called without error.它的文档声称我有大量函数可供使用,但只有两个必需的函数
encoded_len()
和clear()
可以无错误地调用。 Any of the provided ones (those with default implementations) result in a cannot find function in this scope
error.任何提供的(具有默认实现的)都会导致
cannot find function in this scope
错误。
use prost::Message;
pub mod foo {
include!(concat!(env!("OUT_DIR"), "/foo.rs"));
}
fn main() {
let f = foo::Foo { mystring: "bar".to_string() };
let v = encode_to_vec(&f);
}
cargo run
error[E0425]: cannot find function `encode_to_vec` in this scope | | let v = encode_to_vec(&f); | ^^^^^^^^^^^^^ not found in this scope
And, worryingly, this warning implies it's not even looking at the trait it needs to:而且,令人担忧的是,这个警告暗示它甚至没有考虑它需要的特征:
warning: unused import:
prost::Message
警告:未使用的导入:
prost::Message
(edit) For reference: (编辑)供参考:
[package]
name = "testing"
version = "0.1.0"
edition = "2018"
[dependencies]
prost = { version = "0.7.0", features = ["std"] }
[build-dependencies]
prost-build = "0.7.0"
encode_to_vec
is not a free function. You need to call it as one of encode_to_vec
不是免费的 function。您需要将其称为其中之一
f.encode_to_vec()
foo::Foo::encode_to_vec(&f)
Message::encode_to_vec(&f)
<foo::Foo as Message>::encode_to_vec(&f)
The first method is the most normal, but the other ones may produce useful error messages when you get confused about what's going on.第一种方法是最正常的,但当您对正在发生的事情感到困惑时,其他方法可能会产生有用的错误消息。
[Edit:] Well, encode_to_vec
was added in prost 0.8.0 , so with 0.7.0 , it won't be usable. [编辑:] 好吧,在 prost 0.8.0中添加了
encode_to_vec
,所以在0.7.0中,它将无法使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.