使用子特征时如何避免 'source trait is private' ?
How do I avoid 'source trait is private' when using subtraits?
我正在尝试在 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
.
看起来 Gen
具有 rand::Rng
作为父特征,如果您在将 rand = "*"
添加到 Cargo.toml 之后添加 extern crate rand
,则上述方法有效。
[我还必须删除 quickcheck
导入上方的 #[cfg(test)]
]
我正在尝试在 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
.
看起来 Gen
具有 rand::Rng
作为父特征,如果您在将 rand = "*"
添加到 Cargo.toml 之后添加 extern crate rand
,则上述方法有效。
[我还必须删除 quickcheck
导入上方的 #[cfg(test)]
]