提交 ad21976f 编写于 作者: B Brian Anderson

core: Add vec::capacity

上级 5e42c5cf
......@@ -9,6 +9,7 @@
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 @@ fn test_unshift() {
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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册