intrex 0.2.0

Intrusive collections with items addressed by indices
Documentation
use core::fmt;

use crate::node_data::{NodesDataLend, NodesDataLendGat};

use super::*;

/// A helper struct for debug-printing a [`ListAccessor`].
#[derive(Clone)]
struct FmtDebugList<Iter> {
    iter: Iter,
}

/// Formatting
impl<'head, Nodes, Index> ListAccessor<'head, Nodes, Index>
where
    Nodes: NodesLink<Index> + NodesDataLend<Index>,
    Index: PartialEq + Clone,
{
    /// Get a debug printer for all items in `self`.
    #[inline]
    pub fn debug(&self) -> impl fmt::Debug + Clone
    where
        for<'a> <Nodes as NodesDataLendGat<'a, Index>>::Data: fmt::Debug,
        Index: fmt::Debug,
    {
        FmtDebugList { iter: self.iter() }
    }

    /// Get a debug printer for all `Index`s in `self`.
    #[inline]
    pub fn debug_indices(&self) -> impl fmt::Debug + Clone
    where
        Index: fmt::Debug,
    {
        FmtDebugList {
            iter: self.indices(),
        }
    }

    /// Get a debug printer for all `Element`s in `self`.
    #[inline]
    pub fn debug_values(&self) -> impl fmt::Debug + Clone
    where
        for<'a> <Nodes as NodesDataLendGat<'a, Index>>::Data: fmt::Debug,
    {
        FmtDebugList {
            iter: self.values(),
        }
    }
}

impl<Iter> fmt::Debug for FmtDebugList<Iter>
where
    Iter: Iterator<Item: fmt::Debug> + Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter.clone()).finish()
    }
}