3 /// Returns the two slices that cover the `VecDeque`'s valid range
4 pub trait RingSlices: Sized {
5 fn slice(self, from: usize, to: usize) -> Self;
6 fn split_at(self, i: usize) -> (Self, Self);
8 fn ring_slices(buf: Self, head: usize, tail: usize) -> (Self, Self) {
9 let contiguous = tail <= head;
11 let (empty, buf) = buf.split_at(0);
12 (buf.slice(tail, head), empty)
14 let (mid, right) = buf.split_at(tail);
15 let (left, _) = mid.split_at(head);
21 impl<T> RingSlices for &[T] {
22 fn slice(self, from: usize, to: usize) -> Self {
25 fn split_at(self, i: usize) -> (Self, Self) {
30 impl<T> RingSlices for &mut [T] {
31 fn slice(self, from: usize, to: usize) -> Self {
34 fn split_at(self, i: usize) -> (Self, Self) {
35 (*self).split_at_mut(i)
39 impl<T> RingSlices for *mut [T] {
40 fn slice(self, from: usize, to: usize) -> Self {
41 assert!(from <= to && to < self.len());
42 // Not using `get_unchecked_mut` to keep this a safe operation.
44 ptr::slice_from_raw_parts_mut(self.as_mut_ptr().wrapping_add(from), len)
47 fn split_at(self, mid: usize) -> (Self, Self) {
49 let ptr = self.as_mut_ptr();
52 ptr::slice_from_raw_parts_mut(ptr, mid),
53 ptr::slice_from_raw_parts_mut(ptr.wrapping_add(mid), len - mid),