summaryrefslogtreecommitdiff
path: root/src/util/node.rs
diff options
context:
space:
mode:
authorChristian Cunningham <c@localhost>2022-08-23 21:29:16 -0700
committerChristian Cunningham <c@localhost>2022-08-23 21:29:16 -0700
commit114b75703aab558995a250768de47378c57349d9 (patch)
tree3da0fb3595b25127e042377f3db08fdf4f796614 /src/util/node.rs
parent052986797d04475490a07fb3846a04206b60fbb0 (diff)
Add queue structure
Diffstat (limited to 'src/util/node.rs')
-rw-r--r--src/util/node.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/util/node.rs b/src/util/node.rs
new file mode 100644
index 0000000..a22e395
--- /dev/null
+++ b/src/util/node.rs
@@ -0,0 +1,47 @@
+//! # Node type
+//!
+//! Provides a type that holds data and a pointer to the next structure.
+use core::fmt;
+use core::fmt::{Debug, Formatter};
+
+/// # Node
+///
+/// Encapsulates a data element and a pointer to the next `Queue` item
+#[derive(Copy, Clone)]
+pub struct Node<'a, T: Sized> {
+ /// # Data
+ ///
+ /// The encapsulated data
+ pub data: T,
+ /// # Pointer to the next item
+ pub next: Option<*mut Node<'a, T>>,
+}
+
+impl<T> Node<'_, T> {
+ /// # Constructor
+ pub const fn new(data: T) -> Self {
+ Self { data, next: None }
+ }
+ /// # Get the inner data
+ ///
+ /// Returns a borrow of the underlying data.
+ pub fn inner(&mut self) -> &mut T {
+ &mut self.data
+ }
+ /// # Get pointer to inner data
+ pub fn ptr(&mut self) -> *mut u8 {
+ self.inner() as *mut T as *mut u8
+ }
+}
+
+/// # Sharing Thread Safety for Node
+unsafe impl<T> Send for Node<'_, T> {}
+
+impl<T: Debug> Debug for Node<'_, T> {
+ /// # Debug formatter for `Node`
+ ///
+ /// Output the encapsulated data
+ fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
+ return write!(f, "{:?}", self.data);
+ }
+}