about summary refs log tree commit diff
path: root/src/librustc_data_structures/veccell
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2016-01-05 13:02:57 -0500
committerNiko Matsakis <niko@alum.mit.edu>2016-01-05 21:05:50 -0500
commitc77cd480cf2105afcbb92de4f514f1f9637912c5 (patch)
treee20124bc8fe86c9183716ac5fe0c7d47228c5efe /src/librustc_data_structures/veccell
parentbadc23b6ad47c6b6d401a3ea1dc5163bdcd86cd7 (diff)
downloadrust-c77cd480cf2105afcbb92de4f514f1f9637912c5.tar.gz
rust-c77cd480cf2105afcbb92de4f514f1f9637912c5.zip
Introduce the DepGraph and DepTracking map abstractions,
along with a README explaining how they are to be used
Diffstat (limited to 'src/librustc_data_structures/veccell')
-rw-r--r--src/librustc_data_structures/veccell/mod.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/librustc_data_structures/veccell/mod.rs b/src/librustc_data_structures/veccell/mod.rs
new file mode 100644
index 00000000000..1842d0a4958
--- /dev/null
+++ b/src/librustc_data_structures/veccell/mod.rs
@@ -0,0 +1,37 @@
+use std::cell::UnsafeCell;
+use std::mem;
+
+pub struct VecCell<T> {
+    data: UnsafeCell<Vec<T>>
+}
+
+impl<T> VecCell<T> {
+    pub fn with_capacity(capacity: usize) -> VecCell<T>{
+        VecCell { data: UnsafeCell::new(Vec::with_capacity(capacity)) }
+    }
+
+    #[inline]
+    pub fn push(&self, data: T) -> usize {
+        // The logic here, and in `swap` below, is that the `push`
+        // method on the vector will not recursively access this
+        // `VecCell`. Therefore, we can temporarily obtain mutable
+        // access, secure in the knowledge that even if aliases exist
+        // -- indeed, even if aliases are reachable from within the
+        // vector -- they will not be used for the duration of this
+        // particular fn call. (Note that we also are relying on the
+        // fact that `VecCell` is not `Sync`.)
+        unsafe {
+            let v = self.data.get();
+            (*v).push(data);
+            (*v).len()
+        }
+    }
+
+    pub fn swap(&self, mut data: Vec<T>) -> Vec<T> {
+        unsafe {
+            let v = self.data.get();
+            mem::swap(&mut *v, &mut data);
+        }
+        data
+    }
+}