I'm trying to build a data structure where multiple keys may point to the same value. However, I'm trying to avoid reference counting if possible as it has an overhead. Here are some ideas I've tried:
use std::collections::BTreeMap;
use std::rc::Rc;
use serde::Deserialize;
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
struct Key;
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
struct Value;
// Uses reference counting (perf cost).
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Option1 {
db: BTreeMap<Key, Rc<Value>>,
}
// Cannot be serialized / deserialized.
// How to even have non-dropped references?
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Option2<'a> {
values: Vec<Value>,
db: BTreeMap<Key, &'a Value>,
}
// Inelegant, and no compile-time check for
// possible bad indices go out of sync with the values.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Option3 {
values: Vec<Value>,
indices: BTreeMap<Key, usize>,
}