From 02e0f810b94b035b9510cfed675b6d3228ed21d8 Mon Sep 17 00:00:00 2001 From: Herman Skogseth Date: Thu, 5 Sep 2024 20:59:43 +0200 Subject: [PATCH] Some cleanup --- src/stack.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/stack.rs b/src/stack.rs index a4b3f95..2a22068 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -27,9 +27,7 @@ impl SharedStack { } } - fn __push(&self, val: T) -> *mut Node { - let node = Box::into_raw(Box::new(Node::new(val))); - + fn __push(&self, node: *mut Node) { std::sync::atomic::fence(SeqCst); let mut old_top = self.top.load(Acquire); @@ -46,23 +44,23 @@ impl SharedStack { Err(current_top) => old_top = current_top, } } - - node } /// Push a new value onto the stack pub fn push(&self, val: T) { - let _ = self.__push(val); + let node = Box::into_raw(Box::new(Node::new(val))); + let _ = self.__push(node); } /// Push a new value onto the stack and return a reference to the value pub fn push_get(&self, val: T) -> &T { - let node = self.__push(val); + let node = Box::into_raw(Box::new(Node::new(val))); + self.__push(node); unsafe { &(*node).val } } /// Push a new value onto the stack and return a mutable reference to the value - pub fn push_get_mut(&mut self, val: T) -> &mut T { + pub fn push_mut(&mut self, val: T) { let node = Box::into_raw(Box::new(Node::new(val))); let old_top = self.top.load(Acquire); @@ -71,14 +69,13 @@ impl SharedStack { // This should always succeed let _exchange_result = self.top.compare_exchange(old_top, node, SeqCst, Relaxed); debug_assert!(_exchange_result.is_ok()); - - unsafe { &mut (*node).val } } pub fn push_stack(&self, stack: Self) { // TODO: This can be done much more efficiently for val in stack { - let _ = self.__push(val); + let node = Box::into_raw(Box::new(Node::new(val))); + let _ = self.__push(node); } } @@ -124,7 +121,7 @@ impl FromIterator for SharedStack { fn from_iter>(iter: I) -> Self { let mut stack = SharedStack::new(); for item in iter { - stack.push_get_mut(item); + stack.push_mut(item); } stack } @@ -133,7 +130,7 @@ impl FromIterator for SharedStack { impl Extend for SharedStack { fn extend>(&mut self, iter: I) { for item in iter { - self.push_get_mut(item); + self.push_mut(item); } } }