Macro def_node

Source
macro_rules! def_node {
    ($(#[$meta:meta])* $vis:vis struct $name:ident($type:ty); $($t:tt)*) => { ... };
    ($(#[$meta:meta])* $vis:vis struct $name:ident<$gen:ident>($type:ty); $($t:tt)*) => { ... };
    () => { ... };
}
Expand description

A macro for create a node type that can be used in List.

§Syntax

def_node! {
/// A node with usize value.
[pub] struct UsizedNode(usize);
/// A node with generic inner type.
[pub] struct WrapperNode<T>(T);
}

§Example

use linked_list_r4l::{def_node, List};

def_node!{
    /// An example Node with usize
    struct ExampleNode(usize);
    /// An example Node with generic Inner type and pub(crate)
    pub(crate) struct NativeGenericNode(usize);
    /// An example Node with generic Inner type and pub vis
    pub struct GenericNode<T>(T);
}

let node1 = Box::new(ExampleNode::new(0));
let node2 = Box::new(ExampleNode::new(1));
let mut list =  List::<Box<ExampleNode>>::new();

list.push_back(node1);
list.push_back(node2);

for (i,e) in list.iter().enumerate() {
    assert!(*e.inner() == i);
}

let node1 = list.pop_front().unwrap();
let node2 = list.pop_front().unwrap();

assert!(node1.into_inner() == 0);
assert!(node2.into_inner() == 1);
assert!(list.pop_front().is_none());

let node1 = Box::new(GenericNode::new(0));
let node2 = Box::new(GenericNode::new(1));

let mut list =  List::<Box<GenericNode<usize>>>::new();

list.push_back(node1);
list.push_back(node2);

for (i,e) in list.iter().enumerate() {
    assert!(*e.inner() == i);
}