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

Initial support for trait-variant #640

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lasantosr
Copy link

This PR aims to fix #639

Feel free to update the readme, as English is not my primary language

@lasantosr lasantosr requested a review from asomers as a code owner February 11, 2025 19:02
@lasantosr
Copy link
Author

@asomers I think the nightly is failing because some sort of new lints unrelated to this PR, would you like me to fix those also here?

@asomers
Copy link
Owner

asomers commented Feb 23, 2025

@asomers I think the nightly is failing because some sort of new lints unrelated to this PR, would you like me to fix those also here?

It's already fixed on master. You just need to rebase.

@lasantosr lasantosr force-pushed the trait-variant-support branch from 32a14f9 to 4e1e0e7 Compare February 23, 2025 15:18
@lasantosr
Copy link
Author

Great! It looks like it's ready now

Copy link
Owner

@asomers asomers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests could really benefit from some kind of assertion to check that they actually implement the additional trait. That might be hard to do with Send, but what about something like this?

trait Foo {}
#[automock]
#[trait_variant::make(Foo)]
pub trait Bar {
    ...
}
#[test]
fn some_test_method_name() {
    let mock = MockBar::new();
    let p: Box<dyn Foo> = Box::new(mock);   // Ensures that MockBar implements Foo
    ...
}

Also, with this change, does the #[trait_variant::make(OtherTraitName: Foo)] syntax work?

@lasantosr
Copy link
Author

lasantosr commented Feb 23, 2025

I think the tests could really benefit from some kind of assertion to check that they actually implement the additional trait. That might be hard to do with Send, but what about something like this?

trait Foo {}
#[automock]
#[trait_variant::make(Foo)]
pub trait Bar {
    ...
}
#[test]
fn some_test_method_name() {
    let mock = MockBar::new();
    let p: Box<dyn Foo> = Box::new(mock);   // Ensures that MockBar implements Foo
    ...
}

Also, with this change, does the #[trait_variant::make(OtherTraitName: Foo)] syntax work?

I'm not that familiar with the codebase to implement all of those changes.

On your first example, automock would not implement Foo trait for MockBar, because it might have any arbitrary number of unrelated methods, but it could be manually implemented:

trait Foo {}
#[automock]
#[trait_variant::make(Foo)]
pub trait Bar {
    ...
}
impl Foo for MockBar {}

The most common use case is to mark your trait (and async method futures) Send, and mocks are already Send so that's covered.

As for the second example, the syntax already works, but we would be mocking the initial trait only (Bar), not the other variant (OtherTraitName). With that syntax you're actually defining two different traits.

@asomers The main goal of this PR is to have feature parity with async_trait, other improvements could be added later.

As mentioend on #639 the async_trait annotation can be located before or after the automock annotation, and that means you need to return the actual pinned future or just the future's output on the mocks expectations.

With this PR we can do the same for trait_variant, so that the migration is painless.

@lasantosr lasantosr changed the title Add support for trait-variant Initial support for trait-variant Feb 23, 2025
@lasantosr lasantosr requested a review from asomers February 24, 2025 07:05
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 this pull request may close these issues.

Compatibility with trait-variant for automock
2 participants