@@ -27,6 +27,10 @@ use {Collection, Mutable};
27
27
use slice:: { MutableOrdVector , MutableVectorAllocating , CloneableVector } ;
28
28
use slice:: { Items , MutItems } ;
29
29
30
+
31
+ #[ doc( hidden) ]
32
+ pub static PTR_MARKER : u8 = 0 ;
33
+
30
34
/// An owned, growable vector.
31
35
///
32
36
/// # Examples
@@ -71,7 +75,13 @@ impl<T> Vec<T> {
71
75
/// ```
72
76
#[ inline]
73
77
pub fn new ( ) -> Vec < T > {
74
- Vec { len : 0 , cap : 0 , ptr : 0 as * mut T }
78
+ // If we have a 0-sized vector, then the base pointer should not be NULL
79
+ // because an iterator over the slice will attempt to yield the base
80
+ // pointer as the first element in the vector, but this will end up
81
+ // being Some(NULL) which is optimized to None. So instead we set ptr
82
+ // to some arbitrary non-null value which is fine since we never call
83
+ // deallocate on the ptr if cap is 0.
84
+ Vec { len : 0 , cap : 0 , ptr : & PTR_MARKER as * const _ as * mut T }
75
85
}
76
86
77
87
/// Constructs a new, empty `Vec` with the specified capacity.
@@ -88,7 +98,7 @@ impl<T> Vec<T> {
88
98
#[ inline]
89
99
pub fn with_capacity ( capacity : uint ) -> Vec < T > {
90
100
if mem:: size_of :: < T > ( ) == 0 {
91
- Vec { len : 0 , cap : uint:: MAX , ptr : 0 as * mut T }
101
+ Vec { len : 0 , cap : uint:: MAX , ptr : & PTR_MARKER as * const _ as * mut T }
92
102
} else if capacity == 0 {
93
103
Vec :: new ( )
94
104
} else {
@@ -1206,15 +1216,7 @@ impl<T> Vec<T> {
1206
1216
/// would also make any pointers to it invalid.
1207
1217
#[ inline]
1208
1218
pub fn as_ptr ( & self ) -> * const T {
1209
- // If we have a 0-sized vector, then the base pointer should not be NULL
1210
- // because an iterator over the slice will attempt to yield the base
1211
- // pointer as the first element in the vector, but this will end up
1212
- // being Some(NULL) which is optimized to None.
1213
- if mem:: size_of :: < T > ( ) == 0 {
1214
- 1 as * const T
1215
- } else {
1216
- self . ptr as * const T
1217
- }
1219
+ self . ptr as * const T
1218
1220
}
1219
1221
1220
1222
/// Returns a mutable unsafe pointer to the vector's buffer.
@@ -1226,12 +1228,7 @@ impl<T> Vec<T> {
1226
1228
/// would also make any pointers to it invalid.
1227
1229
#[ inline]
1228
1230
pub fn as_mut_ptr ( & mut self ) -> * mut T {
1229
- // see above for the 0-size check
1230
- if mem:: size_of :: < T > ( ) == 0 {
1231
- 1 as * mut T
1232
- } else {
1233
- self . ptr
1234
- }
1231
+ self . ptr
1235
1232
}
1236
1233
1237
1234
/// Retains only the elements specified by the predicate.
0 commit comments