diff --git a/src/vec-final.md b/src/vec-final.md index b84848a..f53c198 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -4,11 +4,11 @@ #![feature(ptr_internals)] // std::ptr::Unique #![feature(alloc_internals)] // std::alloc::* +use std::ptr::{self, Unique}; use std::mem; use std::ops::{Deref, DerefMut}; use std::alloc::{alloc, realloc, Layout, dealloc, rust_oom}; use std::marker::PhantomData; -use std::ptr::{self, Unique}; struct RawVec { ptr: Unique, @@ -90,13 +90,9 @@ pub struct NomVec { } impl NomVec { - fn ptr(&self) -> *mut T { - self.buf.ptr.as_ptr() - } + fn ptr(&self) -> *mut T { self.buf.ptr.as_ptr() } - fn cap(&self) -> usize { - self.buf.cap - } + fn cap(&self) -> usize { self.buf.cap } pub fn new() -> Self { Self { buf: RawVec::new(), len: 0, } @@ -107,6 +103,7 @@ impl NomVec { unsafe { ptr::write(self.ptr().offset(self.len as isize), elem); } + // Can't fail, we'll OOM first. self.len += 1; } @@ -207,31 +204,6 @@ impl IntoIterator for NomVec { } -pub struct IntoIter { - _buf: RawVec, - iter: RawValIter, -} - -impl Iterator for IntoIter { - type Item = T; - fn next(&mut self) -> Option { self.iter.next() } - - fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } -} - -impl DoubleEndedIterator for IntoIter { - fn next_back(&mut self) -> Option { self.iter.next_back() } -} - -impl Drop for IntoIter { - fn drop(&mut self) { - // only need to ensure all our elements are read; - // buffer will clean itself up afterwards. - for _ in &mut self.iter {} - } -} - - struct RawValIter { start: *const T, @@ -299,6 +271,38 @@ impl DoubleEndedIterator for RawValIter { +pub struct IntoIter { + _buf: RawVec, + iter: RawValIter, +} + +impl Iterator for IntoIter { + type Item = T; + fn next(&mut self) -> Option { + self.iter.next() + } + + fn size_hint(&self) -> (usize, Option) { + self.iter.size_hint() + } +} + +impl DoubleEndedIterator for IntoIter { + fn next_back(&mut self) -> Option { + self.iter.next_back() + } +} + +impl Drop for IntoIter { + fn drop(&mut self) { + // only need to ensure all our elements are read; + // buffer will clean itself up afterwards. + for _ in &mut self.iter {} + } +} + + + pub struct Drain<'a, T: 'a> { // Need to bound the lifetime here, so we do it with `&'a mut Vec` // because that's semantically what we contain. We're "just" calling @@ -431,6 +435,7 @@ mod tests { } + # fn main() { # tests::create_push_pop(); # tests::iter_test();