我正在尝试在Rust中使用quickcheck。我想将我的枚举定义为Arbitrary
的实例,因此我可以在测试中使用它。
#![feature(plugin)]
#![plugin(quickcheck_macros)]
#[cfg(test)]
extern crate quickcheck;
use quickcheck::{Arbitrary,Gen};
#[derive(Clone)]
enum Animal {
Cat,
Dog,
Mouse
}
impl Arbitrary for Animal {
fn arbitrary<G: Gen>(g: &mut G) -> Animal {
let i = g.next_u32();
match i % 3 {
0 => Animal::Cat,
1 => Animal::Dog,
2 => Animal::Mouse,
}
}
}
然而,这给了我一个编译错误:
src/main.rs:18:17: 18:29 error: source trait is private
src/main.rs:18 let i = g.next_u32();
^~~~~~~~~~~~
导致此错误的原因是什么?我知道有this rust issue但是自Gen
导入后我会认为我可以致电.next_u32
。
答案 0 :(得分:3)
看起来Gen
有rand::Rng
作为父级特征,如果您在将extern crate rand
添加到Cargo.toml之后添加rand = "*"
,则上述情况有效。
[我还必须删除#[cfg(test)]
导入]
quickcheck