1
0
Fork 0
forked from wry/wry

autocommit 2022-02-14 21:13:42 CET

This commit is contained in:
Julian Orth 2022-02-14 21:13:42 +01:00
parent 9b8e1ac29f
commit da6b29f138
44 changed files with 5903 additions and 364 deletions

View file

@ -2,6 +2,7 @@ use crate::utils::ptr_ext::PtrExt;
use crate::NumCell;
use std::cell::Cell;
use std::fmt::{Debug, Formatter};
use std::mem;
use std::ops::Deref;
use std::ptr::NonNull;
@ -153,6 +154,7 @@ impl<T> Drop for RevLinkedListIter<T> {
}
}
#[repr(transparent)]
pub struct LinkedNode<T> {
data: NonNull<NodeData<T>>,
}
@ -164,13 +166,14 @@ impl<T: Debug> Debug for LinkedNode<T> {
}
impl<T> Deref for LinkedNode<T> {
type Target = T;
type Target = NodeRef<T>;
fn deref(&self) -> &Self::Target {
unsafe { self.data.as_ref().data.as_ref().unwrap_unchecked() }
unsafe { mem::transmute(self) }
}
}
#[repr(transparent)]
pub struct NodeRef<T> {
data: NonNull<NodeData<T>>,
}
@ -215,10 +218,12 @@ impl<T> NodeRef<T> {
unsafe { append(self.data, t) }
}
pub fn prev(&self) -> Option<NodeRef<T>> {
fn peer<F>(&self, peer: F) -> Option<NodeRef<T>>
where F: FnOnce(&NodeData<T>) -> &Cell<NonNull<NodeData<T>>>,
{
unsafe {
let data = self.data.as_ref();
let other = data.prev.get();
let other = peer(&data).get();
if other.as_ref().data.is_some() {
other.as_ref().rc.fetch_add(1);
Some(NodeRef { data: other })
@ -227,6 +232,14 @@ impl<T> NodeRef<T> {
}
}
}
pub fn prev(&self) -> Option<NodeRef<T>> {
self.peer(|d| &d.prev)
}
pub fn next(&self) -> Option<NodeRef<T>> {
self.peer(|d| &d.next)
}
}
struct NodeData<T> {

View file

@ -11,3 +11,4 @@ pub mod ptr_ext;
pub mod queue;
pub mod smallmap;
pub mod vec_ext;
pub mod stack;

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

@ -0,0 +1,28 @@
use std::cell::UnsafeCell;
use crate::utils::ptr_ext::MutPtrExt;
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()
}
}
}