Skip to content

Commit

Permalink
Auto merge of #133 - nnethercote:may_dangle, r=mbrubeck
Browse files Browse the repository at this point in the history
Add a new feature, `may_dangle`.

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.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/133)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Nov 29, 2018
2 parents c1921f4 + 72a500f commit e244d0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smallvec"
version = "0.6.6"
version = "0.6.7"
authors = ["Simon Sapin <[email protected]>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/servo/rust-smallvec"
Expand All @@ -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 e244d0a

Please sign in to comment.