【问题标题】:Deserialize is not implemented反序列化未实现
【发布时间】:2021-10-19 03:07:20
【问题描述】:

所以我正在学习如何使用 Rust 构建 REST API。尝试实现以下行时,我不断收到错误消息:

 #[derive(Deserialize, Debug)]
  pub struct Config {
      pub pg: deadpool_postgres::Config,
      pub server: ServerConfig,
}

这是我得到的错误代码:

 error[E0277]: the trait bound `deadpool_postgres::Config:
 config::_::_serde::Deserialize<'_>` is not satisfied

这是我在 Cargo.toml 中的依赖项:

[dependencies]
actix-rt = "2.3.0"
actix-web = "3.3.2"
serde = "1.0.130"
dotenv = "0.15.0"
config = "0.11.0"
tokio-pg-mapper = "0.2.0"
tokio-pg-mapper-derive = "0.2.0"
deadpool-postgres = "0.10.0"
tokio-postgres = "0.7.3"

我是 Rust 的新手,我不确定错误的含义或修复它的方法。我正在实现 deadpool_postgres 就像它在文档中一样,除了在我的文件中我试图从 dotenv 文件中提取数据库变量。这是完整的文件:

use::config::ConfigError;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct Config {
    pub pg: deadpool_postgres::Config,
    pub server: ServerConfig,
}

#[derive(Deserialize, Debug)]
pub struct ServerConfig {
    pub host: String,
    pub port: i32
}

impl Config {
    pub fn from_env() -> Result<Self, ConfigError> {
        let mut cfg = config::Config::new();
        cfg.merge(config::Environment::new())?;
        cfg.try_into()
    }
}

【问题讨论】:

    标签: rust


    【解决方案1】:

    编译器告诉你deadpool_postgres::Config 结构没有实现Deserialize

    您需要在 Cargo.toml 中启用“serde”feature 才能获得此实现(请参阅list of features)。

    这可以这样完成:

    [dependencies]
    deadpool-postgres = { version = "0.10.0", features = ["serde"] }
    

    【讨论】:

    • 谢谢,成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2021-05-14
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多