double icon indicating copy to clipboard operation
double copied to clipboard

Can't mock Send traits

Open asomers opened this issue 7 years ago • 4 comments

Double cannot mock a trait that must be Send. Example:

    fn send() {
        pub trait A {
            fn foo(&self);
        }

        mock_trait!(
            MockA,
            foo() -> ()
        );
        impl A for MockA {
            mock_method!(foo(&self));
        }
        let mock = MockA::default();
        let _ = Box::new(mock) as Box<A + Send>;
    }

gives the following error:

error[E0277]: `std::rc::Rc<std::cell::RefCell<std::collections::HashMap<(), fn(())>>>` cannot be sent between threads safely
   --> src/t_double.rs:388:17
    |
388 |         let _ = Box::new(mock) as Box<A + Send>;
    |                 ^^^^^^^^^^^^^^ `std::rc::Rc<std::cell::RefCell<std::collections::HashMap<(), fn(())>>>` cannot be sent between threads safely               
    |
    = help: within `<t_double::t::MockDouble as TestSuite>::send::MockA`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::RefCell<std::collections::HashMap<(), fn(())>>>`
    = note: required because it appears within the type `double::Mock<(), ()>`
    = note: required because it appears within the type `<t_double::t::MockDouble as TestSuite>::send::MockA`
    = note: required for the cast to the object type `dyn <t_double::t::MockDouble as TestSuite>::send::A + std::marker::Send`

asomers avatar Aug 03 '18 19:08 asomers

Ack. I think it should be possible to support this. My initial thinking is to simply every private member variable of the generated mock struct in an Arc<Mutex<T>>. This makes the generated mock implementation more complex, but means threaded code can use mocks too.

DonaldWhyte avatar Sep 22 '19 14:09 DonaldWhyte

I intend to spend some time working on this next weekend.

DonaldWhyte avatar Sep 22 '19 14:09 DonaldWhyte

Is there any progress? This crate would be perfect if only it supported Sync + Send.

krojew avatar Nov 14 '19 19:11 krojew

Progress has been made. I have WIP (work in progress) branch that is mostly working. I plan to finish this feature and deploy it to a new version of the double crate next weekend.

DonaldWhyte avatar Dec 07 '19 15:12 DonaldWhyte