Skip to content

Commit e244d0a

Browse files
author
bors-servo
authored
Auto merge of #133 - nnethercote:may_dangle, r=mbrubeck
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 -->
2 parents c1921f4 + 72a500f commit e244d0a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smallvec"
3-
version = "0.6.6"
3+
version = "0.6.7"
44
authors = ["Simon Sapin <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
repository = "https://github.com/servo/rust-smallvec"
@@ -15,6 +15,7 @@ std = []
1515
union = []
1616
default = ["std"]
1717
specialization = []
18+
may_dangle = []
1819

1920
[lib]
2021
name = "smallvec"

lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![cfg_attr(not(feature = "std"), feature(alloc))]
3333
#![cfg_attr(feature = "union", feature(untagged_unions))]
3434
#![cfg_attr(feature = "specialization", feature(specialization))]
35+
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
3536
#![deny(missing_docs)]
3637

3738

@@ -1374,6 +1375,21 @@ impl<A: Array> Default for SmallVec<A> {
13741375
}
13751376
}
13761377

1378+
#[cfg(feature = "may_dangle")]
1379+
unsafe impl<#[may_dangle] A: Array> Drop for SmallVec<A> {
1380+
fn drop(&mut self) {
1381+
unsafe {
1382+
if self.spilled() {
1383+
let (ptr, len) = self.data.heap();
1384+
Vec::from_raw_parts(ptr, len, self.capacity);
1385+
} else {
1386+
ptr::drop_in_place(&mut self[..]);
1387+
}
1388+
}
1389+
}
1390+
}
1391+
1392+
#[cfg(not(feature = "may_dangle"))]
13771393
impl<A: Array> Drop for SmallVec<A> {
13781394
fn drop(&mut self) {
13791395
unsafe {

0 commit comments

Comments
 (0)