Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ strip = false
allocator-api = []
allocator-api2 = ["allocator-api", "dep:allocator-api2"]
internals = []
may-dangle = []
proptest = ["dep:proptest", "std"]
rayon = ["dep:rayon", "std"]
serde = ["dep:serde_core"]
Expand Down Expand Up @@ -80,6 +81,10 @@ required-features = ["encase"]
name = "equivalent"
required-features = ["equivalent"]

[[test]]
name = "maydangle"
required-features = ["may-dangle"]

[[test]]
name = "rayon"
required-features = ["rayon"]
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(feature = "specialization", allow(incomplete_features))]
#![cfg_attr(feature = "specialization", feature(specialization, trusted_len))]
#![cfg_attr(feature = "may-dangle", feature(dropck_eyepatch))]
#![cfg_attr(
all(feature = "allocator-api", not(feature = "allocator-api2")),
feature(allocator_api)
Expand Down Expand Up @@ -453,8 +454,8 @@ impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
tail_start: end,
tail_len: length - end,
iter: range_slice.iter(),
// Since self is a &mut, passing it to a function would invalidate the slice
// iterator.
// Since self is a &mut, passing it to a function would
// invalidate the slice iterator.
vec: NonNull::new_unchecked(self as *mut _)
}
}
Expand Down Expand Up @@ -1501,7 +1502,10 @@ impl<Heap: Allocator> Drop for DropDealloc<'_, Heap> {
}
}

impl<Item, const INLINE: usize, Heap: Allocator> Drop for SmallVec<Item, INLINE, Heap> {
#[cfg_attr(feature = "may-dangle", syntax(unsafe))]
impl<#[cfg_attr(feature = "may-dangle", may_dangle)] Item, const INLINE: usize, Heap: Allocator>
Drop for SmallVec<Item, INLINE, Heap>
{
fn drop(&mut self) {
let (length, on_heap) = self.length.parts();
// SAFETY: the tag tells which member is active
Expand Down
8 changes: 4 additions & 4 deletions src/specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ where I: TrustedLen<Item = Item>
fn spec_from_iter(iter: I) -> Self {
let mut v = match iter.size_hint() {
(_, Some(upper)) => SmallVec::with_capacity(upper),
// TrustedLen contract guarantees that `size_hint() == (_, None)` means that there
// are more than `usize::MAX` elements.
// Since the previous branch would eagerly panic if the capacity is too large
// (via `with_capacity`) we do the same here.
// TrustedLen contract guarantees that `size_hint() == (_, None)`
// means that there are more than `usize::MAX` elements.
// Since the previous branch would eagerly panic if the capacity is
// too large (via `with_capacity`) we do the same here.
_ => panic!("capacity overflow")
};
// Reuse the extend specialization for TrustedLen.
Expand Down
13 changes: 13 additions & 0 deletions tests/maydangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use smallvec::SmallVec;

#[derive(Default)]
struct MyData<'a> {
arr: SmallVec<&'a u8, 5>,
dat: u8
}

#[test]
fn may_dangle_allows_borrow_of_another_field() {
let mut dat = MyData::default();
dat.arr.push(&dat.dat);
}
Loading