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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! A view into a data structure.
//!
//! A lens describes a view into a data structure, like an index or key lookup,
//! with the ability to modify what it sees. It provides a get operation (get
//! the thing we're a lens for) and a set operation (update the data structure
//! we're a lens into with the new value for the thing).
//!
//! Lenses are composable, so that if you have a lens from A to a thing B inside
//! A, and you have a lens from B to another thing C inside B, you can compose
//! them to make a lens from A directly into C.
//!
//! There are two kinds of lenses defined here: `PartialLens`, which is a lens
//! into something which doesn't necessarily exist (such as a map key), and
//! `Lens`, which must always be able to succeed (like a lens into a struct,
/// whose contents are known and guaranteed). All `Lens`es are also
/// `PartialLens`es, but the opposite is not true.

use std::marker::PhantomData;
use std::sync::Arc;

use shared::Shared;

/// A lens from `From` to `To` where the focus of the lens isn't guaranteed to
/// exist inside `From`. Operations on these lenses therefore return `Option`s.
pub trait PartialLens: Clone {
    type From;
    type To;

    /// Get the focus of the lens, if available.
    fn try_get(&self, s: &Self::From) -> Option<Arc<Self::To>>;

    /// Put a value into the lens, returning the updated `From` value if the
    /// operation succeeded.
    fn try_put<Convert>(&self, v: Option<Convert>, s: &Self::From) -> Option<Self::From>
    where
        Convert: Shared<Self::To>;

    /// Compose this lens with a lens from `To` to a new type `Next`, yielding
    /// a lens from `From` to `Next`.
    fn try_chain<L, Next>(&self, next: &L) -> Compose<Self::From, Self::To, Next, Self, L>
    where
        L: PartialLens<From = Self::To, To = Next>,
    {
        compose(self, next)
    }
}

/// A lens from `From` to `To` where `From` is guaranteed to contain the focus
/// of the lens (ie. get and put operations cannot fail).
///
/// These must also implement `PartialLens`, so a default implementation is
/// provided which will just unwrap the results of `try_get` and `try_put`.
pub trait Lens: PartialLens {
    /// Get the focus of the lens.
    fn get(&self, s: &Self::From) -> Arc<Self::To> {
        self.try_get(s).unwrap()
    }

    /// Put a value into the lens, returning the updated `From` value.
    fn put<Convert>(&self, v: Convert, s: &Self::From) -> Self::From
    where
        Convert: Shared<Self::To>,
    {
        self.try_put(Some(v), s).unwrap()
    }

    /// Compose this lens with a lens from `To` to a new type `Next`, yielding
    /// a lens from `From` to `Next`.
    fn chain<L, Next>(&self, next: &L) -> Compose<Self::From, Self::To, Next, Self, L>
    where
        L: Lens<From = Self::To, To = Next>,
    {
        compose(self, next)
    }
}

pub struct Compose<A, B, C, L, R>
where
    L: PartialLens<From = A, To = B>,
    R: PartialLens<From = B, To = C>,
{
    left: Arc<L>,
    right: Arc<R>,
    phantom_a: PhantomData<A>,
    phantom_b: PhantomData<B>,
    phantom_c: PhantomData<C>,
}

impl<A, B, C, L, R> Clone for Compose<A, B, C, L, R>
where
    L: PartialLens<From = A, To = B>,
    R: PartialLens<From = B, To = C>,
{
    fn clone(&self) -> Self {
        Compose {
            left: self.left.clone(),
            right: self.right.clone(),
            phantom_a: PhantomData,
            phantom_b: PhantomData,
            phantom_c: PhantomData,
        }
    }
}

impl<A, B, C, L, R> PartialLens for Compose<A, B, C, L, R>
where
    L: PartialLens<From = A, To = B>,
    R: PartialLens<From = B, To = C>,
{
    type From = A;
    type To = C;

    fn try_get(&self, s: &A) -> Option<Arc<C>> {
        match self.left.try_get(s) {
            None => None,
            Some(s2) => self.right.try_get(&s2),
        }
    }

    fn try_put<FromC>(&self, v: Option<FromC>, s: &A) -> Option<A>
    where
        FromC: Shared<C>,
    {
        self.left
            .try_get(&s)
            .and_then(|s2| self.right.try_put(v, &s2))
            .and_then(|s3| self.left.try_put(Some(s3), &s))
    }
}

impl<A, B, C, L, R> Lens for Compose<A, B, C, L, R>
where
    L: Lens<From = A, To = B>,
    R: Lens<From = B, To = C>,
{
}

