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 generic methods with non-'static return values #76

Closed
asomers opened this issue Nov 16, 2019 · 3 comments · Fixed by #86
Closed

Allow mocking generic methods with non-'static return values #76

asomers opened this issue Nov 16, 2019 · 3 comments · Fixed by #86
Assignees
Labels
enhancement New feature or request

Comments

@asomers
Copy link
Owner

asomers commented Nov 16, 2019

PR #48 added the ability to mock generic methods whose arguments had lifetime parameters. But it didn't handle the case of return values with lifetime parameters. So the following still doesn't work:

struct X<'a>(&'a u32);

mock!{
    Foo {
        fn foo<'a>() -> X<'a>;
    }
}
@asomers asomers added the enhancement New feature or request label Nov 16, 2019
@DGolubets
Copy link

Hi @asomers ! Have you started working on this feature? If not, I may look into it.

I need it to mock methods returning BoxFuture with lifetime. I didn't dig deep so far, sill looking at your code and trying stuff. But my last experiment replacing lifetimes in return types with static seems to work.

Do you already have ideas on what is the right approach handling this case?

@asomers asomers self-assigned this Nov 23, 2019
@asomers
Copy link
Owner Author

asomers commented Nov 23, 2019

Yeah, I'm already working on it. I've got it about 50% working. Could you share the signature of one of the methods you're trying to mock? I might stick it in the test case.

@DGolubets
Copy link

DGolubets commented Nov 23, 2019

Great!
I have a simple method like this:


#[cfg_attr(test, automock)]
pub trait Scraper {
    fn scrape(&self) -> BoxFuture<'_, Result<Vec<ScrapeResult>, Error>>;
}

asomers added a commit that referenced this issue Nov 30, 2019
It's now possible to mock generic methods whose return values' lifetimes are
chosen by the caller.  This is especially useful for methods that return
std::future::Future.

There are presently two restrictions:
1) static methods and bare functions of this type are not supported
2) The expectations' return values must be 'static.  That can often be
   worked-around via an unsafe lifetime transmute.

Hopefully both of those restrictions will be lifted in time.

Fixes #76
asomers added a commit that referenced this issue Nov 30, 2019
It's now possible to mock generic methods whose return values' lifetimes are
chosen by the caller.  This is especially useful for methods that return
std::future::Future.

There are presently two restrictions:
1) static methods and bare functions of this type are not supported
2) The expectations' return values must be 'static.  That can often be
   worked-around via an unsafe lifetime transmute.

Hopefully both of those restrictions will be lifted in time.

Fixes #76
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

Successfully merging a pull request may close this issue.

2 participants