#pattern #traversing #lens

macro lens-rs_derive

macro to derive lens for data type

13 unstable releases (3 breaking)

0.3.2 May 7, 2021
0.3.1 Apr 8, 2021
0.2.2 Apr 3, 2021
0.2.1 Mar 29, 2021
0.0.88 Mar 21, 2021

#993 in Rust patterns

Download history 114/week @ 2022-06-02 12/week @ 2022-06-09 13/week @ 2022-06-16 11/week @ 2022-06-23 10/week @ 2022-06-30 43/week @ 2022-07-07 25/week @ 2022-07-14 54/week @ 2022-07-21 11/week @ 2022-07-28 40/week @ 2022-08-04 21/week @ 2022-08-11 11/week @ 2022-08-18 40/week @ 2022-08-25 14/week @ 2022-09-01 57/week @ 2022-09-08 9/week @ 2022-09-15

121 downloads per month
Used in 3 crates (via lens-rs)

MIT/Apache

100KB
2.5K SLoC

Overview

deriving lens for custom data types.

see the guide

Example

#[derive(Debug, Lens)]
struct Baz<'a, A, B, C>{
    #[optic(ref)] 
    a: &'a A,     // can only take the immutable ref by optics::a
    #[optic(mut)] 
    b: &'a mut B, // can take the mutable ref by optics::b
    #[optic]
    c: C          // can mv it out by by optics::c
}

#[derive(Review, Prism, Debug)]
enum AnEnum<T> {
    A(T, i32), // couldn't derive Prism or Review
    #[optic] 
    B(T),
    #[optic]
    C,
    #[optic]
    D(),
    #[optic]
    E {},
}

#[derive(Lens, Debug)]
struct Foo {
    #[optic] a: i32,
    #[optic] b: i32,
}

fn test() -> Option<()> {
    let x = Review::review(optics!(Some.B), Foo {
        a: 3,
        b: 2,
    });
    assert_eq!(x.preview(optics!(Some.B.b))?, 2);
    
    Some(())
}

Limitations

  • can't derive Lens for enum.
  • can't derive Prism and Review for the variant has more than one argument or has named field.

Dependencies

~0.2–0.9MB
~20K SLoC