summaryrefslogtreecommitdiff
path: root/src/util/node.rs
blob: a22e395ee5a5b29cec77cadee70ccd1f059b0a24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
    }
}