billyrieger / bimap-rs

Generic bijective maps in Rust
Apache License 2.0
129 stars 26 forks source link

Entry API #26

Open glittershark opened 3 years ago

glittershark commented 3 years ago

It'd be really nice to be able to have something akin to the Entry API as it exists on HashMap - something like left_entry and right_entry, for example

billyrieger commented 3 years ago

Thank you for the suggestion! It might be possible to partially replicate the Entry API but anything returning a mutable reference to the inserted value would become an immutable reference instead—otherwise it's possible to violate the bijection invariant. Another issue would be to figure out how to handle any overwritten values in the BiMap. I'd rather not drop them in the background but returning them along with the newly inserted value might be clunky. Could you give me an example of how you'd like to use a hypothetical Entry API for a BiMap?

glittershark commented 3 years ago

here's the code that's currently not using the Entry API that it'd be nice to be able to use an Entry: https://github.com/glittershark/achilles/blob/master/src/tc/mod.rs#L527-L534 (vars.0 is a bimap there). Essentially I'm just recreating or_insert_with with .get_by_right(...).unwrap_or_else(|| .insert_no_overwrite(...))