Hi,
In my (really simplified) project I have two crates: "my-stdlib" and "my-crate". The "my-stdlib" crate should contain only code which doesn't depend on anything outside Rust's stdlib (when doing release build). It contains things like type definitions, helper functions, etc.
I would like to implement quickcheck::Arbitrary for these types in "my-stdlib" so that these implementations are only available when someone somewhere runs "cargo test". This brings quickcheck dependency to "my-stdlib", but I has hoping it's only dev-dependency.
The actual quickcheck properties are then defined in place of actual use, for example in this case it's "my-crate".
This is what I have in my-stdlib/src/lib.rs
:
#[derive(Clone, Debug, PartialEq)]
pub enum MyEnum {
One,
Two,
Three,
}
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
impl quickcheck::Arbitrary for MyEnum {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
let vals = &[
MyEnum::One,
MyEnum::Two,
MyEnum::Three,
];
g.choose(vals).expect("choose value").clone()
}
}
And in my-crate/src/lib.rs
is the code I try to test with quickcheck:
extern crate my_stdlib;
use my_stdlib::MyEnum;
pub fn from_enum(val: &MyEnum) -> u8 {
match *val {
MyEnum::One => 1,
MyEnum::Two => 2,
MyEnum::Three => 3,
}
}
pub fn to_enum(val: u8) -> MyEnum {
match val {
1 => MyEnum::One,
2 => MyEnum::Two,
3 => MyEnum::Three,
_ => panic!("Invalid value"),
}
}
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
#[cfg(test)]
mod tests {
use my_stdlib::MyEnum;
use super::*;
quickcheck! {
fn prop_myenum(xs: MyEnum) -> bool {
xs == to_enum(from_enum(&xs))
}
}
}
The error I get is:
error[E0277]: the trait bound `my_stdlib::MyEnum: quickcheck::Arbitrary` is not satisfied
--> my-crate/src/lib.rs:32:5
|
32 | / quickcheck! {
33 | | fn prop_myenum(xs: MyEnum) -> bool {
34 | | xs == to_enum(from_enum(&xs))
35 | | }
36 | | }
| |_____^ the trait `quickcheck::Arbitrary` is not implemented for `my_stdlib::MyEnum`
|
= note: required because of the requirements on the impl of `quickcheck::Testable` for `fn(my_stdlib::MyEnum) -> bool`
= note: required by `quickcheck::quickcheck`
= note: this error originates in a macro outside of the current crate
Is there some kind of way to achieve this?