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
57
58
59
60
61
62
63
64
65
66
67
68
69
use {IntoList, List};
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct Queue<Q>(pub Q);
impl Queue<()> {
pub fn new() -> Queue<()> {
Queue(())
}
}
impl<Q> Queue<Q> {
pub fn push<V>(self, value: V) -> Queue<(Queue<Q>, V)> {
Queue((self, value))
}
}
impl<H, T> Queue<(Queue<H>, T)> {
pub fn init(&self) -> &Queue<H> {
&(self.0).0
}
pub fn last(&self) -> &T {
&(self.0).1
}
}
pub trait IntoQueue {
type Queue;
fn into_queue(self) -> Self::Queue;
}
pub trait IntoListImpl<L> {
type List;
fn into_list_impl(self, L) -> Self::List;
}
impl<L> IntoListImpl<L> for Queue<()> {
type List = L;
fn into_list_impl(self, list: L) -> L {
list
}
}
impl<H, T, L> IntoListImpl<L> for Queue<(H, T)>
where H: IntoListImpl<List<(T, L)>>
{
type List = H::List;
fn into_list_impl(self, list: L) -> Self::List {
let Queue((head, tail)) = self;
let list = List((tail, list));
head.into_list_impl(list)
}
}
impl<Q> IntoList for Q
where Q: IntoListImpl<List<()>>
{
type List = <Q as IntoListImpl<List<()>>>::List;
fn into_list(self) -> Self::List {
self.into_list_impl(List::new())
}
}