无法将“& Thing"与"Thing"进行比较 [英] Can't compare `&Thing` with `Thing`

查看:143
本文介绍了无法将“& Thing"与"Thing"进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道错误的含义,但我无法解决.我正在使用mockers来测试我的工作,尝试验证赋予模拟特征的函数的struct参数时,我陷入了困境.简化的代码:

I know what the error means, but I can't fix it. I'm using mockers to test my work and I got stuck when trying to verify the struct parameter which was given to a mocked trait's function. The simplified code:

#[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>>

我查看了check的源代码:

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

我认为我给出的闭包|t: &Thing| t.to_owned() == thing是正确的.我也尝试了以下关闭,但没有一个起作用.

I think the closure |t: &Thing| t.to_owned() == thing I had given is right. I also tried the following closures, but none of them worked.

|t: &Thing| t == &thing

|t: &Thing| *t == thing

|t: Thing| t == thing

The Cargo.toml:

The Cargo.toml:

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

推荐答案

您无法使用默认派生的PartialEqThing&Thing进行比较:

You cannot compare a Thing to a &Thing using the default derivation of PartialEq:

#[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`

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

To fix that error, you need to do one of two things:

  1. 匹配参考级别:

  1. Match the reference level:

  • t_val == *t_ref

&t_val == t_ref

对引用数量不匹配的实现相等:

Implement equality for a mismatched number of references:

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
    }
}


但是,这些都不能解决您的实际问题.您已经误解了嘲笑库的工作方式;您的闭包引用错误的级别,并且需要对值的所有权进行比较:


However, none of that solves your actual problem. You've misunderstood how the mockers library works; your closure is taking the wrong level of reference, and it needs to take ownership of the value to compare:

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

这篇关于无法将“&amp; Thing"与"Thing"进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