nodrop: Add no_drop_flag optional feature
This commit is contained in:
+9
-6
@@ -4,11 +4,14 @@ matrix:
|
|||||||
include:
|
include:
|
||||||
- rust: stable
|
- rust: stable
|
||||||
- rust: nightly
|
- rust: nightly
|
||||||
|
- rust: nightly
|
||||||
|
env:
|
||||||
|
- NODROP_FEATURES='no_drop_flag'
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
cargo build --verbose --features "$FEATURES"
|
[ -z "$NODROP_FEATURES" ] && cargo build --verbose --features "$FEATURES"
|
||||||
cargo test --verbose --features "$FEATURES"
|
[ -z "$NODROP_FEATURES" ] && cargo test --verbose --features "$FEATURES"
|
||||||
cargo test --verbose --manifest-path=nodrop/Cargo.toml
|
[ -z "$NODROP_FEATURES" ] && cargo bench --verbose --features "$FEATURES" -- --test
|
||||||
cargo bench --verbose --features "$FEATURES" -- --test
|
[ -z "$NODROP_FEATURES" ] && cargo doc --verbose --features "$FEATURES"
|
||||||
cargo bench --verbose --manifest-path=nodrop/Cargo.toml -- --test
|
cargo test --verbose --manifest-path=nodrop/Cargo.toml --features "$NODROP_FEATURES"
|
||||||
cargo doc --verbose --features "$FEATURES"
|
cargo bench --verbose --manifest-path=nodrop/Cargo.toml --features "$NODROP_FEATURES" -- --test
|
||||||
|
|||||||
@@ -10,3 +10,9 @@ documentation = "http://bluss.github.io/arrayvec/doc/nodrop"
|
|||||||
repository = "https://github.com/bluss/arrayvec"
|
repository = "https://github.com/bluss/arrayvec"
|
||||||
|
|
||||||
keywords = ["container", "drop"]
|
keywords = ["container", "drop"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
|
||||||
|
# Optional, nightly channel
|
||||||
|
# Use no drop flag. See API doc for more info.
|
||||||
|
no_drop_flag = []
|
||||||
|
|||||||
+25
-8
@@ -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::ops::{Deref, DerefMut};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
@@ -10,6 +24,7 @@ enum Flag<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A type holding **T** that will not call its destructor on drop
|
/// 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>);
|
pub struct NoDrop<T>(Flag<T>);
|
||||||
|
|
||||||
impl<T> NoDrop<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 { }
|
enum Void { }
|
||||||
|
|
||||||
/// FIXME: Replace with intrinsic when it's stable
|
/// FIXME: Replace with intrinsic when it's stable
|
||||||
@@ -50,14 +75,6 @@ unsafe fn debug_assert_unreachable() -> ! {
|
|||||||
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> {
|
impl<T> Deref for NoDrop<T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|||||||
Reference in New Issue
Block a user