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

Can't mock trait containing function that takes an argument of type Option<&T> #61

Closed
thomaswhiteway opened this issue Oct 31, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@thomaswhiteway
Copy link

Seen using mockall 0.5.1 on both stable and nightly.

For example, trying to compile the following fails.

use mockall::automock;

#[automock]
trait Test<T> {
    fn test(value: Option<&T>);
}
error[E0637]: `&` without an explicit lifetime name cannot be used here
 --> src/lib.rs:5:27
  |
5 |     fn test(value: Option<&str>);
  |                           ^ explicit lifetime name needed here

error[E0106]: missing lifetime specifier
 --> src/lib.rs:5:27
  |
5 |     fn test(value: Option<&str>);
  |                           ^ expected lifetime parameter

Workaround

If T is known it's possible to work around this by specifying a lifetime parameter in the method definition, e.g. the following works

#[automock]
trait Test {
    fn test<'a>(value: Option<&'a str>);
}

If the trait is generic this fails with a different error.

#[automock]
trait Test<T> {
    fn test(value: Option<&T>);
}
error[E0261]: use of undeclared lifetime name `'a`
 --> src/lib.rs:5:32
  |
5 |     fn test<'a>(value: Option<&'a T>);
  |                                ^^ undeclared lifetime

error: aborting due to previous error
@asomers
Copy link
Owner

asomers commented Oct 31, 2019

Yes, this is a problem. More generally, the only arguments that Mockall really supports are 'static arguments and references to 'static data.

@robin-kunzler
Copy link

Hi, is there a workaround for this issue for generic traits? If not, how could this be fixed in mockall?

@asomers
Copy link
Owner

asomers commented Mar 3, 2020

The only workaround I know of is to change the trait's definition in test mode to take a Option<&'static T>. Enhancing Mockall to fully support this feature is non-trivial.

@robin-kunzler
Copy link

I see, thanks!

@asomers
Copy link
Owner

asomers commented Aug 23, 2020

I'm a fool. I didn't realize that the workaround is trivial: declare an explicit generic lifetime. That's actually worked ever since 0.5.0 (PR #48). Do it like this:

#[automock]
trait Test<T> {
    fn test<'a>(value: Option<&'a T>);
}

@asomers
Copy link
Owner

asomers commented Aug 23, 2020

I think I'm going to close this issue rather than create an enhancement for it. The cost-benefit ratio doesn't seem very good, given how easy the workaround is.

@asomers asomers closed this as completed Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants