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

49
utils/src/stack.rs Normal file
View file

@ -0,0 +1,49 @@
use {
crate::{
clonecell::UnsafeCellCloneSafe,
ptr_ext::{MutPtrExt, PtrExt},
},
std::{cell::UnsafeCell, mem},
};
pub struct Stack<T> {
vec: UnsafeCell<Vec<T>>,
}
impl<T> Default for Stack<T> {
fn default() -> Self {
Self {
vec: Default::default(),
}
}
}
impl<T> Stack<T> {
pub fn push(&self, v: T) {
unsafe {
self.vec.get().deref_mut().push(v);
}
}
pub fn pop(&self) -> Option<T> {
unsafe { self.vec.get().deref_mut().pop() }
}
pub fn to_vec(&self) -> Vec<T>
where
T: UnsafeCellCloneSafe,
{
unsafe {
let v = self.vec.get().deref();
(*v).clone()
}
}
pub fn take(&self) -> Vec<T> {
unsafe { mem::take(self.vec.get().deref_mut()) }
}
pub fn len(&self) -> usize {
unsafe { self.vec.get().deref().len() }
}
}