Allow lens derive to add consts for custom lenses
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 9.7k
- Forks
- 565
- PR merge metrics
- No merged PRs in 30d
Description
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):
```rust
#[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 for MilesToKm {
fn with R>(&self, data: &Arrow, f: F) -> R {
let kms = data.speed * KM_PER_MILE;
f(&kms)
}
fn with_mut 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`.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names the Rust Lens derive macro, the proposed #[lens(custom(...))] attribute, and the Arrow::speed_kmph entry point; no file or test is provided. Start by locating the derive-macro implementation and relevant lens-generation tests, then establish completion as exposing the custom Kmph lens through Arrow::speed_kmph while preserving its custom behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100