flense
Purpose-oriented lensing for Rust.
State
This is not production ready and should not be relied on. The tests pass under miri, but no thorough deep validation of the pointers and logic has been performed that would give a high degree of confidence. Stacked borrows are hard.
High-level
Rather than focusing on accessing specific fields of specific structures, flense allows a sort of duck-typing for lenses, where you annotate purpose-oriented fields like Position or Color (or whatever you please!), then define how to adapt the data inside your structure into flense.
Other lensing libraries can only lense the original structure and its original fields. You can still achieve that with flense by creating a specialized Field for each specific structure field.
Invalid mutable lenses which alias data should be prevented at compile time, though cargo check may fail to find them when cargo build does.
Properties
Creating a lens is simple: some_data.lens(). Type inference drives the creation of the correct lens.
This crate should largely compile out and be zero overhead. Functions taking reified lenses have a small performance overhead since the size of a lens is dependent on the number of fields in it.
Examples
use *;
// A plain old data type, defined anywhere - if it was defined outside your lib,
// you could wrap it in a #[repr(transparent)] wrapper and pierce through it.
// Or it's relatively straightforward to implement a reflexive adapter yourself
// if you want full control.
unsafe
// Somewhere else, possibly even provided in a library which is unaware of the
// concrete Vertex definition entirely.
// Or, you can avoid performing code generation of the body multiple times by
// separating the lens construction from its usage, though this may prevent
// optimizations like vectorization.
use *;
use offset_of;
// hacky drag for the velocities via linear decay
// positions is guaranteed to be rectangular, so you don't have to worry about
// one slice being longer or shorter than the other
// or you can take multiple lenses for more flexibility
Prior art, other lenses
flense was heavily inspired by some work I've done in creating a sound wrapper for meshoptimizer (which will eventually be published). In doing so, I explored concepts used in terrors and frunk.
grist_lensis another lens library which I saw the announcement of a few days before separating this one from mymeshoptimizerwrapper, though I haven't used it. I did, however, read the author's blog post going over the various lens options she has seen and used.lens-rspl-lens