Skip to content

Commit

Permalink
Add a new feature, may_dangle.
Browse files Browse the repository at this point in the history
This commit adds a `may_dangle` annotation to `drop`, matching `Vec`.
This feature increases the ability of `SmallVec` to be used as a drop-in
replacement for `Vec`. A feature is necessary because `may_dangle` is
Nightly-only.

Fixes #132.
  • Loading branch information
nnethercote committed Nov 29, 2018
1 parent c1921f4 commit 2fa60c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ std = []
union = []
default = ["std"]
specialization = []
may_dangle = []

[lib]
name = "smallvec"
Expand Down
16 changes: 16 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#![cfg_attr(not(feature = "std"), feature(alloc))]
#![cfg_attr(feature = "union", feature(untagged_unions))]
#![cfg_attr(feature = "specialization", feature(specialization))]
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
#![deny(missing_docs)]


Expand Down Expand Up @@ -1374,6 +1375,21 @@ impl<A: Array> Default for SmallVec<A> {
}
}

#[cfg(feature = "may_dangle")]
unsafe impl<#[may_dangle] A: Array> Drop for SmallVec<A> {
fn drop(&mut self) {
unsafe {
if self.spilled() {
let (ptr, len) = self.data.heap();
Vec::from_raw_parts(ptr, len, self.capacity);
} else {
ptr::drop_in_place(&mut self[..]);
}
}
}
}

#[cfg(not(feature = "may_dangle"))]
impl<A: Array> Drop for SmallVec<A> {
fn drop(&mut self) {
unsafe {
Expand Down

0 comments on commit 2fa60c5

Please sign in to comment.