frunk_laws/
wrapper.rs

1//! This module holds the Wrapper newtype; used to write
2//! instances of typeclasses that we don't define for types we don't
3//! own
4
5use frunk::monoid::*;
6use frunk::semigroup::*;
7use quickcheck::*;
8
9/// The Wrapper NewType. Used for writing implementations of traits
10/// that we don't own for type we don't own.
11///
12/// Avoids the orphan typeclass instances problem in Haskell.
13#[derive(Eq, PartialEq, PartialOrd, Debug, Clone, Hash)]
14pub struct Wrapper<A>(A);
15
16impl<A: Arbitrary + Ord + Clone> Arbitrary for Wrapper<Max<A>> {
17    fn arbitrary(g: &mut Gen) -> Self {
18        Wrapper(Max(Arbitrary::arbitrary(g)))
19    }
20}
21
22impl<A: Arbitrary + Ord + Clone> Arbitrary for Wrapper<Min<A>> {
23    fn arbitrary(g: &mut Gen) -> Self {
24        Wrapper(Min(Arbitrary::arbitrary(g)))
25    }
26}
27
28impl<A: Arbitrary> Arbitrary for Wrapper<All<A>> {
29    fn arbitrary(g: &mut Gen) -> Self {
30        Wrapper(All(Arbitrary::arbitrary(g)))
31    }
32}
33
34impl<A: Arbitrary> Arbitrary for Wrapper<Any<A>> {
35    fn arbitrary(g: &mut Gen) -> Self {
36        Wrapper(Any(Arbitrary::arbitrary(g)))
37    }
38}
39
40impl<A: Arbitrary> Arbitrary for Wrapper<Product<A>> {
41    fn arbitrary(g: &mut Gen) -> Self {
42        Wrapper(Product(Arbitrary::arbitrary(g)))
43    }
44}
45
46impl<A: Semigroup> Semigroup for Wrapper<A> {
47    fn combine(&self, other: &Self) -> Self {
48        Wrapper(self.0.combine(&other.0))
49    }
50}
51
52impl<A: Monoid> Monoid for Wrapper<A> {
53    fn empty() -> Self {
54        Wrapper(<A as Monoid>::empty())
55    }
56}