diff options
| -rw-r--r-- | src/libcollections/ring_buf.rs | 60 |
1 files changed, 31 insertions, 29 deletions
diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index 6dcdb21f800..6e5970345f1 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -28,7 +28,7 @@ use core::marker; use core::mem; use core::num::{Int, UnsignedInt}; use core::ops::{Index, IndexMut}; -use core::ptr; +use core::ptr::{self, Unique}; use core::raw::Slice as RawSlice; use core::hash::{Writer, Hash, Hasher}; @@ -51,7 +51,7 @@ pub struct RingBuf<T> { tail: usize, head: usize, cap: usize, - ptr: *mut T + ptr: Unique<T>, } #[stable(feature = "rust1", since = "1.0.0")] @@ -74,7 +74,7 @@ impl<T> Drop for RingBuf<T> { self.clear(); unsafe { if mem::size_of::<T>() != 0 { - heap::deallocate(self.ptr as *mut u8, + heap::deallocate(*self.ptr as *mut u8, self.cap * mem::size_of::<T>(), mem::min_align_of::<T>()) } @@ -92,13 +92,13 @@ impl<T> RingBuf<T> { /// Turn ptr into a slice #[inline] unsafe fn buffer_as_slice(&self) -> &[T] { - mem::transmute(RawSlice { data: self.ptr, len: self.cap }) + mem::transmute(RawSlice { data: *self.ptr as *const T, len: self.cap }) } /// Turn ptr into a mut slice #[inline] unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] { - mem::transmute(RawSlice { data: self.ptr, len: self.cap }) + mem::transmute(RawSlice { data: *self.ptr as *const T, len: self.cap }) } /// Moves an element out of the buffer @@ -165,21 +165,21 @@ impl<T> RingBuf<T> { let size = cap.checked_mul(mem::size_of::<T>()) .expect("capacity overflow"); - let ptr = if mem::size_of::<T>() != 0 { - unsafe { + let ptr = unsafe { + if mem::size_of::<T>() != 0 { let ptr = heap::allocate(size, mem::min_align_of::<T>()) as *mut T;; if ptr.is_null() { ::alloc::oom() } - ptr + Unique::new(ptr) + } else { + Unique::new(heap::EMPTY as *mut T) } - } else { - heap::EMPTY as *mut T }; RingBuf { tail: 0, head: 0, cap: cap, - ptr: ptr + ptr: ptr, } } @@ -335,11 +335,12 @@ impl<T> RingBuf<T> { let new = count.checked_mul(mem::size_of::<T>()) .expect("capacity overflow"); unsafe { - self.ptr = heap::reallocate(self.ptr as *mut u8, - old, - new, - mem::min_align_of::<T>()) as *mut T; - if self.ptr.is_null() { ::alloc::oom() } + let ptr = heap::reallocate(*self.ptr as *mut u8, + old, + new, + mem::min_align_of::<T>()) as *mut T; + if ptr.is_null() { ::alloc::oom() } + self.ptr = Unique::new(ptr); } } @@ -453,11 +454,12 @@ impl<T> RingBuf<T> { let old = self.cap * mem::size_of::<T>(); let new_size = target_cap * mem::size_of::<T>(); unsafe { - self.ptr = heap::reallocate(self.ptr as *mut u8, - old, - new_size, - mem::min_align_of::<T>()) as *mut T; - if self.ptr.is_null() { ::alloc::oom() } + let ptr = heap::reallocate(*self.ptr as *mut u8, + old, + new_size, + mem::min_align_of::<T>()) as *mut T; + if ptr.is_null() { ::alloc::oom() } + self.ptr = Unique::new(ptr); } } self.cap = target_cap; @@ -539,8 +541,8 @@ impl<T> RingBuf<T> { tail: self.tail, head: self.head, cap: self.cap, - ptr: self.ptr, - marker: marker::ContravariantLifetime, + ptr: *self.ptr, + marker: marker::PhantomData, } } @@ -1336,7 +1338,7 @@ impl<T> RingBuf<T> { // `at` lies in the first half. let amount_in_first = first_len - at; - ptr::copy_nonoverlapping_memory(other.ptr, + ptr::copy_nonoverlapping_memory(*other.ptr, first_half.as_ptr().offset(at as isize), amount_in_first); @@ -1349,7 +1351,7 @@ impl<T> RingBuf<T> { // in the first half. let offset = at - first_len; let amount_in_second = second_len - offset; - ptr::copy_nonoverlapping_memory(other.ptr, + ptr::copy_nonoverlapping_memory(*other.ptr, second_half.as_ptr().offset(offset as isize), amount_in_second); } @@ -1518,7 +1520,7 @@ pub struct IterMut<'a, T:'a> { tail: usize, head: usize, cap: usize, - marker: marker::ContravariantLifetime<'a>, + marker: marker::PhantomData<&'a mut T>, } #[stable(feature = "rust1", since = "1.0.0")] @@ -1986,9 +1988,9 @@ mod tests { #[derive(Clone, PartialEq, Debug)] enum Taggypar<T> { - Onepar(i32), - Twopar(i32, i32), - Threepar(i32, i32, i32), + Onepar(T), + Twopar(T, T), + Threepar(T, T, T), } #[derive(Clone, PartialEq, Debug)] |
