nodrop: Add no_drop_flag optional feature

This commit is contained in:
root
2015-05-26 17:05:42 +02:00
parent 07e497a2be
commit 09cf5168f9
3 changed files with 40 additions and 14 deletions
+9 -6
View File
@@ -4,11 +4,14 @@ matrix:
include:
- rust: stable
- rust: nightly
- rust: nightly
env:
- NODROP_FEATURES='no_drop_flag'
script:
- |
cargo build --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 --manifest-path=nodrop/Cargo.toml -- --test
cargo doc --verbose --features "$FEATURES"
[ -z "$NODROP_FEATURES" ] && cargo build --verbose --features "$FEATURES"
[ -z "$NODROP_FEATURES" ] && cargo test --verbose --features "$FEATURES"
[ -z "$NODROP_FEATURES" ] && cargo bench --verbose --features "$FEATURES" -- --test
[ -z "$NODROP_FEATURES" ] && cargo doc --verbose --features "$FEATURES"
cargo test --verbose --manifest-path=nodrop/Cargo.toml --features "$NODROP_FEATURES"
cargo bench --verbose --manifest-path=nodrop/Cargo.toml --features "$NODROP_FEATURES" -- --test
+6
View File
@@ -10,3 +10,9 @@ documentation = "http://bluss.github.io/arrayvec/doc/nodrop"
repository = "https://github.com/bluss/arrayvec"
keywords = ["container", "drop"]
[features]
# Optional, nightly channel
# Use no drop flag. See API doc for more info.
no_drop_flag = []
+25 -8
View File
@@ -1,3 +1,17 @@
//!
//! The **nodrop** crate has the following cargo feature flags:
//!
//! - `no_drop_flag`.
//! - Optional.
//! - Requires nightly channel.
//! - Use no drop flag on the **NoDrop** type,
//! which means less space overhead. Use with care and report a bug if anything
//! changes behavior with this feature.
//!
//!
#![cfg_attr(feature="no_drop_flag", feature(unsafe_no_drop_flag))]
use std::ops::{Deref, DerefMut};
use std::ptr;
use std::mem;
@@ -10,6 +24,7 @@ enum Flag<T> {
}
/// A type holding **T** that will not call its destructor on drop
#[cfg_attr(feature="no_drop_flag", unsafe_no_drop_flag)]
pub struct NoDrop<T>(Flag<T>);
impl<T> NoDrop<T> {
@@ -33,6 +48,16 @@ impl<T> NoDrop<T> {
}
}
impl<T> Drop for NoDrop<T> {
fn drop(&mut self) {
// no drop flag info: writing repeatedly is idempotent
// inhibit drop
unsafe {
ptr::write(&mut self.0, Flag::Dropped);
}
}
}
enum Void { }
/// FIXME: Replace with intrinsic when it's stable
@@ -50,14 +75,6 @@ unsafe fn debug_assert_unreachable() -> ! {
unreachable()
}
impl<T> Drop for NoDrop<T> {
fn drop(&mut self) {
// inhibit drop
unsafe {
ptr::write(&mut self.0, Flag::Dropped);
}
}
}
impl<T> Deref for NoDrop<T> {
type Target = T;