I am using quickcheck to validate some properties of my code. At one point, I need an ASCII byte, so I tried to write an implementation of Arbitrary
like this:
cat test.sh | awk '{print $1}'
This fails with the error:
extern crate quickcheck;
use quickcheck::{quickcheck,Arbitrary,Gen};
#[derive(Debug,Copy,Clone)]
struct AsciiChar(u8);
impl Arbitrary for AsciiChar {
fn arbitrary<G>(g: &mut G) -> AsciiChar
where G: Gen
{
let a: u8 = g.gen_range(0, 128);
AsciiChar(a)
}
}
#[test]
fn it_works() {}
Some searching led me to various bug reports (1, 2, 3, 4) that all seem to suggest I need to src/lib.rs:12:21: 12:40 error: source trait is private
src/lib.rs:12 let a: u8 = g.gen_range(0, 128);
^~~~~~~~~~~~~~~~~~~
the supertrait of Gen
, which is rand::Rng
. I updated my crates and use statements to
use
But I continue to get the same error.
I've tried with
extern crate quickcheck;
extern crate rand;
use rand::Rng;
use quickcheck::{quickcheck,Arbitrary,Gen};
rustc version 1.1.0-dev (b402c43f0 2015-05-07) (built 2015-05-07)
I'm also using rustc 1.1.0-dev (3ca008dcf 2015-05-12) (built 2015-05-12)
答案 0 :(得分:3)
impl Arbitrary for AsciiChar {
fn arbitrary<G>(g: &mut G) -> AsciiChar
where G: Gen
{
let a: u8 = Arbitrary::arbitrary(g);
AsciiChar(a % 128)
}
}
编译之后,我收到了这个错误:
src/lib.rs:419:5: 419:23 error: use of unstable library feature 'rand': use `rand` from crates.io
src/lib.rs:419 extern crate rand;
^~~~~~~~~~~~~~~~~~
src/lib.rs:419:5: 419:23 help: add #![feature(rand)] to the crate attributes to enable
我的问题是我忘了将rand
添加到Cargo.toml
。我想我会疯狂地通过quickcheck获得同样的版本。
最终的问题是,我没有实际上use
正确的Rng
版本,Gen
是其中的一个子版本。非常混乱!
将rand
添加到Cargo.toml
后,我又回来了。
答案 1 :(得分:1)
这很奇怪。我可以重现您的初始错误,但您建议的修复程序适用于我:
extern crate quickcheck;
extern crate rand;
use quickcheck::{Arbitrary,Gen};
use rand::Rng;
#[derive(Debug,Copy,Clone)]
struct AsciiChar(u8);
impl Arbitrary for AsciiChar {
fn arbitrary<G>(g: &mut G) -> AsciiChar
where G: Gen
{
let a: u8 = g.gen_range(0, 128);
AsciiChar(a)
}
}
#[test]
fn it_works() {}
在beta5和2015-05-12夜间作品上运行cargo test
。