parent
f8b030152e
commit
eed4628613
@ -0,0 +1,118 @@
|
||||
// https://doc.rust-lang.org/nomicon/vec/vec.html
|
||||
|
||||
use std::alloc::{self, Layout};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::ptr::{self, NonNull};
|
||||
|
||||
pub struct Vec<T> {
|
||||
ptr: NonNull<T>,
|
||||
cap: usize,
|
||||
len: usize,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
unsafe impl<T: Send> Send for Vec<T> {}
|
||||
unsafe impl<T: Sync> Sync for Vec<T> {}
|
||||
|
||||
impl<T> Vec<T> {
|
||||
pub fn new() -> Self {
|
||||
assert!(mem::size_of::<T>() != 0, "We're not ready to handle ZSTs");
|
||||
Vec {
|
||||
ptr: NonNull::dangling(),
|
||||
len: 0,
|
||||
cap: 0,
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, elem: T) {
|
||||
if self.len == self.cap {
|
||||
self.grow();
|
||||
}
|
||||
|
||||
unsafe {
|
||||
ptr::write(self.ptr.as_ptr().add(self.len), elem);
|
||||
}
|
||||
|
||||
// Can't fail, we'll OOM first.
|
||||
self.len += 1;
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
if self.len == 0 {
|
||||
None
|
||||
} else {
|
||||
self.len -= 1;
|
||||
unsafe { Some(ptr::read(self.ptr.as_ptr().add(self.len))) }
|
||||
}
|
||||
}
|
||||
|
||||
fn grow(&mut self) {
|
||||
let (new_cap, new_layout) = if self.cap == 0 {
|
||||
(1, Layout::array::<T>(1).unwrap())
|
||||
} else {
|
||||
// This can't overflow since self.cap <= isize::MAX.
|
||||
let new_cap = 2 * self.cap;
|
||||
|
||||
// `Layout::array` checks that the number of bytes is <= usize::MAX,
|
||||
// but this is redundant since old_layout.size() <= isize::MAX,
|
||||
// so the `unwrap` should never fail.
|
||||
let new_layout = Layout::array::<T>(new_cap).unwrap();
|
||||
(new_cap, new_layout)
|
||||
};
|
||||
|
||||
// Ensure that the new allocation doesn't exceed `isize::MAX` bytes.
|
||||
assert!(
|
||||
new_layout.size() <= isize::MAX as usize,
|
||||
"Allocation too large"
|
||||
);
|
||||
|
||||
let new_ptr = if self.cap == 0 {
|
||||
unsafe { alloc::alloc(new_layout) }
|
||||
} else {
|
||||
let old_layout = Layout::array::<T>(self.cap).unwrap();
|
||||
let old_ptr = self.ptr.as_ptr() as *mut u8;
|
||||
unsafe { alloc::realloc(old_ptr, old_layout, new_layout.size()) }
|
||||
};
|
||||
|
||||
// If allocation fails, `new_ptr` will be null, in which case we abort.
|
||||
self.ptr = match NonNull::new(new_ptr as *mut T) {
|
||||
Some(p) => p,
|
||||
None => alloc::handle_alloc_error(new_layout),
|
||||
};
|
||||
self.cap = new_cap;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for Vec<T> {
|
||||
fn drop(&mut self) {
|
||||
if self.cap != 0 {
|
||||
while self.pop().is_some() {}
|
||||
let layout = Layout::array::<T>(self.cap).unwrap();
|
||||
unsafe {
|
||||
alloc::dealloc(self.ptr.as_ptr() as *mut u8, layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for Vec<T> {
|
||||
type Target = [T];
|
||||
fn deref(&self) -> &[T] {
|
||||
unsafe { std::slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for Vec<T> {
|
||||
fn deref_mut(&mut self) -> &mut [T] {
|
||||
unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Vec<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
Loading…
Reference in new issue