BurntSushi / quickcheck

Automated property based testing for Rust (with shrinking).
The Unlicense
2.4k stars 149 forks source link

source trait is private (Rng) #82

Closed twittner closed 9 years ago

twittner commented 9 years ago

Please consider this test case:

extern crate quickcheck;

use quickcheck::{Arbitrary, Gen};

#[derive(Clone)]
pub struct Foo(u8);

impl Arbitrary for Foo {
    fn arbitrary<G: Gen>(g: &mut G) -> Foo {
        Foo(g.gen())
    }
}

Compilation with rustc 1.3.0-nightly (0c052199b 2015-07-11) gives:

src/lib.rs:10:13: 10:20 error: source trait is private
src/lib.rs:10         Foo(g.gen())
                          ^~~~~~~

I think it is necessary to export not only Gen but also it's parent trait Rng:

diff --git a/src/lib.rs b/src/lib.rs
index f13fcdc..ab1698b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,6 +10,7 @@
 #[macro_use] extern crate log;
 extern crate rand;

+pub use rand::Rng;
 pub use arbitrary::{
     Arbitrary, Gen, StdGen,
     empty_shrinker, single_shrinker,
apasel422 commented 9 years ago

The other solution is to add this to your crate:

extern crate rand;
use rand::Rng;
BurntSushi commented 9 years ago

What is the right thing to do here? Are there any downsides to re-exporting it? That certainly seems like the more convenient option (and one I hadn't thought of!).

twittner commented 9 years ago

The other solution is to add this to your crate: [...]

Yes. In addition it requires adding rand explicitly as a dependency in your Cargo.toml file.

Wilfred commented 9 years ago

Related: https://github.com/rust-lang/rust/issues/22050

BurntSushi commented 9 years ago

OK, I fixed this by re-exporting the rand::Rng type. I'm not sure if it's the right solution, but it seems better than requiring an explicit dependency on rand just to use methods defined by the Gen trait.