use std::io;
use crate::fmt;
pub struct IoWriter<W>
where
W: io::Write,
{
writer: W,
}
impl<W> IoWriter<W>
where
W: io::Write,
{
pub fn new(writer: W) -> Self {
Self { writer }
}
pub fn as_formatter<'a>(&'a mut self, config: &'a fmt::Config) -> fmt::Formatter<'a> {
fmt::Formatter::new(self, config)
}
pub fn into_inner(self) -> W {
self.writer
}
}
impl<W> core::fmt::Write for IoWriter<W>
where
W: io::Write,
{
#[inline(always)]
fn write_char(&mut self, c: char) -> core::fmt::Result {
self.writer
.write_all(c.encode_utf8(&mut [0; 4]).as_bytes())
.map_err(|_| core::fmt::Error)
}
#[inline(always)]
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.writer
.write_all(s.as_bytes())
.map_err(|_| core::fmt::Error)
}
}
impl<W> fmt::Write for IoWriter<W>
where
W: io::Write,
{
#[inline(always)]
fn write_line(&mut self, config: &fmt::Config) -> fmt::Result {
self.writer
.write_all(config.newline.as_bytes())
.map_err(|_| core::fmt::Error)
}
}