Can't mock Send traits
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`
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.
I intend to spend some time working on this next weekend.
Is there any progress? This crate would be perfect if only it supported Sync + Send.
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.