macro-lens 2.0.1

Provides support for lenses, which are a mechanism in functional programming for focusing on a part of a complex data structure.
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//
// Copyright (c) 2015-2019 Plausible Labs Cooperative, Inc.
// All rights reserved.
//
//
// Copyright (c) 2025 Julius Foitzik on derivative work
// All rights reserved.
//

use std::marker::PhantomData;

pub use crate::path::LensPath;

/// A lens offers a purely functional means to access and/or modify a field that is
/// nested in an immutable data structure.
pub trait Lens {
    /// The lens source type, i.e., the object containing the field.
    type Source;

    /// The lens target type, i.e., the field to be accessed or modified.
    type Target;

    /// Returns a `LensPath` that describes the target of this lens relative to its source.
    fn path(&self) -> LensPath;

    /// Sets the target of the lens. (This requires a mutable source reference, and as such is typically
    /// only used internally.)
    #[doc(hidden)]
    fn mutate(&self, source: &mut Self::Source, target: Self::Target);

    /// Sets the target of the lens and returns the new state of the source. (This consumes the source.)
    fn set(&self, source: Self::Source, target: Self::Target) -> Self::Source {
        let mut mutable_source = source;
        self.mutate(&mut mutable_source, target);
        mutable_source
    }
}

/// A lens that allows the target to be accessed and mutated by reference.
pub trait RefLens: Lens {
    /// Gets a reference to the target of the lens. (This does not consume the source.)
    fn get_ref<'a>(&self, source: &'a Self::Source) -> &'a Self::Target;

    /// Gets a mutable reference to the target of the lens. (This requires a mutable source reference,
    /// and as such is typically only used internally.)
    #[doc(hidden)]
    fn get_mut_ref<'a>(&self, source: &'a mut Self::Source) -> &'a mut Self::Target;

    /// Modifies the target of the lens by applying a function to the current value.
    fn mutate_with_fn(&self, source: &mut Self::Source, f: &dyn Fn(&Self::Target) -> Self::Target) {
        let target = f(self.get_ref(source));
        self.mutate(source, target);
    }

    /// Modifies the target of the lens by applying a function to the current value. This consumes the source.
    fn modify(
        &self,
        source: Self::Source,
        f: &dyn Fn(&Self::Target) -> Self::Target,
    ) -> Self::Source {
        let mut mutable_source = source;
        self.mutate_with_fn(&mut mutable_source, f);
        mutable_source
    }
}

/// A lens that allows the target to be accessed only by cloning or copying the target value.
pub trait ValueLens: Lens {
    /// Gets a copy of the lens target. (This does not consume the source.)
    fn get(&self, source: &Self::Source) -> Self::Target;
}

/// Modifies the target of the lens by applying a function to the current value.
/// (This lives outside the `Lens` trait to allow lenses to be object-safe but
/// still allow for static dispatch on the given closure.)
#[doc(hidden)]
pub fn mutate_with_fn<L: RefLens, F>(lens: &L, source: &mut L::Source, f: F)
where
    F: Fn(&L::Target) -> L::Target,
{
    let target = f(lens.get_ref(source));
    lens.mutate(source, target);
}

/// Modifies the target of the lens by applying a function to the current value. This consumes the source.
/// (This lives outside the `Lens` trait to allow lenses to be object-safe but
/// still allow for static dispatch on the given closure.)
pub fn modify<L: RefLens, F>(lens: &L, source: L::Source, f: F) -> L::Source
where
    F: Fn(&L::Target) -> L::Target,
{
    let mut mutable_source = source;
    mutate_with_fn(lens, &mut mutable_source, f);
    mutable_source
}

impl<L: Lens + ?Sized> Lens for Box<L> {
    type Source = L::Source;
    type Target = L::Target;

    #[inline(always)]
    fn path(&self) -> LensPath {
        (**self).path()
    }

    #[inline(always)]
    fn mutate(&self, source: &mut L::Source, target: L::Target) {
        (**self).mutate(source, target)
    }
}

impl<L: RefLens + ?Sized> RefLens for Box<L> {
    #[inline(always)]
    fn get_ref<'a>(&self, source: &'a L::Source) -> &'a L::Target {
        (**self).get_ref(source)
    }

    #[inline(always)]
    fn get_mut_ref<'a>(&self, source: &'a mut L::Source) -> &'a mut L::Target {
        (**self).get_mut_ref(source)
    }
}