/// Compose a lens from `A` to `B` with a lens from `B` to `C`, yielding
/// a lens from `A` to `C`.
pub fn compose<A, B, C, L, R>(left: &L, right: &R) -> Compose<A, B, C, L, R>
where
    L: PartialLens<From = A, To = B>,
    R: PartialLens<From = B, To = C>,
{
    Compose {
        left: Arc::new(left.clone()),
        right: Arc::new(right.clone()),
        phantom_a: PhantomData,
        phantom_b: PhantomData,
        phantom_c: PhantomData,
    }
}

/// An arbitrary non-partial lens defined by a pair of get and put functions.
pub struct GeneralLens<From, To> {
    get: Arc<Fn(&From) -> Arc<To>>,
    put: Arc<Fn(&From, Arc<To>) -> From>,
}

impl<From, To> GeneralLens<From, To> {
    /// Construct a lens from `From` to `To` from a pair of get and put functions.
    pub fn new(
        get: Arc<Fn(&From) -> Arc<To>>,
        put: Arc<Fn(&From, Arc<To>) -> From>,
    ) -> GeneralLens<From, To> {
        GeneralLens { get, put }
    }
}

impl<From, To> Clone for GeneralLens<From, To> {
    fn clone(&self) -> Self {
        GeneralLens {
            get: self.get.clone(),
            put: self.put.clone(),
        }
    }
}

impl<A, B> PartialLens for GeneralLens<A, B> {
    type From = A;
    type To = B;

    fn try_get(&self, s: &A) -> Option<Arc<B>> {
        Some((self.get)(s))
    }

    fn try_put<Convert>(&self, v: Option<Convert>, s: &A) -> Option<A>
    where
        Convert: Shared<B>,
    {
        Some((self.put)(s, v.unwrap().shared()))
    }
}

impl<A, B> Lens for GeneralLens<A, B> {
    fn get(&self, s: &A) -> Arc<B> {
        (self.get)(s)
    }

    fn put<Convert>(&self, v: Convert, s: &A) -> A
    where
        Convert: Shared<B>,
    {
        (self.put)(s, v.shared())
    }
}

/// Construct a lens into a struct, or a series of structs.
///
/// You'll need to specify the type of the source struct, the
/// name of the field you want a lens into, and the type of that
/// field, separated by colons, eg.
/// `lens!(MyStruct: string_field: String)`.
/// You can keep repeating name/type pairs for fields inside structs
/// inside structs.
///
/// **Please note:** this only works on fields which are wrapped in an
/// `Arc` (so the type of the `string_field` field in the example in
/// the previous paragraph must be `Arc<String`>), and the source
/// struct must implement `Clone`.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate im;
/// # use im::lens::Lens;
/// # use std::sync::Arc;
/// #[derive(Clone)]
/// struct Name {
///     first: Arc<String>,
///     last: Arc<String>
/// }
///
/// #[derive(Clone)]
/// struct Person {
///     name: Arc<Name>
/// }
///
/// # fn main() {
/// let person_last_name = lens!(Person: name: Name: last: String);
///
/// let the_admiral = Person {
///     name: Arc::new(Name {
///         first: Arc::new("Grace".to_string()),
///         last: Arc::new("Hopper".to_string())
///     })
/// };
///
/// assert_eq!(
///     Arc::new("Hopper".to_string()),
///     person_last_name.get(&the_admiral)
/// );
/// # }
/// ```
#[macro_export]
macro_rules! lens {
    ( $from:ident : $headpath:ident : $headto:ident : $($tail:tt):* ) => {
        $crate::lens::compose(&lens!($from : $headpath : $headto), &lens!($headto : $($tail):*))
    };

    ( $from:ident : $path:ident : $to:ident ) => {
        $crate::lens::GeneralLens::<$from, $to>::new(
            ::std::sync::Arc::new(|st| st.$path.clone()),
            ::std::sync::Arc::new(|st, v| {
                $from {
                    $path: v,
                    ..st.clone()
                }
            })
        )
    };
}

#[cfg(test)]
mod test {
    use super::*;

    #[derive(Clone)]
    struct Inner {
        omg: Arc<String>,
        wtf: Arc<String>,
    }

    #[derive(Clone)]
    struct Outer {
        inner: Arc<Inner>,
    }

    #[test]
    fn struct_lens() {
        let l = lens!(Outer: inner: Inner: omg: String);
        let omglol = "omg lol".to_string();
        let inner = Inner {
            omg: Arc::new(omglol.clone()),
            wtf: Arc::new("nope".to_string()),
        };
        let outer = Outer {
            inner: Arc::new(inner),
        };
        assert_eq!(Arc::new(omglol), l.get(&outer))
    }
}