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

trait support? #54

Open
xiaoquanju opened this issue Apr 24, 2023 · 3 comments
Open

trait support? #54

xiaoquanju opened this issue Apr 24, 2023 · 3 comments

Comments

@xiaoquanju
Copy link

good library.!!!!!!!!!

is there plan to support trait ?

@xiaoquanju
Copy link
Author

pub trait A{
fn resize(&self, cols: u16, rows: u16) -> Result<(), String>;
}

struct MockA{} //i will define a struct

#[cfg_attr(test, faux::create)] /
impl A for MockA{
// i dont want to write there dummy code , and need faux auto generate
fn resize(&self, cols: u16, rows: u16) -> Result<(), String>{
!todo()
};
}

let a = MockA::faux()
faux::when!(a.resize)
.then_return(Ok(()));

@nrxus
Copy link
Owner

nrxus commented May 5, 2023

This is definitely something I want to do, but I haven't prioritized it yet. The hardest part is coming up with something ergonomic that works for all traits. My ideal solution would be something like:

#[cfg_attr(test, faux::create)] 
pub trait MyTrait {
  /* declare methods */
}

#[test]
fn my_test() {
    let mock = MyTrait::faux();
}

But rust only allows calling instance methods directly on traits for object safe traits which means it is not a global solution. Maybe I'll just have to say that it's good enough and provide a fallback for non-object safe traits? I am not sure... these design decisions slow me down haha

@nrxus
Copy link
Owner

nrxus commented May 5, 2023

It looks like there's been code to support what I want in nightly for over 3 years but it has yet to be stabilized: rust-lang/rust#43561.

There hasn't been any discussion on the tracking issue in a while though so it's hard to know if it's going to be merged soon or if it got lost in the shuffle.

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

No branches or pull requests

2 participants