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

Support for trait's associated const #97

Closed
aliXsed opened this issue Jan 21, 2020 · 4 comments · Fixed by #187
Closed

Support for trait's associated const #97

aliXsed opened this issue Jan 21, 2020 · 4 comments · Fixed by #187
Labels
enhancement New feature or request

Comments

@aliXsed
Copy link

aliXsed commented Jan 21, 2020

The following code, when compiled, throws the "missing C in an implementation" error:

use mockall::predicate::*;
use mockall::*;

trait A {
    type T;
    const C: &'static str;

    fn foo(t: Self::T) -> bool;
}

mock! {
    S {}

    trait A {
        type T = isize;
        const C: &'static str = "Mock";

        fn foo(t: isize) -> bool;
    }
}

fn main() {
    println!("Hello, world!");
    let foo_ctx = MockS::foo_context();
    foo_ctx.expect().times(1).return_const(true);
    MockS::foo(0);
    foo_ctx.checkpoint();
}
@aliXsed
Copy link
Author

aliXsed commented Jan 21, 2020

Hi @asomers,

Thank you for this very useful library. I have had the above issue that seems to be caused by the lack of support for trait consts. However, in the case you know a workaround already, I will appreciate it if you can let us know.

Best regards,
Alex

@asomers
Copy link
Owner

asomers commented Jan 21, 2020

I don't think this is something I've tried yet, so it doesn't surprise me that it doesn't work. But I doubt it's difficult to add. What happens if you do this:

mock! {
    S {
        const C: &'static str = "foo";
    }

    trait A {
        type T = isize;
        const C: &'static str = "Mock";

        fn foo(t: isize) -> bool;
    }
}

@asomers asomers added the enhancement New feature or request label Jan 21, 2020
@aliXsed
Copy link
Author

aliXsed commented Jan 21, 2020

I get a different vague build error:

error: Unsupported in this context
--> src/main.rs:16:5
|
16 | trait A {
| ^^^^^

@asomers
Copy link
Owner

asomers commented Jan 21, 2020

Yep, that means I have some work to do. Unfortunately, I can't suggest any workaround. However, you'll get a better error message if you build Mockall with the "nightly" feature.

asomers added a commit that referenced this issue Aug 29, 2020
automock can now mock a trait impl with an associated const.  It can't
mock the trait declaration, because in that case there's no way to set
the value of the associated const on the mock struct.  Of course, if the
trait itself has a default value for the const, then automock will work.
automock can now mock a struct impl with an associated const.

mock! can now mock structs with both inherent associated consts, and
associated consts on their trait impls.

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