为什么一个明显已经实现的类型没有实现一个特性?

为什么一个明显已经实现的类型没有实现一个特性?

我试着用柴油查询MySQL数据库并使用工具栏模板显示结果火箭.

我把这个放进去了模型

#[derive(Queryable, Serialize)]pub struct Post {
    pub id: i32,
    pub title: String,
    pub text: String,
    pub published: bool,}

cargo run产出如下:

  --> src/main.rs:69:5
   |
69 |     Template::render("index", &results)
   |     ^^^^^^^^^^^^^^^^ the trait `serde::ser::Serialize` is not implemented for `tasty::models::Post`
   |
   = note: required because of the requirements on the impl of `serde::ser::Serialize` for `std::vec::Vec<tasty::models::Post>`
   = note: required by `rocket_contrib::Template::render`

在我的箱子里,我有一个:

[dependencies]chrono = "0.3.0"rocket = "0.2.8"rocket_codegen = "0.2.8"serde = "1.0.8"serde_derive = "1.0.8"serde_json = "1.0.2"mysql = "11.1.2"diesel = { version = "0.13.0", features = ["mysql","chrono"] }diesel_codegen = { version = "0.13.0", features = ["mysql"] }dotenv = "0.10.0"[dependencies.rocket_contrib]version = "*"default-features = falsefeatures = ["handlebars_templates"]

我读到过迪塞尔不支持Serialize但我不确定。


holdtom
浏览 686回答 1
1回答

FFIVE

