1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! This module holds the Wrapper newtype; used to write
//! instances of typeclasses that we don't define for types we don't
//! own

use frunk::semigroup::*;
use frunk::monoid::*;
use quickcheck::*;

/// The Wrapper NewType. Used for writing implementations of traits
/// that we don't own for type we don't own.
///
/// Avoids the orphan typeclass instances problem in Haskell.
#[derive(Eq, PartialEq, PartialOrd, Debug, Clone, Hash)]
pub struct Wrapper<A>(A);

impl<A: Arbitrary + Ord + Clone> Arbitrary for Wrapper<Max<A>> {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Wrapper(Max(Arbitrary::arbitrary(g)))
    }
}

impl<A: Arbitrary + Ord + Clone> Arbitrary for Wrapper<Min<A>> {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Wrapper(Min(Arbitrary::arbitrary(g)))
    }
}

impl<A: Arbitrary> Arbitrary for Wrapper<All<A>> {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Wrapper(All(Arbitrary::arbitrary(g)))
    }
}

impl<A: Arbitrary> Arbitrary for Wrapper<Any<A>> {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Wrapper(Any(Arbitrary::arbitrary(g)))
    }
}

impl<A: Arbitrary> Arbitrary for Wrapper<Product<A>> {
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Wrapper(Product(Arbitrary::arbitrary(g)))
    }
}

impl<A: Semigroup> Semigroup for Wrapper<A> {
    fn combine(&self, other: &Self) -> Self {
        Wrapper(self.0.combine(&other.0))
    }
}

impl<A: Monoid> Monoid for Wrapper<A> {
    fn empty() -> Self {
        Wrapper(<A as Monoid>::empty())
    }
}