使用子商品时如何避免“源特征是私密的”?

时间:2015-07-17 22:41:45

标签: rust quickcheck

我正在尝试在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

1 个答案:

答案 0 :(得分:3)

看起来Genrand::Rng作为父级特征,如果您在将extern crate rand添加到Cargo.toml之后添加rand = "*",则上述情况有效。

[我还必须删除#[cfg(test)]导入]

上方的quickcheck