Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't mock Send traits #18

Open
asomers opened this issue Aug 3, 2018 · 4 comments
Open

Can't mock Send traits #18

asomers opened this issue Aug 3, 2018 · 4 comments
Assignees

Comments

@asomers
Copy link

asomers commented Aug 3, 2018

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`
@DonaldWhyte
Copy link
Owner

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
Copy link
Owner

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

@krojew
Copy link

krojew commented Nov 14, 2019

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

@DonaldWhyte
Copy link
Owner

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 DonaldWhyte self-assigned this Dec 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants