RawVec

We’ve actually reached an interesting situation here: we’ve duplicated the logicfor specifying a buffer and freeing its memory in Vec and IntoIter. Now thatwe’ve implemented it and identified actual logic duplication, this is a goodtime to perform some logic compression.

We’re going to abstract out the (ptr, cap) pair and give them the logic forallocating, growing, and freeing:

  1. struct RawVec<T> {
  2. ptr: Unique<T>,
  3. cap: usize,
  4. }
  5. impl<T> RawVec<T> {
  6. fn new() -> Self {
  7. assert!(mem::size_of::<T>() != 0, "TODO: implement ZST support");
  8. RawVec { ptr: Unique::empty(), cap: 0 }
  9. }
  10. // unchanged from Vec
  11. fn grow(&mut self) {
  12. unsafe {
  13. let align = mem::align_of::<T>();
  14. let elem_size = mem::size_of::<T>();
  15. let (new_cap, ptr) = if self.cap == 0 {
  16. let ptr = heap::allocate(elem_size, align);
  17. (1, ptr)
  18. } else {
  19. let new_cap = 2 * self.cap;
  20. let ptr = heap::reallocate(self.ptr.as_ptr() as *mut _,
  21. self.cap * elem_size,
  22. new_cap * elem_size,
  23. align);
  24. (new_cap, ptr)
  25. };
  26. // If allocate or reallocate fail, we'll get `null` back
  27. if ptr.is_null() { oom() }
  28. self.ptr = Unique::new(ptr as *mut _);
  29. self.cap = new_cap;
  30. }
  31. }
  32. }
  33. impl<T> Drop for RawVec<T> {
  34. fn drop(&mut self) {
  35. if self.cap != 0 {
  36. let align = mem::align_of::<T>();
  37. let elem_size = mem::size_of::<T>();
  38. let num_bytes = elem_size * self.cap;
  39. unsafe {
  40. heap::deallocate(self.ptr.as_mut() as *mut _, num_bytes, align);
  41. }
  42. }
  43. }
  44. }

And change Vec as follows:

  1. pub struct Vec<T> {
  2. buf: RawVec<T>,
  3. len: usize,
  4. }
  5. impl<T> Vec<T> {
  6. fn ptr(&self) -> *mut T { self.buf.ptr.as_ptr() }
  7. fn cap(&self) -> usize { self.buf.cap }
  8. pub fn new() -> Self {
  9. Vec { buf: RawVec::new(), len: 0 }
  10. }
  11. // push/pop/insert/remove largely unchanged:
  12. // * `self.ptr -> self.ptr()`
  13. // * `self.cap -> self.cap()`
  14. // * `self.grow -> self.buf.grow()`
  15. }
  16. impl<T> Drop for Vec<T> {
  17. fn drop(&mut self) {
  18. while let Some(_) = self.pop() {}
  19. // deallocation is handled by RawVec
  20. }
  21. }

And finally we can really simplify IntoIter:

  1. struct IntoIter<T> {
  2. _buf: RawVec<T>, // we don't actually care about this. Just need it to live.
  3. start: *const T,
  4. end: *const T,
  5. }
  6. // next and next_back literally unchanged since they never referred to the buf
  7. impl<T> Drop for IntoIter<T> {
  8. fn drop(&mut self) {
  9. // only need to ensure all our elements are read;
  10. // buffer will clean itself up afterwards.
  11. for _ in &mut *self {}
  12. }
  13. }
  14. impl<T> Vec<T> {
  15. pub fn into_iter(self) -> IntoIter<T> {
  16. unsafe {
  17. // need to use ptr::read to unsafely move the buf out since it's
  18. // not Copy, and Vec implements Drop (so we can't destructure it).
  19. let buf = ptr::read(&self.buf);
  20. let len = self.len;
  21. mem::forget(self);
  22. IntoIter {
  23. start: *buf.ptr,
  24. end: buf.ptr.offset(len as isize),
  25. _buf: buf,
  26. }
  27. }
  28. }
  29. }

Much better.