impl<L: ValueLens + ?Sized> ValueLens for Box<L> {
    #[inline(always)]
    fn get(&self, source: &L::Source) -> L::Target {
        (**self).get(source)
    }
}

/// Returns a `Lens` over a single element at the given `index` for a `Vec<T>`.
pub const fn vec_lens<T>(index: usize) -> VecLens<T> {
    VecLens {
        index,
        _marker: PhantomData,
    }
}

#[doc(hidden)]
pub const fn vec_lens_from_marker<T>(_: PhantomData<T>, index: usize) -> VecLens<T> {
    vec_lens(index)
}

/// A lens over a single element within a `Vec<T>`.
pub struct VecLens<T> {
    index: usize,
    _marker: PhantomData<T>,
}

impl<T> VecLens<T> {
    fn missing_index_message(index: usize) -> String {
        format!("vector lens index {index} is out of bounds")
    }
}

impl<T> Lens for VecLens<T> {
    type Source = Vec<T>;
    type Target = T;

    #[inline(always)]
    fn path(&self) -> LensPath {
        LensPath::from_index(self.index)
    }

    #[inline(always)]
    fn mutate(&self, source: &mut Vec<T>, target: T) {
        let slot = source
            .get_mut(self.index)
            .unwrap_or_else(|| panic!("{}", Self::missing_index_message(self.index)));
        *slot = target;
    }
}

impl<T> RefLens for VecLens<T> {
    #[inline(always)]
    fn get_ref<'a>(&self, source: &'a Vec<T>) -> &'a T {
        source
            .get(self.index)
            .unwrap_or_else(|| panic!("{}", Self::missing_index_message(self.index)))
    }

    #[inline(always)]
    fn get_mut_ref<'a>(&self, source: &'a mut Vec<T>) -> &'a mut T {
        source
            .get_mut(self.index)
            .unwrap_or_else(|| panic!("{}", Self::missing_index_message(self.index)))
    }
}

/// Composes a `Lens<A, B>` with another `Lens<B, C>` to produce a new `Lens<A, C>`.
pub fn compose<LHS, RHS>(lhs: LHS, rhs: RHS) -> ComposedLens<LHS, RHS>
where
    LHS: RefLens,
    LHS::Target: 'static,
    RHS: Lens<Source = LHS::Target>,
{
    ComposedLens { lhs, rhs }
}

/// Composes two `Lens`es.
///
/// In pseudocode:
/// ```text,no_run
/// compose(Lens<A, B>, Lens<B, C>) -> Lens<A, C>
/// ```
pub struct ComposedLens<LHS, RHS> {
    lhs: LHS,
    rhs: RHS,
}

impl<LHS, RHS> Lens for ComposedLens<LHS, RHS>
where
    LHS: RefLens,
    LHS::Target: 'static,
    RHS: Lens<Source = LHS::Target>,
{
    type Source = LHS::Source;
    type Target = RHS::Target;

    #[inline(always)]
    fn path(&self) -> LensPath {
        LensPath::concat(self.lhs.path(), self.rhs.path())
    }

    #[inline(always)]
    fn mutate(&self, source: &mut LHS::Source, target: RHS::Target) {
        let rhs_source = self.lhs.get_mut_ref(source);
        self.rhs.mutate(rhs_source, target)
    }
}

impl<LHS, RHS> RefLens for ComposedLens<LHS, RHS>
where
    LHS: RefLens,
    LHS::Target: 'static,
    RHS: RefLens<Source = LHS::Target>,
{
    #[inline(always)]
    fn get_ref<'a>(&self, source: &'a LHS::Source) -> &'a RHS::Target {
        self.rhs.get_ref(self.lhs.get_ref(source))
    }

    #[inline(always)]
    fn get_mut_ref<'a>(&self, source: &'a mut LHS::Source) -> &'a mut RHS::Target {
        self.rhs.get_mut_ref(self.lhs.get_mut_ref(source))
    }
}

impl<LHS, RHS> ValueLens for ComposedLens<LHS, RHS>
where
    LHS: RefLens,
    LHS::Target: 'static,
    RHS: ValueLens<Source = LHS::Target>,
{
    #[inline(always)]
    fn get(&self, source: &LHS::Source) -> RHS::Target {
        self.rhs.get(self.lhs.get_ref(source))
    }
}

#[cfg(test)]
mod tests {
    use crate::{Lens, LensPath, Lenses, RefLens, ValueLens, compose_lens, modify, vec_lens};

    #[derive(Clone, Debug, PartialEq, Lenses)]
    struct Struct1 {
        int32: i32,
        int16: i16,
    }

