gpt4 book ai didi

rust - 无法将 `&Thing` 与 `Thing` 进行比较

转载 作者:行者123 更新时间:2023-11-29 08:34:23 27 4
gpt4 key购买 nike

我知道错误的含义,但我无法修复它。我正在使用 mockers 来测试我的工作,但在尝试验证提供给模拟特征函数的结构参数时遇到了困难。简化代码:

#[cfg(test)]
extern crate mockers;
#[cfg(test)]
extern crate mockers_derive;

#[cfg(test)]
use mockers_derive::mocked;

#[derive(Ord, PartialOrd, Eq, PartialEq, Debug)]
pub struct Thing {
pub key: String,
pub class: String,
}

#[cfg_attr(test, mocked)]
pub trait DaoTrait {
fn get(&self, thing: &Thing) -> String;
}

struct DataService {
dao: Box<DaoTrait>,
}

impl DataService {
pub fn get(&self, thing: &Thing) -> String {
self.dao.get(thing)
}
}

#[cfg(test)]
mod test {
use super::*;
use mockers::matchers::eq;
use mockers::Scenario;

#[test]
fn my_test() {
use mockers::matchers::check;
let scenario = Scenario::new();
let mut dao = scenario.create_mock_for::<DaoTrait>();
let thing = Thing {
key: "my test".to_string(),
class: "for test".to_string(),
};

scenario.expect(
dao.get_call(check(|t: &Thing| t.to_owned() == thing))
.and_return("hello".to_string()),
);
let testee = DataService { dao: Box::new(dao) };

let rtn = testee.get(&thing);
assert_eq!(rtn, "hello");
}
}

我得到了错误:

warning: unused import: `mockers::matchers::eq`
--> src/main.rs:33:9
|
33 | use mockers::matchers::eq;
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

error[E0277]: can't compare `&Thing` with `Thing`
--> src/main.rs:47:57
|
47 | dao.get_call(check(|t: &Thing| t.to_owned() == thing))
| ^^ no implementation for `&Thing == Thing`
|
= help: the trait `std::cmp::PartialEq<Thing>` is not implemented for `&Thing`

error[E0277]: the trait bound `mockers::matchers::BoolFnMatchArg<Thing, [closure@src/main.rs:47:32: 47:65 thing:_]>: mockers::MatchArg<&Thing>` is not satisfied
--> src/main.rs:47:17
|
47 | dao.get_call(check(|t: &Thing| t.to_owned() == thing))
| ^^^^^^^^ the trait `mockers::MatchArg<&Thing>` is not implemented for `mockers::matchers::BoolFnMatchArg<Thing, [closure@src/main.rs:47:32: 47:65 thing:_]>`
|
= help: the following implementations were found:
<mockers::matchers::BoolFnMatchArg<T, F> as mockers::MatchArg<T>>

我查看了检查的源代码:

pub fn check<T, F: Fn(&T) -> bool>(f: F) -> BoolFnMatchArg<T, F> {
BoolFnMatchArg { func: f, _phantom: PhantomData }
}

我认为闭包 |t: &Thing| t.to_owned() == thing 我给的是对的。我还尝试了以下闭包,但都没有用。

|t: &Thing| t == &thing
|t: &Thing| *t == thing
|t: Thing| t == thing

Cargo.toml:

[dev-dependencies]
mockers = "0.12.1"
mockers_derive = "0.12.1"

最佳答案

您不能使用 PartialEq 的默认推导将 Thing&Thing 进行比较:

#[derive(Debug, PartialEq)]
struct Thing(String);

fn main() {
let t_val = Thing(String::new());
let t_ref = &t_val;

t_val == t_ref;
}
error[E0308]: mismatched types
--> src/main.rs:8:14
|
8 | t_val == t_ref;
| ^^^^^ expected struct `Thing`, found &Thing
|
= note: expected type `Thing`
found type `&Thing`

要修复该错误,您需要执行以下两项操作之一:

  1. 匹配引用水平:

    • t_val == *t_ref

    • &t_val == t_ref

  2. 为不匹配数量的引用实现相等:

    impl<'a> PartialEq<&'a Thing> for Thing {
    fn eq(&self, other: &&'a Thing) -> bool {
    self == *other
    }
    }

    impl<'a> PartialEq<Thing> for &'a Thing {
    fn eq(&self, other: &Thing) -> bool {
    *self == other
    }
    }

但是,这些都不能解决您的实际问题。你误解了 mockers 库的工作原理;您的闭包采用了错误的引用级别,它需要获得要比较的值的所有权:

let expected_thing = thing.clone();
scenario.expect(
dao.get_call(check(move |t: &&Thing| t == &&expected_thing))
.and_return("hello".to_string()),
);

关于rust - 无法将 `&Thing` 与 `Thing` 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com