无法为我自己的类型实现任意快速检查 - "source trait is private"
Cannot implement quickchecks Arbitrary for my own type - "source trait is private"
我正在使用 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
这样的:
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() {}
失败并出现错误:
src/lib.rs:12:21: 12:40 error: source trait is private
src/lib.rs:12 let a: u8 = g.gen_range(0, 128);
^~~~~~~~~~~~~~~~~~~
一些搜索让我得到了各种错误报告(1, 2, 3, 4) that all seem to suggest I need to use
the supertrait of Gen
, which is rand::Rng
。我更新了我的 crates 和 use 语句
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)
rustc 1.1.0-dev (3ca008dcf 2015-05-12) (built 2015-05-12)
我也在用quickcheck v0.2.18
这很奇怪。我可以重现您最初的错误,但您建议的修复对我有用:
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() {}
运行 cargo test
beta5 和 2015-05-12 每晚作品。
啊,这是一个棘手的问题。为了在我的测试中取得进展,我添加了这个技巧:
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
。我在想我会通过快速检查神奇地获得相同的版本。
最终的问题是我 没有 实际上 use
d Rng
的正确版本, Gen
是的一个子特征。非常混乱!
将 rand
添加到我的 Cargo.toml
后,我又回来了。
我正在使用 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
这样的:
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() {}
失败并出现错误:
src/lib.rs:12:21: 12:40 error: source trait is private
src/lib.rs:12 let a: u8 = g.gen_range(0, 128);
^~~~~~~~~~~~~~~~~~~
一些搜索让我得到了各种错误报告(1, 2, 3, 4) that all seem to suggest I need to use
the supertrait of Gen
, which is rand::Rng
。我更新了我的 crates 和 use 语句
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)
rustc 1.1.0-dev (3ca008dcf 2015-05-12) (built 2015-05-12)
我也在用quickcheck v0.2.18
这很奇怪。我可以重现您最初的错误,但您建议的修复对我有用:
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() {}
运行 cargo test
beta5 和 2015-05-12 每晚作品。
啊,这是一个棘手的问题。为了在我的测试中取得进展,我添加了这个技巧:
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
。我在想我会通过快速检查神奇地获得相同的版本。
最终的问题是我 没有 实际上 use
d Rng
的正确版本, Gen
是的一个子特征。非常混乱!
将 rand
添加到我的 Cargo.toml
后,我又回来了。