use core::fmt;
use crate::writer::Writer;
#[repr(transparent)]
pub struct ToFmt<V: ?Sized>(V);
impl<V: sval::Value> ToFmt<V> {
pub fn new(value: V) -> ToFmt<V> {
ToFmt(value)
}
}
impl<V: sval::Value + ?Sized> ToFmt<V> {
pub fn new_borrowed<'a>(value: &'a V) -> &'a ToFmt<V> {
unsafe { &*(value as *const _ as *const ToFmt<V>) }
}
}
pub fn stream_to_fmt(fmt: &mut fmt::Formatter, v: impl sval::Value) -> fmt::Result {
v.stream(&mut Writer::new(fmt)).map_err(|_| fmt::Error)
}
impl<V: sval::Value> fmt::Debug for ToFmt<V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl<V: sval::Value> fmt::Display for ToFmt<V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match stream_to_fmt(f, &self.0) {
Ok(()) => Ok(()),
Err(e) => write!(f, "<{}>", e),
}
}
}