498 lines
16 KiB
Rust
498 lines
16 KiB
Rust
//! # A [`Vec<T: ?Sized>`]
|
|
//!
|
|
//! A dynamic length collection of unsized elements, akin to [`std::vec::Vec`].
|
|
//!
|
|
//! This crate depends on a number of unstable features, namely:
|
|
//! - `#![feature(ptr_metadata)]`, to allow manipulating the metadata of pointers.
|
|
//! - `#![feature(layout_for_ptr)]`, to allow getting the size/alignment of a value with only a pointer to it.
|
|
//! - `#![feature(coerce_unsized)]`, to add trait bounds for types which can be unsized to another type.
|
|
//!
|
|
//! # Examples
|
|
//!
|
|
//! You can create a `Vec` with [`Vec::new`]:
|
|
//!
|
|
//! ```
|
|
//! # use std::fmt::Debug;
|
|
//! # use dyn_vec::prelude::*;
|
|
//! let vec: Vec<dyn Debug> = Vec::new();
|
|
//! # assert_eq!(format!("{:?}", vec), "[]");
|
|
//! ```
|
|
//!
|
|
//! or with the [`vec!`] macro:
|
|
//!
|
|
//! ```
|
|
//! # use dyn_vec::prelude::{*, vec};
|
|
//! # use std::fmt::Debug;
|
|
//! let vec: Vec<i32> = vec![1, 2, 3];
|
|
//! // check the docs for `vec!` for more info on this syntax
|
|
//! let vec_boxed: Vec<dyn Debug> = vec![box: Box::new(1) as _, Box::new("foo") as _, Box::new(true) as _];
|
|
//! let vec_unsized: Vec<dyn Debug> = vec![unsized: 1, "foo", true];
|
|
//! # assert_eq!(format!("{:?}", vec), "[1, 2, 3]");
|
|
//! # assert_eq!(format!("{:?}", vec_boxed), r#"[1, "foo", true]"#);
|
|
//! # assert_eq!(format!("{:?}", vec_unsized), r#"[1, "foo", true]"#);
|
|
//! ```
|
|
//!
|
|
//! a `Vec` can be pushed to with [`Vec::push`]:
|
|
//!
|
|
//! ```
|
|
//! # use dyn_vec::prelude::{*, vec};
|
|
//! let mut vec: Vec<i32> = vec![];
|
|
//! vec.push(1);
|
|
//! vec.push(2);
|
|
//! vec.push(3);
|
|
//! # assert_eq!(format!("{:?}", vec), "[1, 2, 3]");
|
|
//! ```
|
|
//!
|
|
//! ...and with [`push_box`] and [`push_unsize`]
|
|
//!
|
|
//! ```
|
|
//! # use dyn_vec::prelude::{*, vec};
|
|
//! # use std::fmt::Debug;
|
|
//! let mut vec: Vec<dyn Debug> = vec![];
|
|
//! vec.push_box(Box::new(1));
|
|
//! vec.push_box(Box::new("foo"));
|
|
//! vec.push_box(Box::new(true));
|
|
//!
|
|
//! vec.push_unsize(2);
|
|
//! vec.push_unsize("bar");
|
|
//! vec.push_unsize(false);
|
|
//! # assert_eq!(format!("{:?}", vec), r#"[1, "foo", true, 2, "bar", false]"#);
|
|
//! ```
|
|
//!
|
|
//! # Data Layout
|
|
//!
|
|
//! ```text
|
|
//! Vec<T>
|
|
//! ┌────┬────┬────┬────┐
|
|
//! │ptr │len │cap │end │
|
|
//! └─┬──┴────┴─┬──┴─┬──┘
|
|
//! │ │ │
|
|
//! │ └────┼───────────────────────────────────────────────┐
|
|
//! ┌─┘ └───────────────────┐ │
|
|
//! │ │ │
|
|
//! ▼ ▼ ▼
|
|
//! ┌────┬────┬─────┬──────────┬───┬─────┬───────────────┬───┬───┬───┐
|
|
//! │pad │elem│pad │elem │pad│elem │ │ptr│ptr│ptr│
|
|
//! └────┴────┴─────┴──────────┴───┴─────┴───────────────┴─┬─┴─┬─┴─┬─┘
|
|
//! ▲ ▲ ▲ ▲ │ │ │ ▲
|
|
//! │ │ │ └───────────────────────┘ │ │ │
|
|
//! │ │ └──────────────────────────────────────────┘ │ │
|
|
//! │ └─────────────────────────────────────────────────────────┘ │
|
|
//! │ │
|
|
//! └─ aligned to 8 also aligned to 8 ─┘
|
|
//! ```
|
|
//!
|
|
//! [`push_box`]: Vec::push_box
|
|
//! [`push_unsize`]: Vec::push_unsize
|
|
|
|
#![feature(ptr_metadata)]
|
|
#![feature(layout_for_ptr)]
|
|
#![feature(coerce_unsized)]
|
|
|
|
#[cfg(test)]
|
|
mod test;
|
|
|
|
// Too small to put in its own file
|
|
pub mod prelude {
|
|
pub use super::{Vec, vec};
|
|
}
|
|
|
|
use core::panic;
|
|
use std::{
|
|
ptr::{NonNull, self, drop_in_place, metadata},
|
|
marker::PhantomData,
|
|
alloc::{alloc, Layout, dealloc},
|
|
mem::{size_of, size_of_val, align_of_val, self, size_of_val_raw},
|
|
slice,
|
|
ops::{CoerceUnsized, Index, IndexMut}
|
|
};
|
|
|
|
/// Copy `size` bytes of memory from `src` to `dst`.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// `src` must be valid for reads, `dst` must be valid for writes, etc, you get the idea.
|
|
// TODO: inline me! i didnt realize it was avaliable as `copy_to` until the code was mostly complete.
|
|
unsafe fn memcpy(src: *const u8, dst: *mut u8, size: usize) {
|
|
src.copy_to(dst, size);
|
|
}
|
|
|
|
fn align_up<T: ?Sized>(ptr: *const T, align: usize) -> *const T {
|
|
let (mut data, meta) = ptr.to_raw_parts();
|
|
data = ((data as usize + align - 1) & !(align - 1)) as _;
|
|
ptr::from_raw_parts(data, meta)
|
|
}
|
|
|
|
fn align_up_mut<T: ?Sized>(ptr: *mut T, align: usize) -> *mut T {
|
|
align_up(ptr as _, align) as _
|
|
}
|
|
|
|
/// A heap allocated, dynamic length collection of `?Sized` elements.
|
|
///
|
|
/// See [`std::vec::Vec`] (the standard library `Vec` type) for more information.
|
|
pub struct Vec<T: ?Sized> {
|
|
ptr: NonNull<u8>,
|
|
len: usize,
|
|
capacity: usize,
|
|
end_ptr: NonNull<u8>,
|
|
_phantom: PhantomData<T>
|
|
}
|
|
|
|
// keeps this file cleaner
|
|
mod impls;
|
|
mod iter;
|
|
pub use iter::*;
|
|
|
|
/// The extra data stored at the end of the allocation.
|
|
type Extra<T> = *const T;
|
|
|
|
impl<T: ?Sized> Vec<T> {
|
|
/// Creates a new, empty `Vec`.
|
|
pub fn new() -> Self {
|
|
let ptr = NonNull::dangling();
|
|
Self {
|
|
ptr,
|
|
len: 0,
|
|
capacity: 0,
|
|
end_ptr: ptr,
|
|
_phantom: PhantomData
|
|
}
|
|
}
|
|
|
|
/// Creates a new `Vec` with the given capacity (measured in bytes).
|
|
pub fn with_capacity(cap: usize) -> Self {
|
|
let ptr = NonNull::new(unsafe { alloc(Layout::from_size_align(cap, 8).unwrap()) }).unwrap();
|
|
Self {
|
|
ptr,
|
|
len: 0,
|
|
capacity: cap,
|
|
end_ptr: ptr,
|
|
_phantom: PhantomData
|
|
}
|
|
}
|
|
|
|
/// Appends an element to the end of the `Vec`.
|
|
///
|
|
/// Only avaliable if `T: Sized`.
|
|
pub fn push(&mut self, v: T) where T: Sized {
|
|
unsafe { self.push_raw(&v) }
|
|
mem::forget(v);
|
|
}
|
|
|
|
/// Appends an (possibly unsized) boxed element to the end of the `Vec`.
|
|
pub fn push_box(&mut self, v: Box<T>) {
|
|
let ptr = Box::into_raw(v);
|
|
unsafe {
|
|
self.push_raw(ptr);
|
|
dealloc(ptr.cast(), Layout::for_value_raw(ptr));
|
|
}
|
|
}
|
|
|
|
/// Appends a sized element of type `U` to the end of the `Vec`, given that it can be `CoerceUnsized` to a `T`.
|
|
pub fn push_unsize<U>(&mut self, v: U) where for<'a> &'a U: CoerceUnsized<&'a T> {
|
|
let v_unsized: &T = &v;
|
|
unsafe { self.push_raw(v_unsized) };
|
|
mem::forget(v);
|
|
}
|
|
|
|
unsafe fn push_raw(&mut self, v: *const T) {
|
|
let size = size_of_val(&*v);
|
|
|
|
if !self.will_fit(&*v) {
|
|
// oh no! allocation too small!
|
|
|
|
// make sure we have enough space for a new element, but also space for future elements
|
|
// this bit is tricky, we must make sure we have enough space for padding too, so its probably UB somehow
|
|
// FIXME: ^^^
|
|
let new_alloc_size = self.capacity * 2 + size * 2 + size_of::<Extra<T>>();
|
|
self.realloc(new_alloc_size);
|
|
}
|
|
|
|
self.push_raw_unchecked(v);
|
|
}
|
|
|
|
/// Given an element, returns a pointer to where it would be written if it was pushed, assuming no reallocation is needed.
|
|
///
|
|
/// The pointer will be aligned, but writing to it may overwrite data belonging to the Vec.
|
|
/// To check for this, call `will_fit`.
|
|
/// In addition, the extra data for the element must be set using `set_extra_from_ptr`.
|
|
fn get_next_elem_ptr(&self, v: &T) -> *mut u8 {
|
|
align_up_mut(self.end_ptr.as_ptr(), align_of_val(v))
|
|
}
|
|
|
|
/// Checks if a given element will fill in the `Vec` without reallocations.
|
|
pub fn will_fit(&self, v: &T) -> bool {
|
|
let remaining_space = self.get_ptr_to_extra(self.len) as usize - self.end_ptr.as_ptr() as usize;
|
|
let needed_space = size_of_val(v) + size_of::<Extra<T>>();
|
|
remaining_space >= needed_space
|
|
}
|
|
|
|
unsafe fn push_raw_unchecked(&mut self, v: *const T) {
|
|
let size = size_of_val(&*v);
|
|
let dest = self.get_next_elem_ptr(&*v); // this is mentioned by the `// SAFETY:` in `as_slice_flatten`
|
|
|
|
memcpy(v.cast(), dest, size);
|
|
|
|
let new_ptr = ptr::from_raw_parts::<T>(dest.cast(), metadata(v));
|
|
self.set_extra_from_ptr(self.len, new_ptr);
|
|
|
|
self.end_ptr = NonNull::new_unchecked(dest.wrapping_add(size));
|
|
self.len += 1;
|
|
}
|
|
|
|
unsafe fn realloc(&mut self, size: usize) {
|
|
let layout = Layout::from_size_align_unchecked(size, 8).pad_to_align();
|
|
if self.capacity == 0 {
|
|
// will panic if OOM
|
|
self.ptr = NonNull::new(alloc(layout)).unwrap();
|
|
|
|
self.end_ptr = self.ptr;
|
|
} else {
|
|
// cannot use realloc here
|
|
|
|
let new_alloc = NonNull::new(alloc(layout)).unwrap();
|
|
|
|
// data
|
|
let mut ptr = new_alloc.as_ptr();
|
|
for i in 0..self.len {
|
|
let v = self.get_unchecked(i);
|
|
|
|
let size = size_of_val(v);
|
|
ptr = align_up_mut(ptr, align_of_val(v));
|
|
memcpy(v as *const _ as _, ptr, size);
|
|
let meta = self.get_ptr(i).to_raw_parts().1;
|
|
self.set_extra_from_ptr(i, ptr::from_raw_parts(ptr.cast(), meta));
|
|
ptr = ptr.wrapping_add(size);
|
|
}
|
|
self.end_ptr = NonNull::new_unchecked(ptr);
|
|
|
|
// metadata
|
|
let meta_src = self.get_ptr_to_extra(self.len);
|
|
let meta_dst = {
|
|
let current_alloc_end = self.ptr.as_ptr().wrapping_add(self.capacity);
|
|
let new_alloc_end = new_alloc.as_ptr().wrapping_add(layout.size());
|
|
let meta_len = current_alloc_end as usize - meta_src as usize;
|
|
new_alloc_end.wrapping_sub(meta_len)
|
|
};
|
|
let meta_size = self.len * size_of::<Extra<T>>();
|
|
memcpy(meta_src.cast(), meta_dst, meta_size);
|
|
|
|
dealloc(self.ptr.as_ptr(), Layout::from_size_align_unchecked(self.capacity, 8));
|
|
|
|
self.ptr = new_alloc;
|
|
}
|
|
|
|
self.capacity = layout.size();
|
|
}
|
|
|
|
/// for internal use
|
|
///
|
|
/// NOTE: 1-indexed, to allow getting a pointer to the end of the alloc easily
|
|
fn get_ptr_to_extra(&self, index: usize) -> *mut Extra<T> {
|
|
self.ptr.as_ptr()
|
|
.wrapping_add(self.capacity)
|
|
.cast::<Extra<T>>()
|
|
.wrapping_sub(index)
|
|
}
|
|
|
|
unsafe fn set_extra_from_ptr(&self, index: usize, ptr: *const T) {
|
|
self.get_ptr_to_extra(index + 1).write(ptr);
|
|
}
|
|
|
|
unsafe fn get_ptr(&self, index: usize) -> *const T {
|
|
*self.get_ptr_to_extra(index + 1)
|
|
}
|
|
|
|
pub fn get(&self, index: usize) -> Option<&T> {
|
|
if index < self.len {
|
|
Some(unsafe { self.get_unchecked(index) })
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
|
|
&*self.get_ptr(index)
|
|
}
|
|
|
|
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
|
|
if index < self.len {
|
|
Some(unsafe { self.get_unchecked_mut(index) })
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
|
|
&mut *(self.get_ptr(index) as *mut _)
|
|
}
|
|
|
|
/// Returns the length of the `Vec`, which is how many items it contains.
|
|
pub fn len(&self) -> usize {
|
|
self.len
|
|
}
|
|
|
|
/// Returns the capacity, which is the size of the allocation in bytes.
|
|
pub fn capacity(&self) -> usize {
|
|
self.capacity
|
|
}
|
|
|
|
/// Returns a pointer to the allocation of the `Vec`.
|
|
pub fn as_ptr(&self) -> *const u8 {
|
|
self.ptr.as_ptr()
|
|
}
|
|
|
|
/// Returns a mutable pointer to the allocation of the `Vec`.
|
|
pub fn as_mut_ptr(&mut self) -> *mut u8 {
|
|
self.ptr.as_ptr()
|
|
}
|
|
|
|
/// Iterates over the `Vec` by-ref.
|
|
pub fn iter(&self) -> Iter<T> {
|
|
Iter::new(self)
|
|
}
|
|
|
|
/// Iterates over the `Vec` by-mut.
|
|
pub fn iter_mut(&mut self) -> IterMut<T> {
|
|
IterMut::new(self)
|
|
}
|
|
|
|
/// Converts a `Vec<T: Sized>` into a `Vec<U: ?Sized>`, given that `T` can be `CoerceUnsized` into `U`.
|
|
pub fn unsize<U: ?Sized>(self) -> Vec<U> where for<'a> &'a T: CoerceUnsized<&'a U> {
|
|
let new_vec = Vec::<U> {
|
|
ptr: self.ptr,
|
|
len: self.len,
|
|
capacity: self.capacity,
|
|
end_ptr: self.end_ptr,
|
|
_phantom: PhantomData,
|
|
};
|
|
|
|
if size_of::<Extra<U>>() > size_of::<Extra<T>>() {
|
|
// new extra larger than old extra, must go from back to front
|
|
|
|
for i in (0..self.len).rev() {
|
|
let current = unsafe { &*self.get_ptr(i) };
|
|
unsafe { new_vec.set_extra_from_ptr(i, current as &U) }
|
|
}
|
|
} else {
|
|
// new extra smaller or same size as old extra, must go from front to back
|
|
|
|
for i in 0..self.len {
|
|
let current = unsafe { &*self.get_ptr(i) };
|
|
unsafe { new_vec.set_extra_from_ptr(i, current as &U) }
|
|
}
|
|
}
|
|
|
|
mem::forget(self);
|
|
new_vec
|
|
}
|
|
|
|
unsafe fn dealloc(&self) {
|
|
if self.capacity != 0 {
|
|
dealloc(self.ptr.as_ptr(), Layout::from_size_align_unchecked(self.capacity, 8));
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Vec<[T]> {
|
|
pub fn as_slice_flatten(&self) -> &[T] {
|
|
assert!(self.len > 0);
|
|
|
|
// SAFETY: the slices should be contiguous by the logic of `push_raw_unchecked`
|
|
unsafe {
|
|
slice::from_raw_parts(self.get_ptr(0).to_raw_parts().0 as _, {
|
|
let start = self.get_ptr(0).to_raw_parts().0 as usize;
|
|
let end = self.end_ptr.as_ptr() as usize;
|
|
(end - start) / size_of::<T>() // integer division!
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn as_mut_slice_flatten(&mut self) -> &mut [T] {
|
|
assert!(self.len > 0);
|
|
|
|
// SAFETY: the slices should be contiguous by the logic of `push_raw_unchecked`
|
|
unsafe {
|
|
slice::from_raw_parts_mut(self.get_ptr(0).to_raw_parts().0 as _, {
|
|
let start = self.get_ptr(0).to_raw_parts().0 as usize;
|
|
let end = self.end_ptr.as_ptr() as usize;
|
|
(end - start) / size_of::<T>() // integer division!
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: ?Sized> Drop for Vec<T> {
|
|
fn drop(&mut self) {
|
|
unsafe {
|
|
for i in 0..self.len {
|
|
println!("dropping {}", i);
|
|
drop_in_place(self.get_unchecked_mut(i));
|
|
}
|
|
self.dealloc();
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: ?Sized> Index<usize> for Vec<T> {
|
|
type Output = T;
|
|
|
|
#[track_caller]
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
match self.get(index) {
|
|
Some(v) => v,
|
|
None => panic!("index out of bounds: the len is {} but the index is {}", self.len, index),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: ?Sized> IndexMut<usize> for Vec<T> {
|
|
#[track_caller]
|
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
|
let len = self.len;
|
|
match self.get_mut(index) {
|
|
Some(v) => v,
|
|
None => panic!("index out of bounds: the len is {} but the index is {}", len, index),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Creates a [`Vec`].
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// # use dyn_vec::prelude::{vec, Vec};
|
|
/// # use std::fmt::Debug;
|
|
/// let vec1: Vec<dyn Debug> = vec![1, 2, 3].unsize();
|
|
/// let vec2: Vec<dyn Debug> = vec![box:
|
|
/// Box::new(1) as _,
|
|
/// Box::new(String::from("foo")) as _,
|
|
/// Box::new(true) as _
|
|
/// ];
|
|
/// let vec3: Vec<dyn Debug> = vec![unsized: 1, String::from("foo"), true];
|
|
/// ```
|
|
#[macro_export]
|
|
macro_rules! vec {
|
|
() => {
|
|
$crate::Vec::new();
|
|
};
|
|
(box: $($elem:expr),+ $(,)?) => {{
|
|
let mut vec = $crate::Vec::new();
|
|
$(vec.push_box($elem);)+
|
|
vec
|
|
}};
|
|
(unsized: $($elem:expr),+ $(,)?) => {{
|
|
let mut vec = $crate::Vec::new();
|
|
$(vec.push_unsize($elem);)+
|
|
vec
|
|
}};
|
|
($elem:expr; $n:expr) => {
|
|
compile_error!("dyn_vec::vec![T; N] is currently not supported");
|
|
};
|
|
($($elem:expr),+ $(,)?) => {{
|
|
let mut vec = $crate::Vec::new();
|
|
$(vec.push($elem);)+
|
|
vec
|
|
}};
|
|
} |