use core::mem::ManuallyDrop; #[cfg_attr(target_arch = "x86", path = "treiber/cas.rs")] #[cfg_attr(arm_llsc, path = "treiber/llsc.rs")] mod impl_; pub use impl_::{AtomicPtr, NonNullPtr}; pub struct Stack where N: Node, { top: AtomicPtr, } impl Stack where N: Node, { pub const fn new() -> Self { Self { top: AtomicPtr::null(), } } /// # Safety /// - `node` must be a valid pointer /// - aliasing rules must be enforced by the caller. e.g, the same `node` may not be pushed more than once pub unsafe fn push(&self, node: NonNullPtr) { impl_::push(self, node) } pub fn try_pop(&self) -> Option> { impl_::try_pop(self) } } pub trait Node: Sized { type Data; fn next(&self) -> &AtomicPtr; fn next_mut(&mut self) -> &mut AtomicPtr; } pub union UnionNode { next: ManuallyDrop>>, pub data: ManuallyDrop, } impl Node for UnionNode { type Data = T; fn next(&self) -> &AtomicPtr { unsafe { &self.next } } fn next_mut(&mut self) -> &mut AtomicPtr { unsafe { &mut self.next } } } pub struct StructNode { pub next: ManuallyDrop>>, pub data: ManuallyDrop, } impl Node for StructNode { type Data = T; fn next(&self) -> &AtomicPtr { &self.next } fn next_mut(&mut self) -> &mut AtomicPtr { &mut self.next } } #[cfg(test)] mod tests { use core::mem; use super::*; #[test] fn node_is_never_zero_sized() { struct Zst; assert_ne!(mem::size_of::>(), 0); } }