Search code examples
rustquickcheck

How do I avoid 'source trait is private' when using subtraits?


I'm trying to use quickcheck in Rust. I want to define my enum as an instance of Arbitrary, so I can use it in tests.

#![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,
        }
    }
}

However, this gives me a compilation error:

src/main.rs:18:17: 18:29 error: source trait is private
src/main.rs:18         let i = g.next_u32();
                               ^~~~~~~~~~~~

What causes this error? I know there's this rust issue but since Gen is imported I would think I could call .next_u32.


Solution

  • It looks like Gen has rand::Rng as a parent trait, the above works if you add extern crate rand after adding rand = "*" to your Cargo.toml.

    [I also had to remove the #[cfg(test)] above the quickcheck import]