#![no_std]
#![no_implicit_prelude]
extern crate core;
use core::cell::Cell;
use core::fmt::{Debug, Formatter, Result};
use core::iter::IntoIterator;
use core::marker::Sized;
use core::option::Option;
#[cfg(test)]
mod tests;
enum IterDebugStyle {
List,
Set,
}
pub struct IterDebug<T>(Cell<Option<T>>, IterDebugStyle);
impl<T> IterDebug<T> {
#[inline]
pub fn new(item: T) -> Self { Self(Cell::new(Option::Some(item)), IterDebugStyle::List) }
#[inline]
pub fn new_set(item: T) -> Self { Self(Cell::new(Option::Some(item)), IterDebugStyle::Set) }
#[inline]
pub fn try_into_inner(&self) -> Option<T> { self.0.take() }
}
impl<T> Debug for IterDebug<T>
where
T: IntoIterator,
T::Item: Debug,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
match (self.0.take(), &self.1) {
(Option::Some(value), IterDebugStyle::List) => f.debug_list().entries(value).finish(),
(Option::Some(value), IterDebugStyle::Set) => f.debug_set().entries(value).finish(),
(Option::None, _) => f.write_str("<consumed iterator>"),
}
}
}
pub struct KvIterDebug<T>(Cell<Option<T>>);
impl<T> KvIterDebug<T> {
#[inline]
pub fn new(item: T) -> Self { Self(Cell::new(Option::Some(item))) }
#[inline]
pub fn try_into_inner(&self) -> Option<T> { self.0.take() }
}
impl<T, K, V> Debug for KvIterDebug<T>
where
T: IntoIterator<Item = (K, V)>,
K: Debug,
V: Debug,
{
#[inline]
fn fmt(&self, f: &mut Formatter) -> Result {
match self.0.take() {
Option::Some(value) => f.debug_map().entries(value).finish(),
Option::None => f.write_str("<consumed iterator>"),
}
}
}
pub trait DebugIterator {
fn debug(self) -> IterDebug<Self>
where
Self: Sized;
fn debug_set(self) -> IterDebug<Self>
where
Self: Sized;
fn debug_map(self) -> KvIterDebug<Self>
where
Self: Sized;
}
impl<T> DebugIterator for T
where
T: IntoIterator,
T::Item: Debug,
{
#[inline]
fn debug(self) -> IterDebug<Self>
where
Self: Sized,
{
IterDebug::new(self)
}
#[inline]
fn debug_set(self) -> IterDebug<Self>
where
Self: Sized,
{
IterDebug::new_set(self)
}
fn debug_map(self) -> KvIterDebug<Self>
where
Self: Sized,
{
KvIterDebug::new(self)
}
}