|
|
|
@ -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<T> {
|
|
|
|
|
ptr: Unique<T>,
|
|
|
|
@ -90,13 +90,9 @@ pub struct NomVec<T> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> NomVec<T> {
|
|
|
|
|
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<T> NomVec<T> {
|
|
|
|
|
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<T> IntoIterator for NomVec<T> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct IntoIter<T> {
|
|
|
|
|
_buf: RawVec<T>,
|
|
|
|
|
iter: RawValIter<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Iterator for IntoIter<T> {
|
|
|
|
|
type Item = T;
|
|
|
|
|
fn next(&mut self) -> Option<T> { self.iter.next() }
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> DoubleEndedIterator for IntoIter<T> {
|
|
|
|
|
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for IntoIter<T> {
|
|
|
|
|
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<T> {
|
|
|
|
|
start: *const T,
|
|
|
|
@ -299,6 +271,38 @@ impl<T> DoubleEndedIterator for RawValIter<T> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct IntoIter<T> {
|
|
|
|
|
_buf: RawVec<T>,
|
|
|
|
|
iter: RawValIter<T>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Iterator for IntoIter<T> {
|
|
|
|
|
type Item = T;
|
|
|
|
|
fn next(&mut self) -> Option<T> {
|
|
|
|
|
self.iter.next()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
self.iter.size_hint()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> DoubleEndedIterator for IntoIter<T> {
|
|
|
|
|
fn next_back(&mut self) -> Option<T> {
|
|
|
|
|
self.iter.next_back()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for IntoIter<T> {
|
|
|
|
|
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<T>`
|
|
|
|
|
// 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();
|
|
|
|
|