1
0
Fork 0
forked from wry/wry

all: split reusable components into workspace crates

This commit is contained in:
kossLAN 2026-05-29 09:14:53 -04:00
parent 2a079ed800
commit 657e7ce2f7
No known key found for this signature in database
225 changed files with 7422 additions and 17602 deletions

105
utils/src/vecstorage.rs Normal file
View file

@ -0,0 +1,105 @@
use std::{
mem::ManuallyDrop,
ops::{Deref, DerefMut},
};
macro_rules! assert_size_eq {
($t:ty, $u:ty) => {{
struct AssertEqSize<T, U>(std::marker::PhantomData<T>, std::marker::PhantomData<U>);
impl<T, U> AssertEqSize<T, U> {
const VAL: usize = {
if std::mem::size_of::<T>() != std::mem::size_of::<U>() {
panic!("Types have different size");
}
1
};
}
let _ = AssertEqSize::<$t, $u>::VAL;
}};
}
macro_rules! assert_align_eq {
($t:ty, $u:ty) => {{
struct AssertEqAlign<T, U>(std::marker::PhantomData<T>, std::marker::PhantomData<U>);
impl<T, U> AssertEqAlign<T, U> {
const VAL: usize = {
if std::mem::align_of::<T>() != std::mem::align_of::<U>() {
panic!("Types have different alignment");
}
1
};
}
let _ = AssertEqAlign::<$t, $u>::VAL;
}};
}
pub struct VecStorage<T> {
ptr: *mut T,
cap: usize,
}
impl<T> Default for VecStorage<T> {
fn default() -> Self {
let mut v = ManuallyDrop::new(vec![]);
Self {
ptr: v.as_mut_ptr(),
cap: v.capacity(),
}
}
}
impl<T> VecStorage<T> {
pub fn take<'a>(&'a mut self) -> RealizedVec<'a, T, T> {
self.take_as()
}
pub fn take_as<'a, U>(&'a mut self) -> RealizedVec<'a, T, U> {
assert_size_eq!(T, U);
assert_align_eq!(T, U);
unsafe {
RealizedVec {
vec: ManuallyDrop::new(self.to_vector()),
storage: self,
}
}
}
unsafe fn to_vector<U>(&mut self) -> Vec<U> {
unsafe { Vec::from_raw_parts(self.ptr as _, 0, self.cap) }
}
}
impl<T> Drop for VecStorage<T> {
fn drop(&mut self) {
unsafe {
drop(self.to_vector::<T>());
}
}
}
pub struct RealizedVec<'a, T, U> {
vec: ManuallyDrop<Vec<U>>,
storage: &'a mut VecStorage<T>,
}
impl<'a, T, U> Drop for RealizedVec<'a, T, U> {
fn drop(&mut self) {
self.vec.clear();
self.storage.ptr = self.vec.as_mut_ptr() as _;
self.storage.cap = self.vec.capacity();
}
}
impl<'a, T, U> Deref for RealizedVec<'a, T, U> {
type Target = Vec<U>;
fn deref(&self) -> &Self::Target {
self.vec.deref()
}
}
impl<'a, T, U> DerefMut for RealizedVec<'a, T, U> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.vec.deref_mut()
}
}