    #[derive(Clone, Debug, PartialEq, Lenses)]
    struct Struct2 {
        int32: i32,
        string: String,
        struct1: Struct1,
    }

    #[derive(Clone, Debug, PartialEq, Lenses)]
    struct Struct3 {
        int32: i32,
        struct2: Struct2,
    }

    #[derive(Clone, Debug, PartialEq, Lenses)]
    struct Struct4 {
        inner_vec: Vec<Struct1>,
    }

    fn make_struct3() -> Struct3 {
        Struct3 {
            int32: 332,
            struct2: Struct2 {
                int32: 232,
                string: "hi".to_string(),
                struct1: Struct1 {
                    int32: 132,
                    int16: 116,
                },
            },
        }
    }

    #[test]
    fn a_basic_lens_should_work() {
        let lens = crate::lens!(Struct3.struct2.struct1.int32);

        let s3_0 = make_struct3();
        assert_eq!(*lens.get_ref(&s3_0), 132);
        assert_eq!(lens.path(), LensPath::from_vec(vec![1, 2, 0]));

        let s3_1 = lens.set(s3_0, 133);
        assert_eq!(s3_1.struct2.struct1.int32, 133);
        assert_eq!(s3_1.struct2.struct1.int16, 116);

        let s3_2 = lens.modify(s3_1, &|a| a + 1);
        assert_eq!(s3_2.struct2.struct1.int32, 134);

        let s3_3 = modify(&lens, s3_2, |a| a + 1);
        assert_eq!(s3_3.struct2.struct1.int32, 135);
    }

    #[test]
    fn lens_composition_should_work_with_boxed_lenses() {
        let struct1_int32_lens: Box<dyn RefLens<Source = Struct1, Target = i32>> =
            Box::new(Struct1Int32Lens);
        let lens = compose_lens!(
            Struct3Struct2Lens,
            Box::new(Struct2Struct1Lens),
            struct1_int32_lens
        );

        let s3_0 = make_struct3();
        assert_eq!(*lens.get_ref(&s3_0), 132);

        let s3_1 = lens.set(s3_0, 133);
        assert_eq!(s3_1.struct2.struct1.int32, 133);

        let s3_2 = lens.modify(s3_1, &|a| a + 1);
        assert_eq!(s3_2.struct2.struct1.int32, 134);

        let s3_3 = modify(&lens, s3_2, |a| a + 1);
        assert_eq!(s3_3.struct2.struct1.int32, 135);
    }

    #[test]
    fn value_lenses_are_generated_for_scalars_and_strings() {
        assert_eq!(Struct1Int32Lens.get(&Struct1 { int32: 7, int16: 9 }), 7);
        assert_eq!(
            Struct2StringLens.get(&Struct2 {
                int32: 0,
                string: "hello".to_string(),
                struct1: Struct1 { int32: 0, int16: 0 },
            }),
            "hello".to_string()
        );
    }

    #[test]
    fn a_vec_lens_should_work() {
        let lens = vec_lens::<u32>(1);

        let v0 = vec![0u32, 1, 2];
        assert_eq!(*lens.get_ref(&v0), 1);
        assert_eq!(lens.path(), LensPath::from_index(1));

        let v1 = lens.set(v0, 42);
        assert_eq!(v1, vec![0u32, 42, 2]);

        let v2 = modify(&lens, v1, |a| a - 1);
        assert_eq!(v2, vec![0u32, 41, 2]);
    }

    #[test]
    #[should_panic(expected = "vector lens index 3 is out of bounds")]
    fn a_vec_lens_panics_with_a_clear_message() {
        let lens = vec_lens::<u32>(3);
        let _ = lens.get_ref(&vec![0u32, 1, 2]);
    }

    #[test]
    fn the_lens_macro_should_support_vec_indexing() {
        let lens = crate::lens!(Struct4.inner_vec[1].int32);

        let s0 = Struct4 {
            inner_vec: vec![
                Struct1 {
                    int32: 42,
                    int16: 73,
                },
                Struct1 {
                    int32: 110,
                    int16: 210,
                },
            ],
        };
        assert_eq!(*lens.get_ref(&s0), 110);
        assert_eq!(lens.path(), LensPath::from_vec(vec![0, 1, 0]));

        let s1 = lens.set(s0, 111);
        assert_eq!(s1.inner_vec[1].int32, 111);

        let s2 = modify(&lens, s1, |a| a + 1);
        assert_eq!(s2.inner_vec[1].int32, 112);
    }
}