1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#[macro_use]
extern crate log;
extern crate ena;
use ena::{
snapshot_vec as sv,
undo_log::{Rollback, Snapshots, UndoLogs},
unify::{self as ut, EqUnifyValue, UnifyKey},
};
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
struct IntKey(u32);
impl UnifyKey for IntKey {
type Value = Option<IntKey>;
fn index(&self) -> u32 {
self.0
}
fn from_index(u: u32) -> IntKey {
IntKey(u)
}
fn tag() -> &'static str {
"IntKey"
}
}
impl EqUnifyValue for IntKey {}
enum UndoLog {
EqRelation(sv::UndoLog<ut::Delegate<IntKey>>),
Values(sv::UndoLog<i32>),
}
impl From<sv::UndoLog<ut::Delegate<IntKey>>> for UndoLog {
fn from(l: sv::UndoLog<ut::Delegate<IntKey>>) -> Self {
UndoLog::EqRelation(l)
}
}
impl From<sv::UndoLog<i32>> for UndoLog {
fn from(l: sv::UndoLog<i32>) -> Self {
UndoLog::Values(l)
}
}
impl Rollback<UndoLog> for TypeVariableStorage {
fn reverse(&mut self, undo: UndoLog) {
match undo {
UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo),
UndoLog::Values(undo) => self.values.reverse(undo),
}
}
}
#[derive(Default)]
struct TypeVariableStorage {
values: sv::SnapshotVecStorage<i32>,
eq_relations: ut::UnificationTableStorage<IntKey>,
}
impl TypeVariableStorage {
fn with_log<'a>(&'a mut self, undo_log: &'a mut TypeVariableUndoLogs) -> TypeVariableTable<'a> {
TypeVariableTable {
storage: self,
undo_log,
}
}
fn len(&mut self) -> usize {
assert_eq!(self.values.len(), self.eq_relations.len());
self.values.len()
}
}
struct TypeVariableTable<'a> {
storage: &'a mut TypeVariableStorage,
undo_log: &'a mut TypeVariableUndoLogs,
}
impl TypeVariableTable<'_> {
fn new(&mut self, i: i32) -> IntKey {
self.storage.values.with_log(&mut self.undo_log).push(i);
self.storage
.eq_relations
.with_log(&mut self.undo_log)
.new_key(None)
}
}
struct Snapshot {
undo_len: usize,
}
struct TypeVariableUndoLogs {
logs: Vec<UndoLog>,
num_open_snapshots: usize,
}
impl Default for TypeVariableUndoLogs {
fn default() -> Self {
Self {
logs: Default::default(),
num_open_snapshots: Default::default(),
}
}
}
impl<T> UndoLogs<T> for TypeVariableUndoLogs
where
UndoLog: From<T>,
{
fn num_open_snapshots(&self) -> usize {
self.num_open_snapshots
}
fn push(&mut self, undo: T) {
if self.in_snapshot() {
self.logs.push(undo.into())
}
}
fn clear(&mut self) {
self.logs.clear();
self.num_open_snapshots = 0;
}
fn extend<J>(&mut self, undos: J)
where
Self: Sized,
J: IntoIterator<Item = T>,
{
if self.in_snapshot() {
self.logs.extend(undos.into_iter().map(UndoLog::from))
}
}
}
impl Snapshots<UndoLog> for TypeVariableUndoLogs {
type Snapshot = Snapshot;
fn actions_since_snapshot(&self, snapshot: &Self::Snapshot) -> &[UndoLog] {
&self.logs[snapshot.undo_len..]
}
fn start_snapshot(&mut self) -> Self::Snapshot {
self.num_open_snapshots += 1;
Snapshot {
undo_len: self.logs.len(),
}
}
fn rollback_to<R>(&mut self, values: impl FnOnce() -> R, snapshot: Self::Snapshot)
where
R: Rollback<UndoLog>,
{
debug!("rollback_to({})", snapshot.undo_len);
if self.logs.len() > snapshot.undo_len {
let mut values = values();
while self.logs.len() > snapshot.undo_len {
values.reverse(self.logs.pop().unwrap());
}
}
if self.num_open_snapshots == 1 {
// The root snapshot. It's safe to clear the undo log because
// there's no snapshot further out that we might need to roll back
// to.
assert!(snapshot.undo_len == 0);
self.logs.clear();
}
self.num_open_snapshots -= 1;
}
fn commit(&mut self, snapshot: Self::Snapshot) {
debug!("commit({})", snapshot.undo_len);
if self.num_open_snapshots == 1 {
// The root snapshot. It's safe to clear the undo log because
// there's no snapshot further out that we might need to roll back
// to.
assert!(snapshot.undo_len == 0);
self.logs.clear();
}
self.num_open_snapshots -= 1;
}
}
/// Tests that a undo log stored externally can be used with TypeVariableTable
#[test]
fn external_undo_log() {
let mut storage = TypeVariableStorage::default();
let mut undo_log = TypeVariableUndoLogs::default();
let snapshot = undo_log.start_snapshot();
storage.with_log(&mut undo_log).new(1);
storage.with_log(&mut undo_log).new(2);
assert_eq!(storage.len(), 2);
undo_log.rollback_to(|| &mut storage, snapshot);
assert_eq!(storage.len(), 0);
}
|