qrlew/
types.rs

1//! # Type utilities
2//!
3//! A few types and type composers
4//!
5
6/// An abstract product of types
7pub trait And<F> {
8    type Product;
9
10    fn and(self, other: F) -> Self::Product;
11}
12
13pub trait Factor: And<Self, Product = Self> + Sized {
14    fn unit() -> Self;
15
16    fn all<I: IntoIterator<Item = Self>>(iter: I) -> Self {
17        iter.into_iter()
18            .reduce(|p, f| p.and(f))
19            .unwrap_or(Self::unit())
20    }
21}
22
23impl<A: And<A, Product = A> + Default> Factor for A {
24    fn unit() -> Self {
25        Self::default()
26    }
27}
28
29/// An abstract sum of types
30pub trait Or<T> {
31    type Sum;
32
33    fn or(self, other: T) -> Self::Sum;
34}
35
36pub trait Term: Or<Self, Sum = Self> + Sized {
37    fn unit() -> Self;
38
39    fn any<I: IntoIterator<Item = Self>>(iter: I) -> Self {
40        iter.into_iter()
41            .reduce(|p, f| p.or(f))
42            .unwrap_or(Self::unit())
43    }
44}
45
46impl<O: Or<O, Sum = O> + Default> Term for O {
47    fn unit() -> Self {
48        Self::default()
49    }
50}