Lenses in Rust and My Solution

17 points by LambdaLemon 22 hours ago on lobsters | 4 comments

polywolf | 11 hours ago

This is a fairly nice API, especially compared to the examples you show, but the part I'm still confused about is how a self: &Optics<'_, Foo> allows for mutation of self.y. It is behind a shared reference, so any mutation seems about as safe as mutation of a RefCell. That is, it needs to be checked by hand & at runtime instead of being enforced by the type system.

Looking at your code, this is indeed the case: there are panic!("gulp") cases to handle invalid borrows. Do you know if the other lens crates have this same failure mode?

[OP] LambdaLemon | 11 hours ago

The example pseudo-code isn't meant to be an example on how to use my library, just something to show people who are unfamiliar with lenses in general how they work. Maybe I should make a note about this.

Looking at your code, this is indeed the case: there are panic!("gulp") cases to handle invalid borrows. Do you know if the other lens crates have this same failure mode?

I have no idea to be honest. I just spent 30 minutes trying to figure out what lens-rs is doing but the code is very obtuse.

aspizu | 17 hours ago

wow ur the coolest ur so good at rust lemon this is so crazy no way im literally crying

nonagoninf | 15 hours ago

I see that this is your first comment, welcome to lobste.rs! You might want to read the about page to avoid making comments that get flagged. In this particular case:

"Me-too" when a comment doesn't add new information, typically a single sentence of appreciation, agreement, or humor;

Following the guidelines keeps the quality of discussion high. At any rate, enjoy the site!