我遇到了错误“无法在特征对象上调用该方法”,并带有单独的特征实现。
这是我的最小可复制示例:
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())
}
}
integrations / 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中存在的错误?怎样才能以更好,更朴素的方式实现这一目标?
答案 0 :(得分:2)
有一个 解决问题的简单方法:由于您已经在自己的箱子中定义了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
以外的类型重用实现。虽然这不适用于所有人,但可能可以满足您的用例。