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

An async trait mock won't compile in 0.10.1 but did in 0.9 #306

Closed
Sushisource opened this issue Jul 6, 2021 · 6 comments · Fixed by #312
Closed

An async trait mock won't compile in 0.10.1 but did in 0.9 #306

Sushisource opened this issue Jul 6, 2021 · 6 comments · Fixed by #312

Comments

@Sushisource
Copy link

This looks nearly identical to #298 but I'm using 0.10.1 and still getting the error. My trait and the manual mock:

pub type Result<T, E = tonic::Status> = std::result::Result<T, E>;

#[cfg_attr(test, mockall::automock)]
#[async_trait::async_trait]
pub trait Poller<PollResult>
where
    PollResult: Send + Sync + 'static,
{
    async fn poll(&self) -> Option<Result<PollResult>>;
    fn notify_shutdown(&self);
    async fn shutdown(self);
    async fn shutdown_box(self: Box<Self>);
}

#[cfg(test)]
mockall::mock! {
    pub ManualPoller<T: Send + Sync + 'static> {}
    impl<T: Send + Sync + 'static> Poller<T> for ManualPoller<T> {
        fn poll<'a, 'b>(&'a self)
          -> impl Future<Output = Option<Result<T>>> + Send + 'b
            where 'a: 'b, Self: 'b;
        fn notify_shutdown(&self);
        fn shutdown<'a>(self)
          -> impl Future<Output = ()> + Send + 'a
            where Self: 'a;
        fn shutdown_box<'a>(self: Box<Self>)
          -> impl Future<Output = ()> + Send + 'a
            where Self: 'a;
    }
}
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
  --> src/pollers/mod.rs:43:1
   |
43 | / mockall::mock! {
44 | |     pub ManualPoller<T: Send + Sync + 'static> {}
45 | |     impl<T: Send + Sync + 'static> Poller<T> for ManualPoller<T> {
46 | |         fn poll<'a, 'b>(&'a self)
...  |
56 | |     }
57 | | }
   | | ^
   | | |
   | |_this data with an anonymous lifetime `'_`...
   |   ...is captured and required to live as long as `'static` here
@asomers
Copy link
Owner

asomers commented Jul 7, 2021

Did this work on 0.9.1 ?

@Sushisource
Copy link
Author

@asomers Indeed it did

@asomers
Copy link
Owner

asomers commented Jul 10, 2021

The problem is caused by mocking a trait method that returns a non-static structure on a generic struct. That conflicts with the specific impl code from PR #274 . You wouldn't have a problem if:

  • poll were a struct method instead of a trait method,
  • poll's return value were always `'static, or
  • ManualPoller weren't generic.

This is fixable, but it might take some time.

@asomers
Copy link
Owner

asomers commented Jul 10, 2021

@Sushisource could you please test the branch in the linked PR?

@Sushisource
Copy link
Author

@asomers Yep, that's worked for me. Thanks!

@asomers
Copy link
Owner

asomers commented Jul 12, 2021

Just released 0.10.2 . Thanks for the bug report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants