diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-03-28 23:10:58 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-03-29 00:20:05 -0700 |
| commit | ad21976fbc4776fe09f98cd087fbe4cecf23cd84 (patch) | |
| tree | fa22db7b2756e8ea4e90130de78678fb0c7bc1b7 /src/libcore | |
| parent | 5e42c5cf19bea49adc718e8502d364897bebac66 (diff) | |
| download | rust-ad21976fbc4776fe09f98cd087fbe4cecf23cd84.tar.gz rust-ad21976fbc4776fe09f98cd087fbe4cecf23cd84.zip | |
core: Add vec::capacity
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/vec.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 60ed23a6582..e4626c97c3e 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -9,6 +9,7 @@ export is_not_empty; export same_length; export reserve; export reserve_at_least; +export capacity; export len; export from_fn; export from_elem; @@ -134,6 +135,14 @@ fn reserve_at_least<T>(&v: [const T], n: uint) { reserve(v, uint::next_power_of_two(n)); } +#[doc = " +Returns the number of elements the vector can hold without reallocating +"] +fn capacity<T>(&&v: [const T]) -> uint unsafe { + let repr: **unsafe::vec_repr = ::unsafe::reinterpret_cast(addr_of(v)); + (**repr).alloc / sys::size_of::<T>() +} + #[doc = "Returns the length of a vector"] #[inline(always)] pure fn len<T>(&&v: [const T]) -> uint unsafe { @@ -1801,6 +1810,16 @@ mod tests { unshift(x, 0); assert x == [0, 1, 2, 3]; } + + #[test] + fn test_capacity() { + let mut v = [0u64]; + reserve(v, 10u); + assert capacity(v) == 10u; + let mut v = [0u32]; + reserve(v, 10u); + assert capacity(v) == 10u; + } } // Local Variables: |
