From 52c34625866f6e23fd0de484282f326da6a100e3 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 7 Apr 2015 06:11:49 -0400 Subject: [PATCH] Use the newer snapshot_vec, which has a simplified delegate interface since in practice no delegates had any state. --- src/librustc/lib.rs | 1 - src/librustc/middle/infer/type_variable.rs | 8 ++--- src/librustc/middle/infer/unify.rs | 6 ++-- src/librustc_data_structures/lib.rs | 2 ++ .../snapshot_vec.rs | 31 ++++++++++++++++--- 5 files changed, 34 insertions(+), 14 deletions(-) rename src/{librustc/util => librustc_data_structures}/snapshot_vec.rs (87%) diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 4f7bb3d528a..6837483a422 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -142,7 +142,6 @@ pub mod util { pub mod common; pub mod ppaux; pub mod nodemap; - pub mod snapshot_vec; pub mod lev_distance; } diff --git a/src/librustc/middle/infer/type_variable.rs b/src/librustc/middle/infer/type_variable.rs index 03612a6c1ae..b3e3e016d85 100644 --- a/src/librustc/middle/infer/type_variable.rs +++ b/src/librustc/middle/infer/type_variable.rs @@ -17,7 +17,7 @@ use std::marker::PhantomData; use std::mem; use std::u32; -use util::snapshot_vec as sv; +use rustc_data_structures::snapshot_vec as sv; pub struct TypeVariableTable<'tcx> { values: sv::SnapshotVec>, @@ -65,7 +65,7 @@ fn opposite(self) -> RelationDir { impl<'tcx> TypeVariableTable<'tcx> { pub fn new() -> TypeVariableTable<'tcx> { - TypeVariableTable { values: sv::SnapshotVec::new(Delegate(PhantomData)) } + TypeVariableTable { values: sv::SnapshotVec::new() } } fn relations<'a>(&'a mut self, a: ty::TyVid) -> &'a mut Vec { @@ -201,9 +201,7 @@ impl<'tcx> sv::SnapshotVecDelegate for Delegate<'tcx> { type Value = TypeVariableData<'tcx>; type Undo = UndoEntry; - fn reverse(&mut self, - values: &mut Vec>, - action: UndoEntry) { + fn reverse(values: &mut Vec>, action: UndoEntry) { match action { SpecifyVar(vid, relations) => { values[vid.index as usize].value = Bounded(relations); diff --git a/src/librustc/middle/infer/unify.rs b/src/librustc/middle/infer/unify.rs index 4bbced1d75c..5aec4227136 100644 --- a/src/librustc/middle/infer/unify.rs +++ b/src/librustc/middle/infer/unify.rs @@ -17,7 +17,7 @@ use std::fmt::Debug; use std::marker::PhantomData; use syntax::ast; -use util::snapshot_vec as sv; +use rustc_data_structures::snapshot_vec as sv; /// This trait is implemented by any type that can serve as a type /// variable. We call such variables *unification keys*. For example, @@ -95,7 +95,7 @@ pub struct Node { impl UnificationTable { pub fn new() -> UnificationTable { UnificationTable { - values: sv::SnapshotVec::new(Delegate(PhantomData)), + values: sv::SnapshotVec::new(), } } @@ -213,7 +213,7 @@ impl sv::SnapshotVecDelegate for Delegate { type Value = VarValue; type Undo = (); - fn reverse(&mut self, _: &mut Vec>, _: ()) { + fn reverse(_: &mut Vec>, _: ()) { panic!("Nothing to reverse"); } } diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index abac991e5ce..5f2f430df50 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -31,3 +31,5 @@ #[macro_use] extern crate log; extern crate serialize as rustc_serialize; // used by deriving + +pub mod snapshot_vec; diff --git a/src/librustc/util/snapshot_vec.rs b/src/librustc_data_structures/snapshot_vec.rs similarity index 87% rename from src/librustc/util/snapshot_vec.rs rename to src/librustc_data_structures/snapshot_vec.rs index d2e0b3aec2f..5ab740f3629 100644 --- a/src/librustc/util/snapshot_vec.rs +++ b/src/librustc_data_structures/snapshot_vec.rs @@ -21,6 +21,7 @@ use self::UndoLog::*; use std::mem; +use std::ops; pub enum UndoLog { /// Indicates where a snapshot started. @@ -42,7 +43,6 @@ pub enum UndoLog { pub struct SnapshotVec { values: Vec, undo_log: Vec>, - delegate: D } // Snapshots are tokens that should be created/consumed linearly. @@ -55,15 +55,14 @@ pub trait SnapshotVecDelegate { type Value; type Undo; - fn reverse(&mut self, values: &mut Vec, action: Self::Undo); + fn reverse(values: &mut Vec, action: Self::Undo); } impl SnapshotVec { - pub fn new(delegate: D) -> SnapshotVec { + pub fn new() -> SnapshotVec { SnapshotVec { values: Vec::new(), undo_log: Vec::new(), - delegate: delegate } } @@ -77,6 +76,10 @@ pub fn record(&mut self, action: D::Undo) { } } + pub fn len(&self) -> usize { + self.values.len() + } + pub fn push(&mut self, elem: D::Value) -> usize { let len = self.values.len(); self.values.push(elem); @@ -159,7 +162,7 @@ pub fn rollback_to(&mut self, snapshot: Snapshot) { } Other(u) => { - self.delegate.reverse(&mut self.values, u); + D::reverse(&mut self.values, u); } } } @@ -184,3 +187,21 @@ pub fn commit(&mut self, snapshot: Snapshot) { } } } + +impl ops::Deref for SnapshotVec { + type Target = [D::Value]; + fn deref(&self) -> &[D::Value] { &*self.values } +} + +impl ops::DerefMut for SnapshotVec { + fn deref_mut(&mut self) -> &mut [D::Value] { &mut *self.values } +} + +impl ops::Index for SnapshotVec { + type Output = D::Value; + fn index(&self, index: usize) -> &D::Value { self.get(index) } +} + +impl ops::IndexMut for SnapshotVec { + fn index_mut(&mut self, index: usize) -> &mut D::Value { self.get_mut(index) } +}