You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mockall is a great crate that's used by lots of projects, but its syntax isn't perfect. It reflects language limitations, historical implementation details, and the legacies of its predecessors. In particular, it has a few oft-noted problems:
#[automock] doesn't work on structs with more than one trait impl, or structs with both a trait impl and inherent methods.
It's hard to pass extra information to mockall to tweak the generated code. Currently, the only way to do this is by:
Metaitems, used for associated types: #[automock(type Key=u16; type Value=i32;)], but this would get cumbersome if Mockall ever supports more types of metaitems.
The #[concretize] attribute, but its implementation is hacky.
Instead, I propose replacing all of Mockall's syntax with a new Derive macro, which would solve both of these problems. The first, it would solve by generating the code in pieces. The second, it would solve by standard helper attributes. Its syntax would look like this:
// For traits#[derive(Mock)]pubtraitFoo{#[mockall::as(u32)]typeAddress;fnfoo(&self,a:Self::Address);#[mockall::concretize]fnbar<T>(&self,t:T) -> u32;}// For structs#[derive(Mock)]#[mockall::trait_impls(Iterator)]structFoo{
fn foo(&self){// ...}}#[derive(Mock)]implIteratorforFoo{typeItem = u32;fnnext(&mutself) -> Option<Self::Item>{// ...}}
With this syntax, mock! would only be necesary when mocking structs or traits from external traits. Also, it would be easy to add more helper attributes. For example:
#[concretize], which already exists
#[as], for mocking traits with associated constants
I doubt I'll hear any objections to these new features, so what remains is mainly a question of deciding how to name things. That and, of course, finding the time to implement it all ;)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Mockall is a great crate that's used by lots of projects, but its syntax isn't perfect. It reflects language limitations, historical implementation details, and the legacies of its predecessors. In particular, it has a few oft-noted problems:
#[automock]
doesn't work on structs with more than one trait impl, or structs with both a trait impl and inherent methods.#[automock(type Key=u16; type Value=i32;)]
, but this would get cumbersome if Mockall ever supports more types of metaitems.#[concretize]
attribute, but its implementation is hacky.Instead, I propose replacing all of Mockall's syntax with a new Derive macro, which would solve both of these problems. The first, it would solve by generating the code in pieces. The second, it would solve by standard helper attributes. Its syntax would look like this:
With this syntax,
mock!
would only be necesary when mocking structs or traits from external traits. Also, it would be easy to add more helper attributes. For example:#[concretize]
, which already exists#[as]
, for mocking traits with associated constants#[skip]
, as discussed in Support returning of generic parameter usingmock!
macro #536, Skip Generating mock methods #471, and Warning: mocked function never used #410#[default]
to make a method return a default value, so you won't have to dosomething like
.return_const(())
everywhere.#[expect_name(something)]
, which would allow implementing multiple traitswhose methods have the same names, as discussed in How to mock multiple impls of generic struct instances? #271.
I doubt I'll hear any objections to these new features, so what remains is mainly a question of deciding how to name things. That and, of course, finding the time to implement it all ;)
Beta Was this translation helpful? Give feedback.
All reactions