@@ -28,7 +28,7 @@ use core::marker;
28
28
use core:: mem;
29
29
use core:: num:: { Int , UnsignedInt } ;
30
30
use core:: ops:: { Index , IndexMut } ;
31
- use core:: ptr;
31
+ use core:: ptr:: { self , Unique } ;
32
32
use core:: raw:: Slice as RawSlice ;
33
33
34
34
use core:: hash:: { Writer , Hash , Hasher } ;
@@ -51,7 +51,7 @@ pub struct RingBuf<T> {
51
51
tail : usize ,
52
52
head : usize ,
53
53
cap : usize ,
54
- ptr : * mut T
54
+ ptr : Unique < T > ,
55
55
}
56
56
57
57
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -74,7 +74,7 @@ impl<T> Drop for RingBuf<T> {
74
74
self . clear ( ) ;
75
75
unsafe {
76
76
if mem:: size_of :: < T > ( ) != 0 {
77
- heap:: deallocate ( self . ptr as * mut u8 ,
77
+ heap:: deallocate ( * self . ptr as * mut u8 ,
78
78
self . cap * mem:: size_of :: < T > ( ) ,
79
79
mem:: min_align_of :: < T > ( ) )
80
80
}
@@ -92,13 +92,13 @@ impl<T> RingBuf<T> {
92
92
/// Turn ptr into a slice
93
93
#[ inline]
94
94
unsafe fn buffer_as_slice ( & self ) -> & [ T ] {
95
- mem:: transmute ( RawSlice { data : self . ptr , len : self . cap } )
95
+ mem:: transmute ( RawSlice { data : * self . ptr as * const T , len : self . cap } )
96
96
}
97
97
98
98
/// Turn ptr into a mut slice
99
99
#[ inline]
100
100
unsafe fn buffer_as_mut_slice ( & mut self ) -> & mut [ T ] {
101
- mem:: transmute ( RawSlice { data : self . ptr , len : self . cap } )
101
+ mem:: transmute ( RawSlice { data : * self . ptr as * const T , len : self . cap } )
102
102
}
103
103
104
104
/// Moves an element out of the buffer
@@ -165,21 +165,21 @@ impl<T> RingBuf<T> {
165
165
let size = cap. checked_mul ( mem:: size_of :: < T > ( ) )
166
166
. expect ( "capacity overflow" ) ;
167
167
168
- let ptr = if mem :: size_of :: < T > ( ) != 0 {
169
- unsafe {
168
+ let ptr = unsafe {
169
+ if mem :: size_of :: < T > ( ) != 0 {
170
170
let ptr = heap:: allocate ( size, mem:: min_align_of :: < T > ( ) ) as * mut T ; ;
171
171
if ptr. is_null ( ) { :: alloc:: oom ( ) }
172
- ptr
172
+ Unique :: new ( ptr)
173
+ } else {
174
+ Unique :: new ( heap:: EMPTY as * mut T )
173
175
}
174
- } else {
175
- heap:: EMPTY as * mut T
176
176
} ;
177
177
178
178
RingBuf {
179
179
tail : 0 ,
180
180
head : 0 ,
181
181
cap : cap,
182
- ptr : ptr
182
+ ptr : ptr,
183
183
}
184
184
}
185
185
@@ -335,11 +335,12 @@ impl<T> RingBuf<T> {
335
335
let new = count. checked_mul ( mem:: size_of :: < T > ( ) )
336
336
. expect ( "capacity overflow" ) ;
337
337
unsafe {
338
- self . ptr = heap:: reallocate ( self . ptr as * mut u8 ,
339
- old,
340
- new,
341
- mem:: min_align_of :: < T > ( ) ) as * mut T ;
342
- if self . ptr . is_null ( ) { :: alloc:: oom ( ) }
338
+ let ptr = heap:: reallocate ( * self . ptr as * mut u8 ,
339
+ old,
340
+ new,
341
+ mem:: min_align_of :: < T > ( ) ) as * mut T ;
342
+ if ptr. is_null ( ) { :: alloc:: oom ( ) }
343
+ self . ptr = Unique :: new ( ptr) ;
343
344
}
344
345
}
345
346
@@ -453,11 +454,12 @@ impl<T> RingBuf<T> {
453
454
let old = self . cap * mem:: size_of :: < T > ( ) ;
454
455
let new_size = target_cap * mem:: size_of :: < T > ( ) ;
455
456
unsafe {
456
- self . ptr = heap:: reallocate ( self . ptr as * mut u8 ,
457
- old,
458
- new_size,
459
- mem:: min_align_of :: < T > ( ) ) as * mut T ;
460
- if self . ptr . is_null ( ) { :: alloc:: oom ( ) }
457
+ let ptr = heap:: reallocate ( * self . ptr as * mut u8 ,
458
+ old,
459
+ new_size,
460
+ mem:: min_align_of :: < T > ( ) ) as * mut T ;
461
+ if ptr. is_null ( ) { :: alloc:: oom ( ) }
462
+ self . ptr = Unique :: new ( ptr) ;
461
463
}
462
464
}
463
465
self . cap = target_cap;
@@ -539,8 +541,8 @@ impl<T> RingBuf<T> {
539
541
tail : self . tail ,
540
542
head : self . head ,
541
543
cap : self . cap ,
542
- ptr : self . ptr ,
543
- marker : marker:: ContravariantLifetime ,
544
+ ptr : * self . ptr ,
545
+ marker : marker:: PhantomData ,
544
546
}
545
547
}
546
548
@@ -1336,7 +1338,7 @@ impl<T> RingBuf<T> {
1336
1338
// `at` lies in the first half.
1337
1339
let amount_in_first = first_len - at;
1338
1340
1339
- ptr:: copy_nonoverlapping_memory ( other. ptr ,
1341
+ ptr:: copy_nonoverlapping_memory ( * other. ptr ,
1340
1342
first_half. as_ptr ( ) . offset ( at as isize ) ,
1341
1343
amount_in_first) ;
1342
1344
@@ -1349,7 +1351,7 @@ impl<T> RingBuf<T> {
1349
1351
// in the first half.
1350
1352
let offset = at - first_len;
1351
1353
let amount_in_second = second_len - offset;
1352
- ptr:: copy_nonoverlapping_memory ( other. ptr ,
1354
+ ptr:: copy_nonoverlapping_memory ( * other. ptr ,
1353
1355
second_half. as_ptr ( ) . offset ( offset as isize ) ,
1354
1356
amount_in_second) ;
1355
1357
}
@@ -1518,7 +1520,7 @@ pub struct IterMut<'a, T:'a> {
1518
1520
tail : usize ,
1519
1521
head : usize ,
1520
1522
cap : usize ,
1521
- marker : marker:: ContravariantLifetime < ' a > ,
1523
+ marker : marker:: PhantomData < & ' a mut T > ,
1522
1524
}
1523
1525
1524
1526
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1986,9 +1988,9 @@ mod tests {
1986
1988
1987
1989
#[ derive( Clone , PartialEq , Debug ) ]
1988
1990
enum Taggypar < T > {
1989
- Onepar ( i32 ) ,
1990
- Twopar ( i32 , i32 ) ,
1991
- Threepar ( i32 , i32 , i32 ) ,
1991
+ Onepar ( T ) ,
1992
+ Twopar ( T , T ) ,
1993
+ Threepar ( T , T , T ) ,
1992
1994
}
1993
1995
1994
1996
#[ derive( Clone , PartialEq , Debug ) ]
0 commit comments