about summary refs log tree commit diff
path: root/src/librustc_data_structures/array_vec.rs
diff options
context:
space:
mode:
authorMarcin Fatyga <marcinf@google.com>2016-11-01 15:26:22 +0100
committerMarcin Fatyga <marcinf@google.com>2016-11-01 15:26:22 +0100
commit655effedf25e2039d283b839429bf2f42b7012a4 (patch)
tree34fd087d891556c70a14b26a90d1bdccd0a7ccb2 /src/librustc_data_structures/array_vec.rs
parent4e2822c5c28bb342e5862ba7cc0b90b865c68be1 (diff)
parentac968c466451cb9aafd9e8598ddb396ed0e6fe31 (diff)
downloadrust-655effedf25e2039d283b839429bf2f42b7012a4.tar.gz
rust-655effedf25e2039d283b839429bf2f42b7012a4.zip
Merge branch 'master' of https://github.com/rust-lang/rust
Conflicts:
	src/libcoretest/lib.rs
Diffstat (limited to 'src/librustc_data_structures/array_vec.rs')
-rw-r--r--src/librustc_data_structures/array_vec.rs106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/librustc_data_structures/array_vec.rs b/src/librustc_data_structures/array_vec.rs
new file mode 100644
index 00000000000..f87426cee59
--- /dev/null
+++ b/src/librustc_data_structures/array_vec.rs
@@ -0,0 +1,106 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! A stack-allocated vector, allowing storage of N elements on the stack.
+//!
+//! Currently, only the N = 8 case is supported (due to Array only being impl-ed for [T; 8]).
+
+use std::marker::Unsize;
+use std::iter::Extend;
+use std::ptr::drop_in_place;
+use std::ops::{Deref, DerefMut};
+use std::slice;
+use std::fmt;
+
+pub unsafe trait Array {
+    type Element;
+    type PartialStorage: Default + Unsize<[ManuallyDrop<Self::Element>]>;
+    const LEN: usize;
+}
+
+unsafe impl<T> Array for [T; 8] {
+    type Element = T;
+    type PartialStorage = [ManuallyDrop<T>; 8];
+    const LEN: usize = 8;
+}
+
+pub struct ArrayVec<A: Array> {
+    count: usize,
+    values: A::PartialStorage
+}
+
+impl<A: Array> ArrayVec<A> {
+    pub fn new() -> Self {
+        ArrayVec {
+            count: 0,
+            values: Default::default(),
+        }
+    }
+}
+
+impl<A> fmt::Debug for ArrayVec<A>
+    where A: Array,
+          A::Element: fmt::Debug {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        self[..].fmt(f)
+    }
+}
+
+impl<A: Array> Deref for ArrayVec<A> {
+    type Target = [A::Element];
+    fn deref(&self) -> &Self::Target {
+        unsafe {
+            slice::from_raw_parts(&self.values as *const _ as *const A::Element, self.count)
+        }
+    }
+}
+
+impl<A: Array> DerefMut for ArrayVec<A> {
+    fn deref_mut(&mut self) -> &mut [A::Element] {
+        unsafe {
+            slice::from_raw_parts_mut(&mut self.values as *mut _ as *mut A::Element, self.count)
+        }
+    }
+}
+
+impl<A: Array> Drop for ArrayVec<A> {
+    fn drop(&mut self) {
+        unsafe {
+            drop_in_place(&mut self[..])
+        }
+    }
+}
+
+impl<A: Array> Extend<A::Element> for ArrayVec<A> {
+    fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item=A::Element> {
+        for el in iter {
+            unsafe {
+                let arr = &mut self.values as &mut [ManuallyDrop<_>];
+                arr[self.count].value = el;
+            }
+            self.count += 1;
+        }
+    }
+}
+
+// FIXME: This should use repr(transparent) from rust-lang/rfcs#1758.
+#[allow(unions_with_drop_fields)]
+pub union ManuallyDrop<T> {
+    value: T,
+    #[allow(dead_code)]
+    empty: (),
+}
+
+impl<T> Default for ManuallyDrop<T> {
+    fn default() -> Self {
+        ManuallyDrop { empty: () }
+    }
+}
+