higher_cat/
pure.rs

1use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
2use std::hash::{BuildHasher, Hash};
3use std::iter;
4
5/// `Pure` lets you construct a value of type `F<A>` using a single value of
6/// `A`.
7pub trait Pure<A> {
8    fn pure(value: A) -> Self;
9}
10
11impl<A> Pure<A> for Option<A> {
12    fn pure(value: A) -> Self {
13        Some(value)
14    }
15}
16
17impl<A, E> Pure<A> for Result<A, E> {
18    fn pure(value: A) -> Self {
19        Ok(value)
20    }
21}
22
23impl<A> Pure<A> for Vec<A> {
24    fn pure(value: A) -> Self {
25        vec![value]
26    }
27}
28
29impl<A> Pure<A> for VecDeque<A> {
30    fn pure(value: A) -> Self {
31        iter::once(value).collect()
32    }
33}
34
35impl<A> Pure<A> for LinkedList<A> {
36    fn pure(value: A) -> Self {
37        iter::once(value).collect()
38    }
39}
40
41impl<A> Pure<A> for BinaryHeap<A>
42where
43    A: Ord,
44{
45    fn pure(value: A) -> Self {
46        iter::once(value).collect()
47    }
48}
49
50impl<A> Pure<A> for BTreeSet<A>
51where
52    A: Ord,
53{
54    fn pure(value: A) -> Self {
55        iter::once(value).collect()
56    }
57}
58
59impl<A, S> Pure<A> for HashSet<A, S>
60where
61    A: Hash + Eq,
62    S: BuildHasher + Default,
63{
64    fn pure(value: A) -> Self {
65        iter::once(value).collect()
66    }
67}
68
69impl<A, B> Pure<(A, B)> for BTreeMap<A, B>
70where
71    A: Ord,
72    B: Ord,
73{
74    fn pure(value: (A, B)) -> Self {
75        iter::once(value).collect()
76    }
77}
78
79impl<A, B, S> Pure<(A, B)> for HashMap<A, B, S>
80where
81    A: Hash + Eq,
82    B: Hash + Eq,
83    S: BuildHasher + Default,
84{
85    fn pure(value: (A, B)) -> Self {
86        iter::once(value).collect()
87    }
88}