Use crate nodrop -- it moved to a separate crate

This commit is contained in:
root
2015-05-21 11:54:35 +02:00
parent c146002c83
commit 8047dc621c
4 changed files with 23 additions and 47 deletions
+2
View File
@@ -8,5 +8,7 @@ script:
- | - |
cargo build --verbose --features "$FEATURES" cargo build --verbose --features "$FEATURES"
cargo test --verbose --features "$FEATURES" cargo test --verbose --features "$FEATURES"
cargo test --verbose --manifest-path=nodrop/Cargo.toml
cargo bench --verbose --features "$FEATURES" -- --test cargo bench --verbose --features "$FEATURES" -- --test
cargo bench --verbose --manifest-path=nodrop/Cargo.toml -- --test
cargo doc --verbose --features "$FEATURES" cargo doc --verbose --features "$FEATURES"
+4
View File
@@ -9,3 +9,7 @@ documentation = "http://bluss.github.io/arrayvec"
repository = "https://github.com/bluss/arrayvec" repository = "https://github.com/bluss/arrayvec"
keywords = ["stack", "vector", "array", "container", "data-structure"] keywords = ["stack", "vector", "array", "container", "data-structure"]
[dependencies]
nodrop = "0.1"
+1 -1
View File
@@ -1,6 +1,6 @@
docs: VERSION docs: VERSION
cargo doc --no-deps cargo doc
rm -rf ./doc rm -rf ./doc
cp -r ./target/doc ./doc cp -r ./target/doc ./doc
# Put in the crate version into the docs # Put in the crate version into the docs
+16 -46
View File
@@ -1,3 +1,7 @@
extern crate nodrop;
use nodrop::NoDrop;
use std::iter; use std::iter;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
@@ -12,13 +16,6 @@ use std::borrow::{Borrow, BorrowMut};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::fmt; use std::fmt;
/// Make sure the non-nullable pointer optimization does not occur!
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
/// Trait for fixed size arrays. /// Trait for fixed size arrays.
pub unsafe trait Array { pub unsafe trait Array {
/// The array's element type /// The array's element type
@@ -77,17 +74,14 @@ fix_array_impl_recursive!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
/// ///
/// The vector also implements a by value iterator. /// The vector also implements a by value iterator.
pub struct ArrayVec<A: Array> { pub struct ArrayVec<A: Array> {
xs: Flag<A>, xs: NoDrop<A>,
len: u8, len: u8,
} }
impl<A: Array> Drop for ArrayVec<A> { impl<A: Array> Drop for ArrayVec<A> {
fn drop(&mut self) { fn drop(&mut self) {
// clear all elements, then inhibit drop of inner array // clear all elements, then NoDrop inhibits drop of inner array
while let Some(_) = self.pop() { } while let Some(_) = self.pop() { }
unsafe {
ptr::write(&mut self.xs, Flag::Dropped);
}
} }
} }
@@ -109,26 +103,7 @@ impl<A: Array> ArrayVec<A> {
/// ``` /// ```
pub fn new() -> ArrayVec<A> { pub fn new() -> ArrayVec<A> {
unsafe { unsafe {
ArrayVec { xs: Flag::Alive(Array::new()), len: 0 } ArrayVec { xs: NoDrop::new(Array::new()), len: 0 }
}
}
#[inline]
fn inner_ref(&self) -> &A {
match self.xs {
Flag::Alive(ref xs) => xs,
_ => unreachable!(),
//_ => std::intrinsics::unreachable(),
}
}
#[inline]
fn inner_mut(&mut self) -> &mut A {
// FIXME: Optimize this, we know it's always Some.
match self.xs {
Flag::Alive(ref mut xs) => xs,
_ => unreachable!(),
//_ => std::intrinsics::unreachable(),
} }
} }
@@ -221,7 +196,7 @@ impl<A: Array> Deref for ArrayVec<A> {
#[inline] #[inline]
fn deref(&self) -> &[A::Item] { fn deref(&self) -> &[A::Item] {
unsafe { unsafe {
slice::from_raw_parts(self.inner_ref().as_ptr(), self.len()) slice::from_raw_parts(self.xs.as_ptr(), self.len())
} }
} }
} }
@@ -231,7 +206,7 @@ impl<A: Array> DerefMut for ArrayVec<A> {
fn deref_mut(&mut self) -> &mut [A::Item] { fn deref_mut(&mut self) -> &mut [A::Item] {
let len = self.len(); let len = self.len();
unsafe { unsafe {
slice::from_raw_parts_mut(self.inner_mut().as_mut_ptr(), len) slice::from_raw_parts_mut(self.xs.as_mut_ptr(), len)
} }
} }
} }
@@ -248,7 +223,7 @@ impl<A: Array> DerefMut for ArrayVec<A> {
/// ``` /// ```
impl<A: Array> From<A> for ArrayVec<A> { impl<A: Array> From<A> for ArrayVec<A> {
fn from(array: A) -> Self { fn from(array: A) -> Self {
ArrayVec { xs: Flag::Alive(array), len: A::capacity() as u8 } ArrayVec { xs: NoDrop::new(array), len: A::capacity() as u8 }
} }
} }
@@ -538,21 +513,16 @@ fn test_is_send_sync() {
&data as &Sync; &data as &Sync;
} }
#[test]
fn test_no_nonnullable_opt() {
// Make sure `Flag` does not apply the non-nullable pointer optimization
// as Option would do.
assert!(mem::size_of::<Flag<&i32>>() > mem::size_of::<&i32>());
assert!(mem::size_of::<Flag<Vec<i32>>>() > mem::size_of::<Vec<i32>>());
}
#[test] #[test]
fn test_compact_size() { fn test_compact_size() {
// 4 elements size + 1 len + 1 enum tag + [1 drop flag] // Future rust will kill these drop flags!
// 4 elements size + 1 len + 1 enum tag + [1 drop flag] + [1 drop flag nodrop]
type ByteArray = ArrayVec<[u8; 4]>; type ByteArray = ArrayVec<[u8; 4]>;
assert!(mem::size_of::<ByteArray>() <= 7); println!("{}", mem::size_of::<ByteArray>());
assert!(mem::size_of::<ByteArray>() <= 8);
// 12 element size + 1 len + 1 drop flag + 2 padding + 1 enum tag + 3 padding // 12 element size + 1 len + 1 drop flag + 2 padding + 1 enum tag + 3 padding
type QuadArray = ArrayVec<[u32; 3]>; type QuadArray = ArrayVec<[u32; 3]>;
assert!(mem::size_of::<QuadArray>() <= 20); println!("{}", mem::size_of::<QuadArray>());
assert!(mem::size_of::<QuadArray>() <= 24);
} }