nodrop: Add feature flag use_needs_drop; Tag version 0.1.5

This commit is contained in:
bluss
2015-12-14 04:21:44 +01:00
parent 1921048acd
commit 2d55a2d507
4 changed files with 43 additions and 8 deletions
+25 -4
View File
@@ -1,6 +1,10 @@
//!
//! The **nodrop** crate has the following cargo feature flags:
//!
//! - `use_needs_drop`
//! - Optional
//! - Requires nightly channel.
//! - Use `needs_drop` to skip overwriting if not necessary
//! - `no_drop_flag`.
//! - Optional.
//! - Requires nightly channel.
@@ -11,6 +15,7 @@
//!
#![cfg_attr(feature="no_drop_flag", feature(unsafe_no_drop_flag))]
#![cfg_attr(feature="use_needs_drop", feature(core_intrinsics))]
extern crate odds;
@@ -52,12 +57,28 @@ impl<T> NoDrop<T> {
}
}
#[cfg(not(feature = "use_needs_drop"))]
#[inline]
fn needs_drop<T>() -> bool {
true
}
#[cfg(feature = "use_needs_drop")]
#[inline]
fn needs_drop<T>() -> bool {
unsafe {
std::intrinsics::needs_drop::<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);
if needs_drop::<T>() {
// no drop flag info: writing repeatedly is idempotent
// inhibit drop
unsafe {
ptr::write(&mut self.0, Flag::Dropped);
}
}
}
}