#pin #map #data-driven #stateful #reprojection

no-std linotype

A keyed sequence reprojector that can optionally pin its values. (A stable-ordered transactionally-incremental multi-map.)

1 unstable release

0.0.1 Jan 31, 2022

#25 in #data-driven

MIT/Apache

38KB
567 lines

linotype

Lib.rs Crates.io Docs.rs

Rust 1.56 CI Crates.io - License

GitHub open issues open pull requests good first issues

crev reviews Zulip Chat

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.

Audience

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.

Installation

Please use cargo-edit to always add the latest version of this library:

cargo add linotype --features std

Features

(See documentation for more details.)

"std"

Avoids aborting the process if the drop implementation of a key or value inside a pinning OwnedProjection panics.

Example

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.

License

Licensed under either of

at your option.

Contribution

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.

Code of Conduct

Changelog

Versioning

linotype strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes (except for v1).
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
    Consider it the global patch level.

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.

Dependencies

~105KB