Skip to content
Open
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
16 changes: 9 additions & 7 deletions library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::ptr::NonNull;
use core::{fmt, ptr};

use super::VecDeque;
use super::index::WrappedIndex;
use crate::alloc::{Allocator, Global};

/// A draining iterator over the elements of a `VecDeque`.
Expand Down Expand Up @@ -203,11 +204,11 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
let (src, dst, len);
if head_len < tail_len {
src = source_deque.head;
dst = source_deque.to_physical_idx(drain_len);
dst = source_deque.to_wrapped_index(drain_len);
len = head_len;
} else {
src = source_deque.to_physical_idx(head_len + drain_len);
dst = source_deque.to_physical_idx(head_len);
src = source_deque.to_wrapped_index(head_len + drain_len);
dst = source_deque.to_wrapped_index(head_len);
len = tail_len;
};

Expand All @@ -220,10 +221,10 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
if new_len == 0 {
// Special case: If the entire deque was drained, reset the head back to 0,
// like `.clear()` does.
source_deque.head = 0;
source_deque.head = WrappedIndex::zero();
} else if head_len < tail_len {
// If we moved the head above, then we need to adjust the head index here.
source_deque.head = source_deque.to_physical_idx(drain_len);
source_deque.head = source_deque.to_wrapped_index(drain_len);
}
source_deque.len = new_len;
}
Expand All @@ -240,7 +241,7 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
if self.remaining == 0 {
return None;
}
let wrapped_idx = unsafe { self.deque.as_ref().to_physical_idx(self.idx) };
let wrapped_idx = unsafe { self.deque.as_ref().to_wrapped_index(self.idx) };
self.idx += 1;
self.remaining -= 1;
Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) })
Expand All @@ -261,7 +262,8 @@ impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
return None;
}
self.remaining -= 1;
let wrapped_idx = unsafe { self.deque.as_ref().to_physical_idx(self.idx + self.remaining) };
let wrapped_idx =
unsafe { self.deque.as_ref().to_wrapped_index(self.idx + self.remaining) };
Some(unsafe { self.deque.as_mut().buffer_read(wrapped_idx) })
}
}
Expand Down
14 changes: 7 additions & 7 deletions library/alloc/src/collections/vec_deque/extract_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ where
//
// Note: we can't use `vec.get_mut(i).unwrap()` here since the precondition for that
// function is that i < vec.len, but we've set vec's length to zero.
let idx = self.vec.to_physical_idx(i);
let cur = unsafe { &mut *self.vec.ptr().add(idx) };
let idx = self.vec.to_wrapped_index(i);
let cur = unsafe { &mut *self.vec.ptr().add(idx.as_index()) };
let drained = (self.pred)(cur);
// Update the index *after* the predicate is called. If the index
// is updated prior and the predicate panics, the element at this
Expand All @@ -95,7 +95,7 @@ where
// SAFETY: We never touch this element again after returning it.
return Some(unsafe { ptr::read(cur) });
} else if self.del > 0 {
let hole_slot = self.vec.to_physical_idx(i - self.del);
let hole_slot = self.vec.to_wrapped_index(i - self.del);
// SAFETY: `self.del` > 0, so the hole slot must not overlap with current element.
// We use copy for move, and never touch this element again.
unsafe { self.vec.wrap_copy(idx, hole_slot, 1) };
Expand All @@ -113,8 +113,8 @@ where
impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> {
fn drop(&mut self) {
if self.del > 0 {
let src = self.vec.to_physical_idx(self.idx);
let dst = self.vec.to_physical_idx(self.idx - self.del);
let src = self.vec.to_wrapped_index(self.idx);
let dst = self.vec.to_wrapped_index(self.idx - self.del);
let len = self.old_len - self.idx;
// SAFETY: Trailing unchecked items must be valid since we never touch them.
unsafe { self.vec.wrap_copy(src, dst, len) };
Expand All @@ -131,7 +131,7 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let peek = if self.idx < self.end {
let idx = self.vec.to_physical_idx(self.idx);
let idx = self.vec.to_wrapped_index(self.idx);
// This has to use pointer arithmetic as `self.vec[self.idx]` or
// `self.vec.get_unchecked(self.idx)` wouldn't work since we
// temporarily set the length of `self.vec` to zero.
Expand All @@ -141,7 +141,7 @@ where
// smaller than `self.old_len`, `idx` is valid for indexing the
// buffer. Also, per the invariant of `self.idx`, this element
// has not been inspected/moved out yet.
Some(unsafe { &*self.vec.ptr().add(idx) })
Some(unsafe { &*self.vec.ptr().add(idx.as_index()) })
} else {
None
};
Expand Down
9 changes: 5 additions & 4 deletions library/alloc/src/collections/vec_deque/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::ops::Try;
use core::{array, fmt, ptr};

use super::VecDeque;
use super::index::WrappedIndex;
use crate::alloc::{Allocator, Global};

/// An owning iterator over the elements of a `VecDeque`.
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> {
fn drop(&mut self) {
self.deque.len -= self.consumed;
self.deque.head = self.deque.to_physical_idx(self.consumed);
self.deque.head = self.deque.to_wrapped_index(self.consumed);
}
}

Expand Down Expand Up @@ -140,7 +141,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
// SAFETY: By manually adjusting the head and length of the deque, we effectively
// make it forget the first `N` elements, so taking ownership of them is safe.
unsafe { ptr::copy_nonoverlapping(head.as_ptr(), raw_arr_ptr, N) };
self.inner.head = self.inner.to_physical_idx(N);
self.inner.head = self.inner.to_wrapped_index(N);
self.inner.len -= N;
// SAFETY: We initialized the entire array with items from `head`
return Ok(unsafe { raw_arr.transpose().assume_init() });
Expand All @@ -155,7 +156,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
unsafe {
ptr::copy_nonoverlapping(tail.as_ptr(), raw_arr_ptr.add(head.len()), remaining)
};
self.inner.head = self.inner.to_physical_idx(N);
self.inner.head = self.inner.to_wrapped_index(N);
self.inner.len -= N;
// SAFETY: We initialized the entire array with items from `head` and `tail`
Ok(unsafe { raw_arr.transpose().assume_init() })
Expand All @@ -166,7 +167,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
};
let init = head.len() + tail.len();
// We completely drained all the deques elements.
self.inner.head = 0;
self.inner.head = WrappedIndex::zero();
self.inner.len = 0;
// SAFETY: We copied all elements from both slices to the beginning of the array, so
// the given range is initialized.
Expand Down
Loading
Loading