一般的问题是,代码有多个版本的机箱,每个版本提供了不同版本的特征。事实上,Rust允许这是一个好东西,&nbsp;但是它周围的错误信息令人困惑。.你的箱子Serialize但是库使用的是B版在公共接口中..这些特性不兼容,所以当您传递类型时Serialize@A对所需的职能Serialize@B编译器会阻止您。虽然您的例子是关于不同的特性,但这也有可能发生在类型已经从一个板条箱再出口。货物树对于验证这是您的问题非常有用。它显示了所有的依赖项及其版本。它甚至有一个-d标志显示重复的依赖关系!这里没有显示这种模式,但是非常有用。一般的解决方案是手动限制Cargo.toml中的Serde版本,以匹配其余的依赖项:serde&nbsp;=&nbsp;"0.9" serde_derive&nbsp;=&nbsp;"0.9" serde_json&nbsp;=&nbsp;"0.9"这可能并不总是可能的,在这种情况下,您可能需要跟踪机箱所有者来升级他们的依赖关系。工作实例火箭[dependencies] chrono&nbsp;=&nbsp;"0.3.0" rocket&nbsp;=&nbsp;"0.2.8" rocket_codegen&nbsp;=&nbsp;"0.2.8" serde&nbsp;=&nbsp;"1.0.8" serde_derive&nbsp;=&nbsp;"1.0.8" serde_json&nbsp;=&nbsp;"1.0.2" mysql&nbsp;=&nbsp;"11.1.2" diesel&nbsp;=&nbsp;{&nbsp;version&nbsp;=&nbsp;"0.13.0",&nbsp;features&nbsp;=&nbsp;["mysql","chrono"]&nbsp;} diesel_codegen&nbsp;=&nbsp;{&nbsp;version&nbsp;=&nbsp;"0.13.0",&nbsp;features&nbsp;=&nbsp;["mysql"]&nbsp;} dotenv&nbsp;=&nbsp;"0.10.0" [dependencies.rocket_contrib] version&nbsp;=&nbsp;"*" default-features&nbsp;=&nbsp;false features&nbsp;=&nbsp;["handlebars_templates"]火箭_cont肋骨0.2.8依赖于Serde0.9,但您已经使用了Serde1.0。这段经删节的片段cargo tree显示问题:reproduction&nbsp;v0.1.0&nbsp;(file:///private/tmp/reproduction) ├──&nbsp;rocket_contrib&nbsp;v0.2.8 │&nbsp;&nbsp;&nbsp;├──&nbsp;handlebars&nbsp;v0.25.3 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;└──&nbsp;serde_json&nbsp;v0.9.10 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└──&nbsp;serde&nbsp;v0.9.15 │&nbsp;&nbsp;&nbsp;├──&nbsp;serde&nbsp;v0.9.15&nbsp;(*) │&nbsp;&nbsp;&nbsp;└──&nbsp;serde_json&nbsp;v0.9.10&nbsp;(*) ├──&nbsp;serde&nbsp;v1.0.8&nbsp;(*) ├──&nbsp;serde_derive&nbsp;v1.0.8 │&nbsp;&nbsp;&nbsp;├──&nbsp;serde_derive_internals&nbsp;v0.15.1 └──&nbsp;serde_json&nbsp;v1.0.2&nbsp;(*)这个即将推出的0.3版火箭应该允许使用Serde 1.0。铁/BSON/MongoDB[dependencies] bodyparser&nbsp;=&nbsp;"0.5" bson&nbsp;=&nbsp;"0.8" iron&nbsp;=&nbsp;"0.5" jwt&nbsp;=&nbsp;"0.4" mongodb&nbsp;=&nbsp;"0.3" router&nbsp;=&nbsp;"0.5" rust-crypto&nbsp;=&nbsp;"0.2" serde&nbsp;=&nbsp;"1.0" serde_derive&nbsp;=&nbsp;"1.0" serde_json&nbsp;=&nbsp;"1.0" time&nbsp;=&nbsp;"0.1"车身解析器0.5依赖于Serde 0.8,MongoDB在0.9中,而Cate和BSON则在Serde 1.0中。这段经删节的片段cargo tree显示问题:reproduction&nbsp;v0.1.0&nbsp;(file:///private/tmp/reproduction) ├──&nbsp;bodyparser&nbsp;v0.5.0 │&nbsp;&nbsp;&nbsp;├──&nbsp;serde&nbsp;v0.8.23 │&nbsp;&nbsp;&nbsp;└──&nbsp;serde_json&nbsp;v0.8.6 │&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;└──&nbsp;serde&nbsp;v0.8.23&nbsp;(*) ├──&nbsp;bson&nbsp;v0.8.0 │&nbsp;&nbsp;&nbsp;├──&nbsp;serde&nbsp;v1.0.8 │&nbsp;&nbsp;&nbsp;├──&nbsp;serde_json&nbsp;v1.0.2 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;└──&nbsp;serde&nbsp;v1.0.8&nbsp;(*) ├──&nbsp;mongodb&nbsp;v0.3.0 │&nbsp;&nbsp;&nbsp;├──&nbsp;textnonce&nbsp;v0.6.0 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;├──&nbsp;serde&nbsp;v0.9.15 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;└──&nbsp;serde_derive&nbsp;v0.9.15 ├──&nbsp;serde&nbsp;v1.0.8&nbsp;(*) ├──&nbsp;serde_derive&nbsp;v1.0.8 ├──&nbsp;serde_json&nbsp;v1.0.2&nbsp;(*)Body解析器0.7.0应该支持Serde 1.0。这个文本状态不太清楚,但这种依赖性可能是私一个,所以在这种情况下可能不重要。柴油/Chronochrono&nbsp;=&nbsp;"0.4.0"diesel&nbsp;=&nbsp;{&nbsp;version&nbsp;=&nbsp;"0.13.0",&nbsp;features&nbsp;=&nbsp;[&nbsp;"chrono",&nbsp;"sqlite"&nbsp;]&nbsp;}diesel_codegen&nbsp;=&nbsp;{&nbsp;version&nbsp;=&nbsp;"0.13.0",&nbsp;features&nbsp;=&nbsp;[&nbsp;"sqlite"&nbsp;]&nbsp;}dotenv&nbsp;=&nbsp;"0.9.0"目前的Chrono版本为0.4.0,但DUSIC只知道如何序列化Chrono 0.3.0。reproduction&nbsp;v0.1.0&nbsp;(file:///private/tmp/reproduction) ├──&nbsp;chrono&nbsp;v0.4.0 ├──&nbsp;diesel&nbsp;v0.13.0 │&nbsp;&nbsp;&nbsp;├──&nbsp;chrono&nbsp;v0.3.0河豚/分组密码-特性[dependencies]blowfish&nbsp;=&nbsp;"0.2.1"block-cipher-trait&nbsp;=&nbsp;"0.3.0"reproduction&nbsp;v0.1.0&nbsp;(file:///private/tmp/reproduction) ├──&nbsp;block-cipher-trait&nbsp;v0.3.0 │──&nbsp;blowfish&nbsp;v0.2.1 &nbsp;&nbsp;&nbsp;&nbsp;├──&nbsp;block-cipher-trait&nbsp;v0.2.0康罗德/活塞2d-图形[dependencies]piston_window&nbsp;=&nbsp;"0.74.0"conrod&nbsp;=&nbsp;{&nbsp;version&nbsp;=&nbsp;"0.56.0",&nbsp;features&nbsp;=&nbsp;["piston"]&nbsp;}repro&nbsp;v0.1.0&nbsp;(file:///private/tmp/repro) ├──&nbsp;conrod&nbsp;v0.56.0 │&nbsp;&nbsp;&nbsp;├──&nbsp;piston2d-graphics&nbsp;v0.23.0 └──&nbsp;piston_window&nbsp;v0.74.0 &nbsp;&nbsp;&nbsp;&nbsp;├──&nbsp;piston2d-graphics&nbsp;v0.24.0&nbsp;(*)Actix/期货[dependencies]actix-web&nbsp;=&nbsp;"0.6.10"futures&nbsp;=&nbsp;"0.2.1"repro&nbsp;v0.1.0&nbsp;(file:///private/tmp/repro) ├──&nbsp;actix-web&nbsp;v0.6.12 │&nbsp;&nbsp;&nbsp;├──&nbsp;actix&nbsp;v0.5.8 │&nbsp;&nbsp;&nbsp;│&nbsp;&nbsp;&nbsp;├──&nbsp;futures&nbsp;v0.1.21 └──&nbsp;futures&nbsp;v0.2.1光明的未来?RFC 1977建议引入公众和私对货物的依赖。如果您使用的板条箱反过来公开了另一个板条箱的类型,货将确保您使用一个单一的统一版本的板条箱与普通类型。
打开App,查看更多内容
随时随地看视频慕课网APP