@@ -486,24 +486,18 @@ impl<T> SliceRingBuffer<T> {
486
486
/// of the deque and the second one is empty.
487
487
#[ inline]
488
488
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)
495
492
}
496
493
497
494
/// Returns a pair of slices, where the first slice contains the contents
498
495
/// of the deque and the second one is empty.
499
496
#[ inline]
500
497
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)
507
501
}
508
502
509
503
/// Returns the slice of uninitialized memory between the `tail` and the
@@ -783,14 +777,17 @@ impl<T> SliceRingBuffer<T> {
783
777
#[ inline]
784
778
unsafe fn append_elements ( & mut self , other : * const [ T ] ) {
785
779
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
+ }
794
791
}
795
792
796
793
/// Steal the elements from the slice `s`. You should `mem::forget` the
@@ -1717,10 +1714,10 @@ impl<T> SliceRingBuffer<T> {
1717
1714
}
1718
1715
debug_assert ! ( self . len( ) < self . capacity( ) ) ;
1719
1716
unsafe {
1720
- ptr:: write ( self . get_unchecked_mut ( len) , element) ;
1721
1717
// NB can't overflow since we would have had to alloc the
1722
1718
// address space
1723
1719
self . move_tail_unchecked ( 1 ) ;
1720
+ ptr:: write ( self . get_unchecked_mut ( len) , element) ;
1724
1721
}
1725
1722
}
1726
1723
}
@@ -2580,8 +2577,8 @@ fn from_iter_default<T, I: Iterator<Item = T>>(
2580
2577
let mut deque =
2581
2578
SliceRingBuffer :: < T > :: with_capacity ( lower. saturating_add ( 1 ) ) ;
2582
2579
unsafe {
2583
- ptr:: write ( deque. get_unchecked_mut ( 0 ) , element) ;
2584
2580
deque. move_tail_unchecked ( 1 ) ;
2581
+ ptr:: write ( deque. get_unchecked_mut ( 0 ) , element) ;
2585
2582
}
2586
2583
deque
2587
2584
}
0 commit comments