Skip to content

Commit 5300503

Browse files
authored
Merge pull request #10 from adierking/fix-asserts
2 parents 07555da + c1e6752 commit 5300503

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/lib.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,18 @@ impl<T> SliceRingBuffer<T> {
486486
/// of the deque and the second one is empty.
487487
#[inline]
488488
pub fn as_slices(&self) -> (&[T], &[T]) {
489-
unsafe {
490-
let left = self.as_slice();
491-
let right =
492-
slice::from_raw_parts(usize::max_value() as *const _, 0);
493-
(left, right)
494-
}
489+
let left = self.as_slice();
490+
let right = &[];
491+
(left, right)
495492
}
496493

497494
/// Returns a pair of slices, where the first slice contains the contents
498495
/// of the deque and the second one is empty.
499496
#[inline]
500497
pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
501-
unsafe {
502-
let left = self.as_mut_slice();
503-
let right =
504-
slice::from_raw_parts_mut(usize::max_value() as *mut _, 0);
505-
(left, right)
506-
}
498+
let left = self.as_mut_slice();
499+
let right = &mut [];
500+
(left, right)
507501
}
508502

509503
/// Returns the slice of uninitialized memory between the `tail` and the
@@ -783,14 +777,17 @@ impl<T> SliceRingBuffer<T> {
783777
#[inline]
784778
unsafe fn append_elements(&mut self, other: *const [T]) {
785779
let count = (*other).len();
786-
self.reserve(count);
787-
let len = self.len();
788-
ptr::copy_nonoverlapping(
789-
other as *const T,
790-
self.get_unchecked_mut(len),
791-
count,
792-
);
793-
self.move_tail_unchecked(count as isize);
780+
// Rust 1.78+: get_unchecked_mut() may panic in debug builds if we don't move the tail
781+
if count > 0 {
782+
self.reserve(count);
783+
let len = self.len();
784+
self.move_tail_unchecked(count as isize);
785+
ptr::copy_nonoverlapping(
786+
other as *const T,
787+
self.get_unchecked_mut(len),
788+
count,
789+
);
790+
}
794791
}
795792

796793
/// Steal the elements from the slice `s`. You should `mem::forget` the
@@ -1717,10 +1714,10 @@ impl<T> SliceRingBuffer<T> {
17171714
}
17181715
debug_assert!(self.len() < self.capacity());
17191716
unsafe {
1720-
ptr::write(self.get_unchecked_mut(len), element);
17211717
// NB can't overflow since we would have had to alloc the
17221718
// address space
17231719
self.move_tail_unchecked(1);
1720+
ptr::write(self.get_unchecked_mut(len), element);
17241721
}
17251722
}
17261723
}
@@ -2580,8 +2577,8 @@ fn from_iter_default<T, I: Iterator<Item = T>>(
25802577
let mut deque =
25812578
SliceRingBuffer::<T>::with_capacity(lower.saturating_add(1));
25822579
unsafe {
2583-
ptr::write(deque.get_unchecked_mut(0), element);
25842580
deque.move_tail_unchecked(1);
2581+
ptr::write(deque.get_unchecked_mut(0), element);
25852582
}
25862583
deque
25872584
}

0 commit comments

Comments
 (0)