lens-rs_derive 0.3.2

macro to derive lens for data type
docs.rs failed to build lens-rs_derive-0.3.2
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: lens-rs_derive-0.3.1

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.