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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::marker::PhantomData;


pub trait Lense {
    type Object;
    type Value;

    fn get(&self, obj: &Self::Object) -> Self::Value;
    fn set(&self, obj: &Self::Object, val: &Self::Value) -> Self::Object;
}

pub struct L<O, V, G, S> {
    g: G,
    s: S,
    o: PhantomData<O>,
    v: PhantomData<V>,
}

impl<O, V, G, S> L<O, V, G, S> {
    pub fn new(x: G, y: S) -> L<O, V, G, S> {
        L {
            g: x,
            s: y,
            o: PhantomData,
            v: PhantomData,
        }
    }
}
pub fn lense<O, V, G, S>(x: G, y: S) -> L<O, V, G, S> {
    L::new(x, y)
}


impl<O, V, G, S> Lense for L<O, V, G, S>
where
    G: Fn(&O) -> V,
    S: Fn(&O, &V) -> O,
{
    type Object = O;
    type Value = V;

    fn get(&self, obj: &(Self::Object)) -> Self::Value {
        (self.g)(obj)
    }
    fn set(&self, obj: &Self::Object, val: &Self::Value) -> Self::Object {
        (self.s)(obj, val)
    }
}

pub struct Compose<'composite, OUTER, INNER, VALUE, L1, L2>
where
    L1: 'composite + Lense<Object = OUTER, Value = INNER>,
    L2: 'composite + Lense<Object = INNER, Value = VALUE>,
{
    outer: &'composite L1,
    inner: &'composite L2,
}

impl<'composite, OUTER, INNER, VALUE, L1, L2> Lense
    for Compose<'composite, OUTER, INNER, VALUE, L1, L2>
where
    L1: 'composite + Lense<Object = OUTER, Value = INNER>,
    L2: 'composite + Lense<Object = INNER, Value = VALUE>,
{
    type Object = OUTER;
    type Value = VALUE;

    fn get(&self, obj: &Self::Object) -> Self::Value {
        self.inner.get(&(self.outer.get(obj)))
    }
    fn set(&self, obj: &Self::Object, val: &Self::Value) -> Self::Object {
        self.outer.set(
            obj,
            &(self.inner.set(&(self.outer.get(obj)), val)),
        )
    }
}

pub fn compose<'composite, OUTER, INNER, VALUE, L1, L2>(
    outer: &'composite L1,
    inner: &'composite L2,
) -> Compose<'composite, OUTER, INNER, VALUE, L1, L2>
where
    L1: 'composite + Lense<Object = OUTER, Value = INNER>,
    L2: 'composite + Lense<Object = INNER, Value = VALUE>,
{
    Compose {
        outer: outer,
        inner: inner,
    }
}


mod tests {
    use super::Lense;
    use super::L;
    use super::Compose;
    use super::*;

    #[test]
    fn it_works() {

        #[derive(Debug, Clone, Copy)]
        struct Point {
            x: i32,
            y: i32,
        }
        #[derive(Debug, Clone, Copy)]
        struct Turtle {
            id: i32,
            position: Point,
        }


        let turtle_position = lense(|turtle: &Turtle| turtle.position, |turtle: &Turtle,
         pos: &Point| {
            Turtle {
                id: turtle.id,
                position: *pos,
            }
        });

        let point_x = lense(|point: &Point| point.x, |point: &Point, x: &i32| {
            Point { x: *x, y: point.y }
        });

        let turtle_point_x = compose(&turtle_position, &point_x);

        let t1 = Turtle {
            id: 1,
            position: Point { x: 0, y: 0 },
        };
        let t11 = Turtle {
            id: 1,
            position: Point { x: 0, y: 0 },
        };
        let p = turtle_position.get(&t1);
        let t2 = turtle_position.set(&t11, &Point { x: 9, y: 9 });

        let turtle_3 = Turtle {
            id: 3,
            position: Point { x: 0, y: 0 },
        };

        let turtle_3_moved = turtle_point_x.set(&turtle_3, &100);

        println!("##### Result: {:?} ", p);
        println!("##### Result: {:?} ", t2);
        println!("##### Result: {:?} ", turtle_3_moved);
        //println!("##### Result: {:?} ", v2);
        assert!(true);
    }
}