1
0
Fork 0
forked from wry/wry

autocommit 2022-02-27 01:35:49 CET

This commit is contained in:
Julian Orth 2022-02-27 01:35:49 +01:00
parent 6e466360a8
commit db88f2db42
26 changed files with 2696 additions and 6 deletions

22
src/utils/aligned.rs Normal file
View file

@ -0,0 +1,22 @@
use uapi::{Packed, Pod};
#[repr(C, align(8))]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AlignedI64(pub i64);
unsafe impl Pod for AlignedI64 {}
unsafe impl Packed for AlignedI64 {}
#[repr(C, align(8))]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AlignedU64(pub u64);
unsafe impl Pod for AlignedU64 {}
unsafe impl Packed for AlignedU64 {}
#[repr(C, align(8))]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct AlignedF64(pub f64);
unsafe impl Pod for AlignedF64 {}
unsafe impl Packed for AlignedF64 {}

15
src/utils/hex.rs Normal file
View file

@ -0,0 +1,15 @@
pub fn to_hex(b: &str) -> String {
let mut s = String::with_capacity(b.len() * 2);
for &b in b.as_bytes() {
s.push(nibble_to_hex(b >> 4) as char);
s.push(nibble_to_hex(b & 7) as char);
}
s
}
fn nibble_to_hex(n: u8) -> u8 {
match n {
n @ 0..=9 => b'0' + n,
n => b'a' + n,
}
}

View file

@ -1,3 +1,4 @@
pub mod aligned;
pub mod array;
pub mod asyncevent;
pub mod bitflags;
@ -6,6 +7,7 @@ pub mod clonecell;
pub mod copyhashmap;
pub mod debug_fn;
pub mod errorfmt;
pub mod hex;
pub mod linkedlist;
pub mod numcell;
pub mod ptr_ext;
@ -14,3 +16,4 @@ pub mod smallmap;
pub mod stack;
pub mod tri;
pub mod vec_ext;
pub mod vecstorage;

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

@ -0,0 +1,74 @@
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
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> {
#[allow(dead_code)]
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> {
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()
}
}