use core::fmt::{Debug, Display, Formatter, Result};
pub fn from_fn<F>(f: F) -> FromFn<F> {
FromFn(f)
}
pub struct FromFn<F>(F);
impl<F> Display for FromFn<F>
where
F: Fn(&mut Formatter) -> Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
(self.0)(f)
}
}
impl<F> Debug for FromFn<F>
where
F: Fn(&mut Formatter) -> Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
(self.0)(f)
}
}
pub fn repeat<T>(item: T, n: usize) -> Repeat<T> {
Repeat { item, n }
}
#[derive(Clone, Copy)]
pub struct Repeat<T> {
pub item: T,
pub n: usize,
}
impl<T: Display> Display for Repeat<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
for _ in 0..self.n {
self.item.fmt(f)?;
}
Ok(())
}
}
pub struct FormatWith<T, F> {
pub item: T,
pub with: F,
}
impl<T, F> Display for FormatWith<T, F>
where
F: Fn(&T, &mut Formatter) -> Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
(self.with)(&self.item, f)
}
}
impl<T, F> Debug for FormatWith<T, F>
where
F: Fn(&T, &mut Formatter) -> Result,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
(self.with)(&self.item, f)
}
}