简体   繁体   English

编译器不知道提供的特征方法

[英]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文件:

foo.proto foo.proto 文件

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 在编译时生成的类型:

foo.rs

#[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错误。

main.rs主要.rs

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: (编辑)供参考:

Cargo.toml货物.toml

[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM