Crates.io | myopic |
lib.rs | myopic |
version | 0.1.2 |
source | src |
created_at | 2019-09-30 01:42:00.010969 |
updated_at | 2019-10-02 14:33:56.798045 |
description | A possibly bad lens library for Rust |
homepage | |
repository | https://github.com/nsmryan/myopic |
max_upload_size | |
id | 168728 |
size | 58,781 |
My optics library, Myopic, is a possibly bad implementation of the concept of a functional lense. This is a beautiful concept, and if you want an elegant implementation see lens from the Haskell ecosystem, and one of the many great introductions such as lens over tea.
Myopic is not elegant- it satisfies a need that I have for an implementation of lense that acts on mutable data, which has no or little performance penalty, and which is simple enough to use in other libraries of mine.
This library contains three implementations of the concept of lenses. They are all the simplest kind of lenses which can only get and set data in structures (not the fancy Haskell ones which can do much more).
The lenses are implemented as struct containing either closures (lens), boxed trait objects (lens_box), or function pointers (lens_fn).
Each lense implementation has a Lens type containing functions for getting and setting data, and a ComposedLens type for nested compositions of lenses and ComposedLens. This implementation comes from the lenses library (credit where credit is due).
The reason there are three implementations was to benchmark them, as described below. The takeaway is that the 'lens' module is at worst much faster, and at best much much faster then the others.
The benchmarks can be run with cargo bench
cargo bench
They show that the 'lens' module is by far the fastest. It appears to be able to inline such that I have to use black_box in criterion just to get any benchmarking data out of them.
The other implementations seem to incur some indirection penalty which doesn't get inlined (I assume). This appears to get worst the more they are composed, while a level of composition in 'lens' does not incur a noticable penalty.
The joke in the name is that this is both my own optics library, and it is likely a bad optics library. Myopic means having bad vision, and it sounds like "my optics". Hopefully this is a little amusing.
There are a number of Rust lense libraries, some of which are quite extensive, and some of which have vastly different designs.
Myopic is licensed under either MIT or APACHE2, whichever you prefer.