关于单独性状实现"the method cannot be invoked on a trait object"



我在单独的 trait 实现中遇到了错误"无法在 trait 对象上调用该方法"。

这是我的最小可重现示例:

Cargo.toml

[package]
name = "mrp"
version = "0.1.0"
authors = ["Empty2k12"]
edition = "2018"
[dependencies]
futures = "0.1.27"
tokio = "0.1.20"
serde = { version = "1.0.92", features = ["derive"] }
serde_json = { version = "1.0" }

main.rs

use serde::{Deserialize, Serialize};
pub mod integrations {
    pub mod serde_integration;
}
struct MyDbClient {}
#[derive(Serialize, Deserialize, Debug)]
pub struct Weather {
    temperature: i32,
}
#[cfg(test)]
mod tests {
    use super::Weather;
    use super::MyDbClient;
    use crate::integrations::serde_integration::MyDbSerdeORM;
    #[test]
    fn mrp() {
        let weather = Weather { temperature: 82 };
        MyDbClient {}.json_query::<Weather, ToString>(serde_json::to_string(&weather).unwrap())
    }
}

集成/serde_integration.rs

use serde::de::DeserializeOwned;
use super::super::MyDbClient;
use futures::Future;
pub trait MyDbSerdeORM {
    fn json_query<T: 'static, Q>(self, q: Q) -> Box<dyn Future<Item = Option<T>, Error = ()>>
    where
        Q: ToString,
        T: DeserializeOwned;
}
impl MyDbSerdeORM for MyDbClient {
    fn json_query<T: 'static, Q>(self, q: Q) -> Box<dyn Future<Item = Option<T>, Error = ()>>
    where
        Q: ToString,
        T: DeserializeOwned,
    {
        Box::new(futures::future::ok(Some(
            serde_json::from_str(&q.to_string()).unwrap(),
        )))
    }
}
error: the `json_query` method cannot be invoked on a trait object
  --> src/main.rs:27:23
   |
27 |         MyDbClient {}.json_query::<Weather, ToString>(serde_json::to_string(&weather).unwrap())
   |                       ^^^^^^^^^^
   |
   = note: another candidate was found in the following trait, perhaps add a `use` for it:
           `use crate::integrations::serde_integration::MyDbSerdeORM;`

该错误也没有帮助,因为它建议添加已经存在的导入。

如何修复 MRE 中存在的错误?如何以更好、更质朴的方式实现这一点?

有一个简单的解决方法:由于您已经在自己的 crate 中定义了 MyDbClient 结构,因此您可以简单地实现所需的方法,而无需指定其他特征。这将适用于您的原始示例:

use futures::Future;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub struct MyDbClient {
    pub url: String,
}
#[cfg(feature = "serde-orm")]
impl MyDbClient {
    pub fn json_query<T: 'static, Q>(self, q: Q) -> Box<dyn Future<Item = Option<T>, Error = ()>>
    where
        Q: ToString,
        T: DeserializeOwned,
    {
        Box::new(futures::future::ok(Some(
            serde_json::from_str(&q.to_string()).unwrap(),
        )))
    }
}
#[cfg_attr(feature = "serde-orm", derive(Serialize, Deserialize, Debug))]
pub struct Weather {
    temperature: i32,
}
#[cfg(test)]
#[cfg(feature = "serde-orm")]
mod tests {
    use super::*;
    #[test]
    fn mrp() {
        let weather = Weather { temperature: 82 };
        let client: MyDbClient = MyDbClient {
            url: "localhost".to_owned(),
        };
        client.json_query::<Weather, _>(serde_json::to_string(&weather).unwrap());
    }
}

不过,这有一个缺点,因为您不能为MyDbClient以外的类型重用该实现。虽然这并不适合所有人,但这可能可以适应您的用例。

相关内容

最新更新