hetseq/
queue.rs

1use {IntoList, List};
2
3/// Heterogenous queue
4/// Supports pushing, splitting to init and last
5/// Mapping and folding
6#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
7pub struct Queue<Q>(pub Q);
8
9impl Queue<()> {
10    pub fn new() -> Queue<()> {
11        Queue(())
12    }
13}
14
15impl<Q> Queue<Q> {
16    pub fn push<V>(self, value: V) -> Queue<(Queue<Q>, V)> {
17        Queue((self, value))
18    }
19}
20
21impl<H, T> Queue<(Queue<H>, T)> {
22    pub fn init(&self) -> &Queue<H> {
23        &(self.0).0
24    }
25    pub fn last(&self) -> &T {
26        &(self.0).1
27    }
28}
29
30pub trait IntoQueue {
31    ///
32    type Queue;
33    ///
34    fn into_queue(self) -> Self::Queue;
35}
36
37pub trait IntoListImpl<L> {
38    type List;
39    fn into_list_impl(self, L) -> Self::List;
40}
41
42impl<L> IntoListImpl<L> for Queue<()> {
43    type List = L;
44
45    fn into_list_impl(self, list: L) -> L {
46        list
47    }
48}
49
50impl<H, T, L> IntoListImpl<L> for Queue<(H, T)>
51    where H: IntoListImpl<List<(T, L)>>
52{
53    type List = H::List;
54
55    fn into_list_impl(self, list: L) -> Self::List {
56        let Queue((head, tail)) = self;
57        let list = List((tail, list));
58        head.into_list_impl(list)
59    }
60}
61
62impl<Q> IntoList for Q
63    where Q: IntoListImpl<List<()>>
64{
65    type List = <Q as IntoListImpl<List<()>>>::List;
66    fn into_list(self) -> Self::List {
67        self.into_list_impl(List::new())
68    }
69}