Deallocating
Next we should implement Drop so that we don’t massively leak tons of resources.The easiest way is to just call pop
until it yields None, and then deallocateour buffer. Note that calling pop
is unneeded if T: !Drop
. In theory we canask Rust if T
needs_drop
and omit the calls to pop
. However in practiceLLVM is really good at removing simple side-effect free code like this, so Iwouldn’t bother unless you notice it’s not being stripped (in this case it is).
We must not call heap::deallocate
when self.cap == 0
, as in this case wehaven’t actually allocated any memory.
impl<T> Drop for Vec<T> {
fn drop(&mut self) {
if self.cap != 0 {
while let Some(_) = self.pop() { }
let align = mem::align_of::<T>();
let elem_size = mem::size_of::<T>();
let num_bytes = elem_size * self.cap;
unsafe {
heap::deallocate(self.ptr.as_ptr() as *mut _, num_bytes, align);
}
}
}
}
当前内容版权归 rust-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 rust-lang.org .