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

Allow mocking functions with unknown size type bounds #421

Merged
merged 4 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
methods, not generic ones. Among other effects, this prevents "unused method
expect" warnings from the latest nightly compiler.
([#415](https://github.com/asomers/mockall/pull/415))
- Methods with unknown size type bounds can now be mocked.
([#421](https://github.com/asomers/mockall/pull/421))

## [ 0.11.2 ] - 2022-07-24

Expand Down
2 changes: 1 addition & 1 deletion mockall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ pub struct Key(any::TypeId);

#[doc(hidden)]
impl Key {
pub fn new<T: 'static>() -> Self {
pub fn new<T: 'static + ?Sized>() -> Self {
Key(any::TypeId::of::<T>())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// vim: tw=80
//! generic methods with unknown size bounds on their generic parameters
#![deny(warnings)]

use mockall::*;

#[automock]
trait Foo {
fn foo<T: 'static + ?Sized>(&self, input: Box<T>);
}

trait Bar {
fn get(&self) -> u32;
}

struct Foobar {
value: u32,
}

impl Bar for Foobar {
fn get(&self) -> u32 {
self.value
}
}

#[test]
fn withf() {
let mut mock = MockFoo::new();
mock.expect_foo::<dyn Bar>()
.withf(|x| x.get() == 42)
.return_const(());

mock.foo::<dyn Bar>(Box::new(Foobar { value: 42 }));
}