A prickly BTreeMap.
You can insert through shared references and values are pin-projected.
You can remove keys and drop entries through exclusive references.
You can remove values through exclusive references until the PineMap
is pinned.
I need only a fairly barebones implementation and not necessarily optimized version of this data structure for my own project(s), but am committed to maintaining it into shape if there's interest.
Follow the "good first issues" badge above for starting points!
Note that the crate uses unsafe code very frequently, so you should be at least somewhat comfortable with ensuring soundness manually. Code reviews are also highly appreciated, both within and outside of this regard.
Please use cargo-edit to always add the latest version of this library:
cmd
cargo add pinus
```rust use ergopin::ergopin; use pinus::{prelude::*, sync::PineMap}; use std::{convert::Infallible, pin::Pin};
fn main() {
// PineMap
is interior-mutable, so either is useful:
let map = PineMap::new();
let mut mut_map = PineMap::new();
// Get parallel shared references by inserting like this: let a: &String = map.insert("Hello!", "Hello!".tostring()) .unwrap(/* Your data back if the entry already existed. */); let b: &String = map.insertwith("Hello again!", |k| k.tostring()) .maperr(|(key, factory)| key).unwrap(); let c: &String = map.tryinsertwith::<_, Infallible>("Hello once again!", |k| Ok(k.tostring())) .unwrap(/* Error from factory. */) .map_err(|(key, _factory)| key).unwrap();
let a2: &String = map.get("Hello!").unwrap();
let _ = (a, a2, b, c);
// Get exclusive references like this (also with or without factory): let muta: &mut String = mutmap.insertwithmut("Hi!", |k| k.tostring()) .maperr(|(key, _factory)| key).unwrap();
let muta2: &mut String = mutmap.get_mut("Hi!").unwrap();
// The …_mut
methods are actually faster, but their results can't be held onto at once:
// let _ = (muta, muta2); // "error[E0499]: cannot borrow mut_map
as mutable more than once at a time"
// Remove entries like this:
mutmap.clear();
let _: Option<(&str, String)> = mutmap.removepair("A");
let _: Option
/////
// Now on to part 2, pin projection: let mut map: Pin<&> = pin!(map).asref(); let mut mutmap: Pin<&mut _> = pin!(mutmap);
// Shared references are now pinned: let a: Pin<&String> = map.insert("Hello!!", "Hello!!".tostring()) .unwrap(); let b: Pin<&String> = map.insertwith("Hello again!!", |k| k.tostring()) .ok().unwrap(); let c: Pin<&String> = map.tryinsertwith::<_, Infallible>("Hello once again!!", |k| Ok(k.tostring())) .unwrap().ok().unwrap();
let a2: Pin<&String> = map.get("Hello!").unwrap();
let _ = (a, a2, b, c);
// Exclusive references are also pinned: let mut muta: Pin<&mut String> = mutmap.asmut().insertwithmut("Hi!", |k| k.tostring()) .map_err(|(key, _factory)| key).unwrap();
let mut muta2: Pin<&mut String> = mutmap.asmut().getmut_pinned("Hi!").unwrap();
// The …_mut
methods are actually faster, but their results can't be held onto at once:
// let _ = (muta, muta2); // "error[E0499]: cannot borrow mut_map
as mutable more than once at a time"
// Only keys can be removed now, but values must be dropped in place: mutmap.asmut().clear(); let : Option<&str> = mutmap.asmut().removekey("C"); let : bool = mutmap.asmut().dropentry("D"); } ```
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.
pinus
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.