Skip to content

Commit

Permalink
Fix regression mocking "impl trait" for generic traits
Browse files Browse the repository at this point in the history
The regression was probably introduced by PR #86
  • Loading branch information
asomers committed Dec 6, 2019
1 parent 65e0833 commit 435245a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
21 changes: 21 additions & 0 deletions mockall/tests/mock_impl_generic_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// vim: tw=80

use mockall::*;

trait MyTrait<T> {}

struct MyStruct<T>(T);
impl<T> MyTrait<T> for MyStruct<T> {}

mock!{
Foo {
fn foo<R: 'static>(&self) -> impl MyTrait<R>;
}
}

#[test]
fn returning() {
let mut mock = MockFoo::new();
mock.expect_foo::<u32>().returning(|| Box::new(MyStruct(42u32)));
let _mto: Box<dyn MyTrait<u32>> = mock.foo();
}
14 changes: 12 additions & 2 deletions mockall_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,18 @@ fn find_lifetimes(ty: &Type) -> HashSet<Lifetime> {
}
ret
},
_ => unimplemented!()
Type::ImplTrait(tit) => {
let mut ret = HashSet::default();
for tpb in tit.bounds.iter() {
ret.extend(find_lifetimes_in_tpb(tpb));
}
ret
},
_ => {
compile_error(ty.span(), "unsupported type in this context");
HashSet::default()
}

}
}

Expand Down Expand Up @@ -675,7 +686,6 @@ fn split_lifetimes(
match arg {
FnArg::Typed(pt) => {
alts.extend(find_lifetimes(pt.ty.as_ref()))
//alts.extend(find_lifetimes(pt.ty.as_ref(), args, rt))
},
_ => ()
};
Expand Down

0 comments on commit 435245a

Please sign in to comment.