diff options
Diffstat (limited to 'src/util/node.rs')
| -rw-r--r-- | src/util/node.rs | 47 |
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); + } +} |
