Skip to content

Commit

Permalink
api: remove Send bound from Arbitrary/Testable traits
Browse files Browse the repository at this point in the history
The Send bound is a relic from the past. Indeed, the docs for the
Arbitrary trait have been outdated for quite some time. quickcheck
stopped running each test in a separate thread once
`std::panic::catch_unwind` was stabilized many moons ago. With
`catch_unwind`, the `Send` bound is no longer necessary.

We do need to retain the `'static` bound though. Without that,
implementing shrink seems implausible.

Fixes #262, Closes #263
  • Loading branch information
BurntSushi committed Dec 27, 2020
1 parent 4a300c5 commit c309676
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
14 changes: 4 additions & 10 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ pub fn single_shrinker<A: 'static>(value: A) -> Box<dyn Iterator<Item = A>> {
///
/// As of now, all types that implement `Arbitrary` must also implement
/// `Clone`. (I'm not sure if this is a permanent restriction.)
///
/// They must also be sendable and static since every test is run in its own
/// thread using `thread::Builder::spawn`, which requires the `Send + 'static`
/// bounds.
pub trait Arbitrary: Clone + Send + 'static {
pub trait Arbitrary: Clone + 'static {
fn arbitrary<G: Gen>(g: &mut G) -> Self;

fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Expand Down Expand Up @@ -401,7 +397,7 @@ impl<K: Arbitrary + Ord, V: Arbitrary> Arbitrary for BTreeMap<K, V> {
impl<
K: Arbitrary + Eq + Hash,
V: Arbitrary,
S: BuildHasher + Default + Clone + Send + 'static,
S: BuildHasher + Default + Clone + 'static,
> Arbitrary for HashMap<K, V, S>
{
fn arbitrary<G: Gen>(g: &mut G) -> Self {
Expand Down Expand Up @@ -441,10 +437,8 @@ impl<T: Arbitrary + Ord> Arbitrary for BinaryHeap<T> {
}
}

impl<
T: Arbitrary + Eq + Hash,
S: BuildHasher + Default + Clone + Send + 'static,
> Arbitrary for HashSet<T, S>
impl<T: Arbitrary + Eq + Hash, S: BuildHasher + Default + Clone + 'static>
Arbitrary for HashSet<T, S>
{
fn arbitrary<G: Gen>(g: &mut G) -> Self {
let vec: Vec<T> = Arbitrary::arbitrary(g);
Expand Down
12 changes: 6 additions & 6 deletions src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ impl TestResult {
pub fn must_fail<T, F>(f: F) -> TestResult
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
F: 'static,
T: 'static,
{
let f = panic::AssertUnwindSafe(f);
TestResult::from_bool(panic::catch_unwind(f).is_err())
Expand Down Expand Up @@ -305,7 +305,7 @@ impl TestResult {
/// and potentially shrink those arguments if they produce a failure.
///
/// It's unlikely that you'll have to implement this trait yourself.
pub trait Testable: Send + 'static {
pub trait Testable: 'static {
fn result<G: Gen>(&self, _: &mut G) -> TestResult;
}

Expand All @@ -330,7 +330,7 @@ impl Testable for TestResult {
impl<A, E> Testable for Result<A, E>
where
A: Testable,
E: Debug + Send + 'static,
E: Debug + 'static,
{
fn result<G: Gen>(&self, g: &mut G) -> TestResult {
match *self {
Expand Down Expand Up @@ -409,8 +409,8 @@ testable_fn!(A, B, C, D, E, F, G, H);
fn safe<T, F>(fun: F) -> Result<T, String>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
F: 'static,
T: 'static,
{
panic::catch_unwind(panic::AssertUnwindSafe(fun)).map_err(|any_err| {
// Extract common types of panic payload:
Expand Down

0 comments on commit c309676

Please sign in to comment.