From c77cd480cf2105afcbb92de4f514f1f9637912c5 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 5 Jan 2016 13:02:57 -0500 Subject: Introduce the DepGraph and DepTracking map abstractions, along with a README explaining how they are to be used --- src/librustc_data_structures/veccell/mod.rs | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/librustc_data_structures/veccell/mod.rs (limited to 'src/librustc_data_structures/veccell') 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 { + data: UnsafeCell> +} + +impl VecCell { + pub fn with_capacity(capacity: usize) -> VecCell{ + 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) -> Vec { + unsafe { + let v = self.data.get(); + mem::swap(&mut *v, &mut data); + } + data + } +} -- cgit 1.4.1-3-g733a5