Rust 实施';std::cmp::PartialEq';这还不够普遍
我试图使用模拟函数,该函数接受我的结构Rust 实施';std::cmp::PartialEq';这还不够普遍,rust,mocking,traits,Rust,Mocking,Traits,我试图使用模拟函数,该函数接受我的结构a的选项,但收到以下错误 error: implementation of `std::cmp::PartialEq` is not general enough --> src\main.rs:45:14 | 45 | .expect_do_feature(|arg| arg.partial_eq(Some(&cfg))) | ^^^^^^^^^^^^^^^^^
a
的选项
,但收到以下错误
error: implementation of `std::cmp::PartialEq` is not general enough
--> src\main.rs:45:14
|
45 | .expect_do_feature(|arg| arg.partial_eq(Some(&cfg)))
| ^^^^^^^^^^^^^^^^^ implementation of `std::cmp::PartialEq` is not general enough
|
= note: `std::cmp::PartialEq<std::option::Option<&'1 A>>` would have to be implemented for the type `std::option::Option<&A>`, for any lifetime `'1`...
= note: ...but `std::cmp::PartialEq` is actually implemented for the type `std::option::Option<&'2 A>`, for some specific lifetime `'2`
为了编译,我在
Cargo.toml
中使用了mockiato=“0.9.5”
。如果您试图复制,请不要忘记使用cargo test
运行此测试。我认为,从派生中,对于具有相同生存期的引用,您只能获得PartialEq
。如果像这里一样,你需要它作为不同生命周期的参考,你需要更高的等级特质界限:@matthiaswiller你建议哪个界限可以更高等级?在PartialEq
impl上没有必需的特征边界,因此它已经尽可能地通用了。我认为这是mockiato本身的一个限制。我会报告mockiato github上的一个问题,看看是否有一个维护人员可以提供帮助——对于一个非常简单的设置来说,这是一个非常严重的错误,因此我希望他们能够提供一些指导,因为他们对内部结构有一定的了解。在检查github上的现有问题后,我认为已经知道了。不幸的是,项目的最后更新是6个月前,所以我不知道是否会有一个解决方案很快。
use std::collections::HashMap;
#[cfg(test)]
use mockiato::mockable;
#[derive(PartialEq)]
pub struct A {
pub data: HashMap<String, i32>,
}
impl A {
pub fn new() -> Self {
A {
data: HashMap::new(),
}
}
}
pub struct B;
#[cfg_attr(test, mockable)]
pub trait ProvidesFeature {
fn do_feature(&self, config: Option<&A>) -> Option<Vec<i32>>;
}
impl B {
pub fn new() -> Self {
B {}
}
pub fn do_feature(&self, config: Option<&A>) -> Option<Vec<i32>> {
Some(vec!())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mocking() {
let mut feature_mock = ProvidesFeatureMock::new();
feature_mock
.expect_do_feature(|arg| arg.partial_eq(None))
.times(1..)
.returns(Some(vec!()));
assert_eq!(vec![0; 0], feature_mock.do_feature(None).unwrap());
}
}
fn main() {
println!("Hello, world!");
}