From 114b75703aab558995a250768de47378c57349d9 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Tue, 23 Aug 2022 21:29:16 -0700 Subject: Add queue structure --- src/util/node.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/util/node.rs (limited to 'src/util/node.rs') 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 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 Send for Node<'_, T> {} + +impl 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); + } +} -- cgit v1.2.1