diff --git a/Cargo.toml b/Cargo.toml index 3496e76..b599741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ harness = false [features] default = ["std"] std = [] +may_dangle = [] [profile.bench] debug = true diff --git a/src/arrayvec.rs b/src/arrayvec.rs index f646b08..87f8ff6 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -46,6 +46,16 @@ pub struct ArrayVec { xs: [MaybeUninit; CAP], } +#[cfg(feature = "may_dangle")] +unsafe impl<#[may_dangle] T, const CAP: usize> Drop for ArrayVec { + fn drop(&mut self) { + self.clear(); + + // MaybeUninit inhibits array's drop + } +} + +#[cfg(not(feature = "may_dangle"))] impl Drop for ArrayVec { fn drop(&mut self) { self.clear(); diff --git a/src/lib.rs b/src/lib.rs index 5c4bcee..6fc0f24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,12 @@ //! - `zeroize` //! - Optional //! - Implement `Zeroize` for ArrayVec and ArrayString +//! +//! - `may_dangle` +//! - Optional and unstable +//! - Requires nightly toolchain +//! - Makes compiler less strict about storing references with a lifetime in ArrayVec, +//! see [Rustonomicon](https://doc.rust-lang.org/1.42.0/nomicon/dropck.html#an-escape-hatch). //! //! ## Rust Version //! @@ -21,6 +27,7 @@ //! #![doc(html_root_url="https://docs.rs/arrayvec/0.7/")] #![cfg_attr(not(feature="std"), no_std)] +#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))] #[cfg(feature="serde")] extern crate serde;