Skip to content

Commit 2fa60c5

Browse files
committed
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.
1 parent c1921f4 commit 2fa60c5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ std = []
1515
union = []
1616
default = ["std"]
1717
specialization = []
18+
may_dangle = []
1819

1920
[lib]
2021
name = "smallvec"

lib.rs

Lines changed: 16 additions & 0 deletions
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)