Sebastian Thiel`just fmt`ffc12763 years agoHistory
52 lines · 1.5 KB · Rust
RawDownload
//! A utility to store objects by identity, which deduplicates them while avoiding lifetimes.//!//! We chose to use hashing/identity over pointers as it's possible that different objects end up in the same memory location,//! which would create obscure bugs. The same could happen with hash collisions, but they these are designed to be less likely.use std::{    collections::{btree_map::Entry, hash_map::DefaultHasher, BTreeMap},    hash::{Hash, Hasher},}; pub(crate) type RefMapKey = u64;pub(crate) struct RefMap<T>(BTreeMap<RefMapKey, T>); impl<T> Default for RefMap<T> {    fn default() -> Self {        RefMap(Default::default())    }} impl<T> RefMap<T>where    T: Hash + Clone,{    pub(crate) fn insert(&mut self, value: &T) -> RefMapKey {        let mut s = DefaultHasher::new();        value.hash(&mut s);        let key = s.finish();        match self.0.entry(key) {            Entry::Vacant(e) => {                e.insert(value.clone());                key            }            Entry::Occupied(_) => key,        }    }     pub(crate) fn insert_owned(&mut self, value: T) -> RefMapKey {        let mut s = DefaultHasher::new();        value.hash(&mut s);        let key = s.finish();        match self.0.entry(key) {            Entry::Vacant(e) => {                e.insert(value);                key            }            Entry::Occupied(_) => key,        }    }     pub(crate) fn resolve(&self, key: RefMapKey) -> Option<&T> {        self.0.get(&key)    }}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t