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
70
71
72
//! Abstraction over pushable collections
//!
//! [Push] trait allows writing functions which are generic over collection and can only append
//! elements to it, preventing from accident accessing/modifying existing data. Along with [PushExt]
//! trait, that provides extra utilities for `Push`able collections, they are convenient for
//! describing protocol in terms of rounds, where every round may send messages by appending
//! them to sending queue.

/// Collection which can only be appended by 1 element
pub trait Push<T> {
    fn push(&mut self, element: T);
}

impl<T> Push<T> for Vec<T> {
    fn push(&mut self, element: T) {
        Vec::push(self, element)
    }
}

impl<T, P> Push<T> for &mut P
where
    P: Push<T>,
{
    fn push(&mut self, element: T) {
        P::push(self, element)
    }
}

mod private {
    pub trait Sealed<T> {}
    impl<P, T> Sealed<T> for P where P: super::Push<T> {}
}

/// Utilities around [Push]able collections
pub trait PushExt<T>: Push<T> + private::Sealed<T> {
    /// Takes a closure and produces a `Push`able object which applies closure to each element
    fn gmap<B, F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnMut(B) -> T;
}

impl<P, T> PushExt<T> for P
where
    P: Push<T>,
{
    fn gmap<B, F>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnMut(B) -> T,
    {
        Map { pushable: self, f }
    }
}

/// Wraps pushable collection and applies closure `f` to every pushed element
///
/// This wrapper is created by method [map](PushExt::gmap) on [PushExt]
pub struct Map<P, F> {
    pushable: P,
    f: F,
}

impl<A, B, P, F> Push<B> for Map<P, F>
where
    P: Push<A>,
    F: FnMut(B) -> A,
{
    fn push(&mut self, element: B) {
        self.pushable.push((self.f)(element))
    }
}