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
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
const VEC_SIZE: usize = 16;
const SPILLED_SIZE: usize = 100;

trait Vector<T>: for<'a> From<&'a [T]> + Extend<T> + FromIterator<T> {
trait Vector<T>: for<'valid> From<&'valid [T]> + Extend<T> + FromIterator<T> {
fn new() -> Self;
fn push(&mut self, val: T);
fn pop(&mut self) -> Option<T>;
Expand Down
10 changes: 5 additions & 5 deletions fuzz/fuzz_targets/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ const MAX_CAP: usize = 1024;
pub struct Bounded<T, const CAP: usize = 255>(pub T);

// Bounded `usize` between `0..=CAP`
impl<'a, const CAP: usize> Arbitrary<'a> for Bounded<usize, CAP> {
impl<'valid, const CAP: usize> Arbitrary<'valid> for Bounded<usize, CAP> {
#[inline]
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
fn arbitrary(u: &mut arbitrary::Unstructured<'valid>) -> arbitrary::Result<Self> {
Ok(Bounded(u.int_in_range(0..=CAP)?))
}
}

// Bounded `Vec<T>` whose length is between `0..=CAP`
impl<'a, T, const CAP: usize> Arbitrary<'a> for Bounded<Vec<T>, CAP>
where T: Arbitrary<'a>
impl<'valid, T, const CAP: usize> Arbitrary<'valid> for Bounded<Vec<T>, CAP>
where T: Arbitrary<'valid>
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
fn arbitrary(u: &mut arbitrary::Unstructured<'valid>) -> arbitrary::Result<Self> {
let len = u.int_in_range(0..=CAP)?;
let vec = u
.arbitrary_iter()?
Expand Down
24 changes: 13 additions & 11 deletions src/iterators/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
/// Returned from [`SmallVec::drain`][1].
///
/// [1]: struct.SmallVec.html#method.drain
pub struct Drain<'a, Item: 'a, const INLINE: usize, Heap: Allocator = Global> {
pub struct Drain<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator = Global> {
// `vec` points to a valid object within its lifetime.
// This is ensured by the fact that we're holding an iterator to its items.
//
Expand All @@ -21,12 +21,12 @@ pub struct Drain<'a, Item: 'a, const INLINE: usize, Heap: Allocator = Global> {
// even though vec has length < tail_start
pub(crate) tail_start: usize,
pub(crate) tail_len: usize,
pub(crate) iter: core::slice::Iter<'a, Item>,
pub(crate) iter: core::slice::Iter<'valid, Item>,
pub(crate) vec: core::ptr::NonNull<SmallVec<Item, INLINE, Heap>>
}

impl<'a, Item: 'a, const INLINE: usize, Heap: Allocator> Iterator
for Drain<'a, Item, INLINE, Heap>
impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Iterator
for Drain<'valid, Item, INLINE, Heap>
{
type Item = Item;

Expand All @@ -45,8 +45,8 @@ impl<'a, Item: 'a, const INLINE: usize, Heap: Allocator> Iterator
}
}

impl<'a, Item: 'a, const INLINE: usize, Heap: Allocator> DoubleEndedIterator
for Drain<'a, Item, INLINE, Heap>
impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> DoubleEndedIterator
for Drain<'valid, Item, INLINE, Heap>
{
#[inline]
fn next_back(&mut self) -> Option<Item> {
Expand All @@ -71,15 +71,17 @@ impl<Item, const INLINE: usize, Heap: Allocator> core::iter::FusedIterator
{
}

impl<'a, Item: 'a, const INLINE: usize, Heap: Allocator> Drop for Drain<'a, Item, INLINE, Heap> {
impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator> Drop
for Drain<'valid, Item, INLINE, Heap>
{
fn drop(&mut self) {
/// Moves back the un-`Drain`ed elements to restore the original `Vec`.
struct DropGuard<'r, 'a, Item, const INLINE: usize, Heap: Allocator>(
&'r mut Drain<'a, Item, INLINE, Heap>
struct DropGuard<'guard, 'valid, Item, const INLINE: usize, Heap: Allocator>(
&'guard mut Drain<'valid, Item, INLINE, Heap>
);

impl<'r, 'a, Item, const INLINE: usize, Heap: Allocator> Drop
for DropGuard<'r, 'a, Item, INLINE, Heap>
impl<'guard, 'valid, Item, const INLINE: usize, Heap: Allocator> Drop
for DropGuard<'guard, 'valid, Item, INLINE, Heap>
{
fn drop(&mut self) {
if self.0.tail_len > 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/iterators/extractif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::{
/// Returned from [`SmallVec::extract_if`][1].
///
/// [1]: struct.SmallVec.html#method.extract_if
pub struct ExtractIf<'a, Item, const INLINE: usize, F, Heap: Allocator = Global>
pub struct ExtractIf<'valid, Item, const INLINE: usize, F, Heap: Allocator = Global>
where F: FnMut(&mut Item) -> bool
{
pub(crate) vec: &'a mut SmallVec<Item, INLINE, Heap>,
pub(crate) vec: &'valid mut SmallVec<Item, INLINE, Heap>,
/// The index of the item that will be inspected by the next call to `next`.
pub(crate) idx: usize,
/// Elements at and beyond this point will be retained. Must be equal or
Expand Down
10 changes: 5 additions & 5 deletions src/iterators/rayon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use {
}
};

struct SliceDrain<'a, Item>(slice::IterMut<'a, Item>);
struct SliceDrain<'valid, Item>(slice::IterMut<'valid, Item>);

impl<Item> Iterator for SliceDrain<'_, Item> {
type Item = Item;
Expand Down Expand Up @@ -52,13 +52,13 @@ impl<Item> Drop for SliceDrain<'_, Item> {
}
}

struct DrainProducer<'a, Item>(&'a mut [Item]);
struct DrainProducer<'valid, Item>(&'valid mut [Item]);

impl<'a, Item: Send> Producer for DrainProducer<'a, Item> {
type IntoIter = SliceDrain<'a, Item>;
impl<'valid, Item: Send> Producer for DrainProducer<'valid, Item> {
type IntoIter = SliceDrain<'valid, Item>;
type Item = Item;

fn into_iter(mut self) -> SliceDrain<'a, Item> {
fn into_iter(mut self) -> SliceDrain<'valid, Item> {
SliceDrain(take(&mut self.0).iter_mut())
}

Expand Down
15 changes: 9 additions & 6 deletions src/iterators/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ use super::{
drain::Drain
};

pub struct Splice<'a, I: Iterator + 'a, const INLINE: usize, Heap: Allocator = Global> {
drain: Drain<'a, I::Item, INLINE, Heap>,
pub struct Splice<'valid, I: Iterator + 'valid, const INLINE: usize, Heap: Allocator = Global> {
drain: Drain<'valid, I::Item, INLINE, Heap>,
replace_with: I
}

impl<'a, I: Iterator + 'a, const INLINE: usize, Heap: Allocator> Splice<'a, I, INLINE, Heap> {
pub(crate) fn new(drain: Drain<'a, I::Item, INLINE, Heap>, replace_with: I) -> Self {
impl<'valid, I: Iterator + 'valid, const INLINE: usize, Heap: Allocator>
Splice<'valid, I, INLINE, Heap>
{
pub(crate) fn new(drain: Drain<'valid, I::Item, INLINE, Heap>, replace_with: I) -> Self {
Self {
drain,
replace_with
}
}
}

impl<'a, I, const INLINE: usize, Heap: Allocator> core::fmt::Debug for Splice<'a, I, INLINE, Heap>
impl<'valid, I, const INLINE: usize, Heap: Allocator> core::fmt::Debug
for Splice<'valid, I, INLINE, Heap>
where
I: core::fmt::Debug + Iterator + 'a,
I: core::fmt::Debug + Iterator + 'valid,
<I as Iterator>::Item: core::fmt::Debug
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down
45 changes: 23 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<Item, const INLINE: usize> SmallVec<Item, INLINE, Global> {
/// // Overwrite memory with [4, 5, 6].
/// //
/// // This is only safe if `spilled` is true! Otherwise, we are
/// // writing into the old `SmallVec`'s inline storage on the
/// // writing into the old `SmallVec`'valid inline storage on the
/// // stack.
/// assert!(spilled);
/// for i in 0..length {
Expand Down Expand Up @@ -1057,8 +1057,8 @@ impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
// panicked. It shifts unchecked elements to cover holes and
// `set_len` to the correct length. In cases when predicate and
// `drop` never panic, it will be optimized out.
struct Guard<'a, Item, const INLINE: usize, Heap: Allocator> {
v: &'a mut SmallVec<Item, INLINE, Heap>,
struct Guard<'valid, Item, const INLINE: usize, Heap: Allocator> {
v: &'valid mut SmallVec<Item, INLINE, Heap>,
read: usize,
write: usize,
original_len: usize
Expand Down Expand Up @@ -1193,7 +1193,7 @@ impl<Item, const INLINE: usize, Heap: Allocator> SmallVec<Item, INLINE, Heap> {
}
}

pub fn leak<'a>(self) -> &'a mut [Item] {
pub fn leak<'valid>(self) -> &'valid mut [Item] {
let (length, on_heap) = self.length.parts();
if !on_heap {
panic!(
Expand Down Expand Up @@ -1473,11 +1473,11 @@ impl<Item> Drop for DropGuard<Item> {
}
}

struct DropDealloc<'a, Heap: Allocator> {
struct DropDealloc<'valid, Heap: Allocator> {
ptr: NonNull<u8>,
size_bytes: usize,
align: usize,
allocator: &'a Heap
allocator: &'valid Heap
}

impl<Heap: Allocator> Drop for DropDealloc<'_, Heap> {
Expand Down Expand Up @@ -1761,14 +1761,14 @@ impl<Item, const INLINE: usize, Heap: Allocator> Extend<Item> for SmallVec<Item,
}
}

impl<'a, Item: Clone + 'a, const INLINE: usize, Heap: Allocator> Extend<&'a Item>
impl<'valid, Item: Clone + 'valid, const INLINE: usize, Heap: Allocator> Extend<&'valid Item>
for SmallVec<Item, INLINE, Heap>
{
#[inline]
fn extend<I: IntoIterator<Item = &'a Item>>(&mut self, iter: I) {
fn extend<I: IntoIterator<Item = &'valid Item>>(&mut self, iter: I) {
#[cfg(feature = "specialization")]
{
specialization::SpecExtend::<&'a Item, _>::spec_extend(self, iter.into_iter());
specialization::SpecExtend::<&'valid Item, _>::spec_extend(self, iter.into_iter());
}

#[cfg(not(feature = "specialization"))]
Expand All @@ -1793,22 +1793,22 @@ impl<Item, const INLINE: usize> core::iter::FromIterator<Item> for SmallVec<Item
}
}

impl<'a, Item, const INLINE: usize, Heap: Allocator> IntoIterator
for &'a SmallVec<Item, INLINE, Heap>
impl<'valid, Item, const INLINE: usize, Heap: Allocator> IntoIterator
for &'valid SmallVec<Item, INLINE, Heap>
{
type IntoIter = core::slice::Iter<'a, Item>;
type Item = &'a Item;
type IntoIter = core::slice::Iter<'valid, Item>;
type Item = &'valid Item;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, Item, const INLINE: usize, Heap: Allocator> IntoIterator
for &'a mut SmallVec<Item, INLINE, Heap>
impl<'valid, Item, const INLINE: usize, Heap: Allocator> IntoIterator
for &'valid mut SmallVec<Item, INLINE, Heap>
{
type IntoIter = core::slice::IterMut<'a, Item>;
type Item = &'a mut Item;
type IntoIter = core::slice::IterMut<'valid, Item>;
type Item = &'valid mut Item;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
Expand Down Expand Up @@ -1836,7 +1836,7 @@ impl<Item: Zeroize, const INLINE: usize, Heap: Allocator> Zeroize for SmallVec<I
self.spare_capacity_mut().zeroize();

// Zeroize the elements in place so nested allocations, such as a
// `String`'s heap buffer, are wiped before they are dropped.
// `String`'valid heap buffer, are wiped before they are dropped.
self.iter_mut().zeroize();

self.clear();
Expand All @@ -1845,14 +1845,15 @@ impl<Item: Zeroize, const INLINE: usize, Heap: Allocator> Zeroize for SmallVec<I

#[cfg(feature = "arbitrary")]
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
impl<'a, Item, const INLINE: usize> arbitrary::Arbitrary<'a> for SmallVec<Item, INLINE, Global>
where Item: arbitrary::Arbitrary<'a>
impl<'valid, Item, const INLINE: usize> arbitrary::Arbitrary<'valid>
for SmallVec<Item, INLINE, Global>
where Item: arbitrary::Arbitrary<'valid>
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
fn arbitrary(u: &mut arbitrary::Unstructured<'valid>) -> arbitrary::Result<Self> {
u.arbitrary_iter()?.collect()
}

fn arbitrary_take_rest(u: arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
fn arbitrary_take_rest(u: arbitrary::Unstructured<'valid>) -> arbitrary::Result<Self> {
u.arbitrary_take_rest_iter()?.collect()
}

Expand Down
10 changes: 5 additions & 5 deletions src/specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ impl<Item, const INLINE: usize, const M: usize, Heap: Allocator>
}
}

impl<'a, Item: 'a, const INLINE: usize, I, Heap: Allocator> SpecExtend<&'a Item, I>
impl<'valid, Item: 'valid, const INLINE: usize, I, Heap: Allocator> SpecExtend<&'valid Item, I>
for SmallVec<Item, INLINE, Heap>
where
I: Iterator<Item = &'a Item>,
I: Iterator<Item = &'valid Item>,
Item: Clone
{
#[inline]
Expand All @@ -148,11 +148,11 @@ where
}
}

impl<'a, Item: 'a, const INLINE: usize, Heap: Allocator>
SpecExtend<&'a Item, core::slice::Iter<'a, Item>> for SmallVec<Item, INLINE, Heap>
impl<'valid, Item: 'valid, const INLINE: usize, Heap: Allocator>
SpecExtend<&'valid Item, core::slice::Iter<'valid, Item>> for SmallVec<Item, INLINE, Heap>
where Item: Copy
{
fn spec_extend(&mut self, iter: core::slice::Iter<'a, Item>) {
fn spec_extend(&mut self, iter: core::slice::Iter<'valid, Item>) {
let slice = iter.as_slice();
let length = slice.len();
let old_len = self.len();
Expand Down
6 changes: 3 additions & 3 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn splice_inline_fill_then_move_tail_ub_test() {

#[test]
fn splice_reserve_panic() {
struct CountDrop<'a>(&'a Cell<usize>);
struct CountDrop<'valid>(&'valid Cell<usize>);

impl Drop for CountDrop<'_> {
fn drop(&mut self) {
Expand Down Expand Up @@ -311,9 +311,9 @@ fn into_iter_rev() {

#[test]
fn into_iter_drop() {
struct DropCounter<'a>(&'a Cell<i32>);
struct DropCounter<'valid>(&'valid Cell<i32>);

impl<'a> Drop for DropCounter<'a> {
impl<'valid> Drop for DropCounter<'valid> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
Expand Down
Loading