File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ export is_not_empty;
9
9
export same_length;
10
10
export reserve;
11
11
export reserve_at_least;
12
+ export capacity;
12
13
export len;
13
14
export from_fn;
14
15
export from_elem;
@@ -134,6 +135,14 @@ fn reserve_at_least<T>(&v: [const T], n: uint) {
134
135
reserve ( v, uint:: next_power_of_two ( n) ) ;
135
136
}
136
137
138
+ #[ doc = "
139
+ Returns the number of elements the vector can hold without reallocating
140
+ " ]
141
+ fn capacity < T > ( & & v: [ const T ] ) -> uint unsafe {
142
+ let repr: * * unsafe :: vec_repr = :: unsafe:: reinterpret_cast ( addr_of ( v) ) ;
143
+ ( * * repr) . alloc / sys:: size_of :: < T > ( )
144
+ }
145
+
137
146
#[ doc = "Returns the length of a vector" ]
138
147
#[ inline( always) ]
139
148
pure fn len < T > ( & & v: [ const T ] ) -> uint unsafe {
@@ -1801,6 +1810,16 @@ mod tests {
1801
1810
unshift ( x, 0 ) ;
1802
1811
assert x == [ 0 , 1 , 2 , 3 ] ;
1803
1812
}
1813
+
1814
+ #[ test]
1815
+ fn test_capacity ( ) {
1816
+ let mut v = [ 0u64 ] ;
1817
+ reserve ( v, 10 u) ;
1818
+ assert capacity( v) == 10 u;
1819
+ let mut v = [ 0u32 ] ;
1820
+ reserve ( v, 10 u) ;
1821
+ assert capacity( v) == 10 u;
1822
+ }
1804
1823
}
1805
1824
1806
1825
// Local Variables:
You can’t perform that action at this time.
0 commit comments