rust/src/librustc_data_structures/snapshot_map/mod.rs

141 lines
3.4 KiB
Rust
Raw Normal View History

2019-02-08 16:36:22 +00:00
use crate::fx::FxHashMap;
use crate::undo_log::{Rollback, Snapshots, UndoLogs, VecLog};
use std::borrow::{Borrow, BorrowMut};
2016-05-21 12:18:09 +00:00
use std::hash::Hash;
use std::marker::PhantomData;
2019-12-22 22:42:04 +00:00
use std::ops;
2016-05-21 12:18:09 +00:00
pub use crate::undo_log::Snapshot;
2016-05-21 12:18:09 +00:00
#[cfg(test)]
mod tests;
2016-05-21 12:18:09 +00:00
pub type SnapshotMapStorage<K, V> = SnapshotMap<K, V, FxHashMap<K, V>, ()>;
pub type SnapshotMapRef<'a, K, V, L> = SnapshotMap<K, V, &'a mut FxHashMap<K, V>, &'a mut L>;
pub struct SnapshotMap<K, V, M = FxHashMap<K, V>, L = VecLog<UndoLog<K, V>>> {
map: M,
undo_log: L,
_marker: PhantomData<(K, V)>,
2016-05-21 12:18:09 +00:00
}
// HACK(eddyb) manual impl avoids `Default` bounds on `K` and `V`.
impl<K, V, M, L> Default for SnapshotMap<K, V, M, L>
2019-12-22 22:42:04 +00:00
where
M: Default,
L: Default,
{
fn default() -> Self {
SnapshotMap { map: Default::default(), undo_log: Default::default(), _marker: PhantomData }
}
}
pub enum UndoLog<K, V> {
2016-05-21 12:18:09 +00:00
Inserted(K),
Overwrite(K, V),
Purged,
2016-05-21 12:18:09 +00:00
}
impl<K, V, M, L> SnapshotMap<K, V, M, L> {
pub fn with_log<L2>(&mut self, undo_log: L2) -> SnapshotMap<K, V, &mut M, L2> {
SnapshotMap { map: &mut self.map, undo_log, _marker: PhantomData }
}
}
impl<K, V, M, L> SnapshotMap<K, V, M, L>
2019-12-22 22:42:04 +00:00
where
K: Hash + Clone + Eq,
M: BorrowMut<FxHashMap<K, V>> + Borrow<FxHashMap<K, V>>,
L: UndoLogs<UndoLog<K, V>>,
{
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 21:16:55 +00:00
pub fn clear(&mut self) {
self.map.borrow_mut().clear();
Generate documentation for auto-trait impls A new section is added to both both struct and trait doc pages. On struct/enum pages, a new 'Auto Trait Implementations' section displays any synthetic implementations for auto traits. Currently, this is only done for Send and Sync. On trait pages, a new 'Auto Implementors' section displays all types which automatically implement the trait. Effectively, this is a list of all public types in the standard library. Synthesized impls for a particular auto trait ('synthetic impls') take into account generic bounds. For example, a type 'struct Foo<T>(T)' will have 'impl<T> Send for Foo<T> where T: Send' generated for it. Manual implementations of auto traits are also taken into account. If we have the following types: 'struct Foo<T>(T)' 'struct Wrapper<T>(Foo<T>)' 'unsafe impl<T> Send for Wrapper<T>' // pretend that Wrapper<T> makes this sound somehow Then Wrapper will have the following impl generated: 'impl<T> Send for Wrapper<T>' reflecting the fact that 'T: Send' need not hold for 'Wrapper<T>: Send' to hold Lifetimes, HRTBS, and projections (e.g. '<T as Iterator>::Item') are taken into account by synthetic impls However, if a type can *never* implement a particular auto trait (e.g. 'struct MyStruct<T>(*const T)'), then a negative impl will be generated (in this case, 'impl<T> !Send for MyStruct<T>') All of this means that a user should be able to copy-paste a synthetic impl into their code, without any observable changes in behavior (assuming the rest of the program remains unchanged).
2017-11-22 21:16:55 +00:00
self.undo_log.clear();
}
2016-05-21 12:18:09 +00:00
pub fn insert(&mut self, key: K, value: V) -> bool {
match self.map.borrow_mut().insert(key.clone(), value) {
2016-05-21 12:18:09 +00:00
None => {
self.undo_log.push(UndoLog::Inserted(key));
2016-05-21 12:18:09 +00:00
true
}
Some(old_value) => {
self.undo_log.push(UndoLog::Overwrite(key, old_value));
2016-05-21 12:18:09 +00:00
false
}
}
}
pub fn remove(&mut self, key: K) -> bool {
match self.map.borrow_mut().remove(&key) {
2016-05-21 12:18:09 +00:00
Some(old_value) => {
self.undo_log.push(UndoLog::Overwrite(key, old_value));
2016-05-21 12:18:09 +00:00
true
}
2016-10-19 19:10:05 +00:00
None => false,
2016-05-21 12:18:09 +00:00
}
}
pub fn get(&self, key: &K) -> Option<&V> {
self.map.borrow().get(key)
2016-05-21 12:18:09 +00:00
}
}
2016-05-21 12:18:09 +00:00
impl<K, V> SnapshotMap<K, V>
where
K: Hash + Clone + Eq,
{
2016-05-21 12:18:09 +00:00
pub fn snapshot(&mut self) -> Snapshot {
self.undo_log.start_snapshot()
2016-05-21 12:18:09 +00:00
}
pub fn commit(&mut self, snapshot: Snapshot) {
self.undo_log.commit(snapshot)
}
pub fn rollback_to(&mut self, snapshot: Snapshot) {
let map = &mut self.map;
self.undo_log.rollback_to(|| map, snapshot)
}
}
2016-05-21 12:18:09 +00:00
impl<'k, K, V, M, L> ops::Index<&'k K> for SnapshotMap<K, V, M, L>
where
K: Hash + Clone + Eq,
M: Borrow<FxHashMap<K, V>>,
{
type Output = V;
fn index(&self, key: &'k K) -> &V {
&self.map.borrow()[key]
}
}
impl<K, V, M, L> Rollback<UndoLog<K, V>> for SnapshotMap<K, V, M, L>
where
K: Eq + Hash,
M: Rollback<UndoLog<K, V>>,
{
fn reverse(&mut self, undo: UndoLog<K, V>) {
self.map.reverse(undo)
2016-05-21 12:18:09 +00:00
}
}
impl<K, V> Rollback<UndoLog<K, V>> for FxHashMap<K, V>
where
K: Eq + Hash,
{
fn reverse(&mut self, undo: UndoLog<K, V>) {
match undo {
UndoLog::Inserted(key) => {
self.remove(&key);
}
UndoLog::Overwrite(key, old_value) => {
self.insert(key, old_value);
}
UndoLog::Purged => {}
}
}
2016-05-21 12:18:09 +00:00
}