Skip to content

Allow lens derive to add consts for custom lenses #836

Open
@cmyr

Description

@cmyr

It would be nice if, when writing a custom lens, you could annotate the type so
that the derive macro would include the custom lens along with the derived
lenses. This would look something like (forgive my contrived example):

#[derive(Lens)]
#[lens(custom(speed_kmph = "lenses::Kmph"))]
struct Arrow {
    // a unit vector
    heading: Vec2,
    // in mph
    speed: f64,
}

mod lenses {
    struct Kmph;

    const KM_PER_MILE: f64 = 1.609_344;

    impl Lens<Arrow, f64> for MilesToKm {
        fn with<R, F: FnOnce(&f64) -> R>(&self, data: &Arrow, f: F) -> R {
            let kms = data.speed * KM_PER_MILE;
            f(&kms)
        }

        fn with_mut<R, F: FnOnce(&mut f64) -> R>(&self, data: &mut Arrow, f: F) -> R {
            let mut kms = data.speed * KM_PER_MILE;
            let kms_2 = kms;
            let result = f(&mut kms);
            // avoid doing the conversion if unchanged, it might be lossy?
            if !kms.same(&kms_2) {
                let miles = kms * KM_PER_MILE.recip();
                data.speed = miles;
            }
            result
        }
    }
}

and then I would be able to access this lenes as Arrow::speed_kmph.

Metadata

Metadata

Assignees

No one assigned

    Labels

    D-Mediumrequires some research and thinkingderiveconcerns a druid-derive featureenhancementadds or requests a new featurehelp wantedhas no one working on it yet

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions