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 has supported generic methods since 0.1.0. But it's always required that all generic parameters are 'static. That's a serious limitation (for the curious, it comes from the 'static bound on std::any::Any). But now it's lifted! Partially.
The new #[concretize] attribute, introduced by PR #408 , allows for mocking some generic methods whose generic parameters are non-'static. It works by turning the generic function argument into a trait object before matching it to expectations. As such, there are still some limitations. For one thing, it obviously only works with generic types that can be turned into trait objects. For another, matchers can't use .with, they can only use .withf. But most limiting of all, it only works with a fairly narrow set of types build off of the generic parameters. For example, fn foo<T>(t: &[T]) will work, but fn foo<T>(t: Vec<T>) will not.
So my question is, despite the limitations is this new feature sufficiently useful? And of course, does it have any bugs?
One alternative syntax that would be somewhat more flexible would be to allow the user to specify how to turn an argument into a trait object, like this:
#[concretize]fnfoo<T>(x:i32,#[concretize_with(Arc::new(&*t as &dyn T))]t:Arc<T>,z:bool) -> bool{
...}
but that's really ugly, and I haven't tried implementing it yet.
If you use Mockall with generic methods, I'd be interested to hear your experience with this new feature before I release the next version.
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 has supported generic methods since 0.1.0. But it's always required that all generic parameters are
'static
. That's a serious limitation (for the curious, it comes from the'static
bound on std::any::Any). But now it's lifted! Partially.The new
#[concretize]
attribute, introduced by PR #408 , allows for mocking some generic methods whose generic parameters are non-'static
. It works by turning the generic function argument into a trait object before matching it to expectations. As such, there are still some limitations. For one thing, it obviously only works with generic types that can be turned into trait objects. For another, matchers can't use.with
, they can only use.withf
. But most limiting of all, it only works with a fairly narrow set of types build off of the generic parameters. For example,fn foo<T>(t: &[T])
will work, butfn foo<T>(t: Vec<T>)
will not.So my question is, despite the limitations is this new feature sufficiently useful? And of course, does it have any bugs?
One alternative syntax that would be somewhat more flexible would be to allow the user to specify how to turn an argument into a trait object, like this:
but that's really ugly, and I haven't tried implementing it yet.
If you use Mockall with generic methods, I'd be interested to hear your experience with this new feature before I release the next version.
Beta Was this translation helpful? Give feedback.
All reactions