Crates.io | linotype |
lib.rs | linotype |
version | 0.0.1 |
source | src |
created_at | 2022-01-31 18:21:19.358299 |
updated_at | 2022-01-31 18:21:19.358299 |
description | A keyed sequence reprojector that can optionally pin its values. (A stable-ordered transactionally-incremental multi-map.) |
homepage | https://github.com/Tamschi/linotype/tree/v0.0.1 |
repository | https://github.com/Tamschi/linotype |
max_upload_size | |
id | 524730 |
size | 60,191 |
A keyed sequence reprojector that can optionally pin its values.
It can act as list state reconciliator for data-driven GUIs.
More generally speaking, this library can be used to manage item-associated state of a changing sequence.
This crate is optimised for flexible use (requiring only Eq
for keys, with automatic key cache and flexible lifetimes) and relatively low entry counts.
If you're looking to reconcile associated states for a list with many items, especially with large changes to the list between reprojections, a different approach may be more appropriate.
Please use cargo-edit to always add the latest version of this library:
cargo add linotype --features std
(See documentation for more details.)
"std"
Avoids aborting the process if the drop implementation of a key or value inside a pinning [OwnedProjection
] panics.
use linotype::OwnedProjection;
// This is tricky to write as closure except directly as parameter.
// See the `higher-order-closure` in the dependencies for a workaround.
fn selector<'a>(item: &'a mut &'static str) -> &'a str {
item
}
let mut counter = (0..).into_iter();
let mut factory = move |_item: &mut _, _k: &_| {
counter.next().unwrap()
};
// Inferred to store `String` keys (`K`) and `{integer}` values (`V`).
let mut linotype = OwnedProjection::new();
let mut reproject = |iter: &[&'static str]| linotype
.reproject_by_with(
iter.into_iter().copied(), // : IntoIter<Item = T>, here: T = &'static str
selector, // : FnMut(&mut T) -> &Q,
&mut factory, // : FnMut(&mut T, &K) -> V,
)
.map(|(_item, value)| *value)
.collect::<Vec<_>>(); // Left-over values are dropped here. Their slots are recycled.
assert_eq!(reproject(&["a", "b", "c"]), vec![0, 1, 2]);
assert_eq!(reproject(&["a", "b", "c", "d"]), vec![0, 1, 2, 3]);
assert_eq!(reproject(&["a", "b", "c"]), vec![0, 1, 2]);
assert_eq!(reproject(&["a", "b", "c", "d"]), vec![0, 1, 2, 4]);
assert_eq!(reproject(&["e", "c", "b", "a"]), vec![5, 2, 1, 0]);
// Reprojection methods for fallible closures and per-item closures are also available.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING for more information.
linotype
strictly follows Semantic Versioning 2.0.0 with the following exceptions:
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.
Note that dependencies of this crate may have a more lenient MSRV policy!
Please use cargo +nightly update -Z minimal-versions
in your automation if you don't generate Cargo.lock manually (or as necessary) and require support for a compiler older than current stable.