use super::MyWrite;
pub struct Tee<W1, W2>
{
pub(crate) w1: W1,
pub(crate) w2: W2,
}
impl<W1, W2> MyWrite for Tee<W1, W2>
where
W1: MyWrite,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = W1::Error;
fn write(&mut self, buf: &[u8]) -> Result<(), <W1 as MyWrite>::Error> {
self.w2.write(buf).unwrap();
self.w1.write(buf)
}
fn flush(&mut self) -> Result<(), Self::Error> {
self.w2.flush().unwrap();
self.w1.flush()
}
}
#[cfg(feature = "std")]
impl<W1, W2> std::io::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
MyWrite::write(self, buf).unwrap();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
self.w2.flush().unwrap();
self.w1.flush().unwrap();
Ok(())
}
}
impl<W1, W2> core::fmt::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
fn write_str(&mut self, s: &str) -> core::fmt::Result {
self.write(s.as_bytes()).unwrap();
Ok(())
}
}
#[cfg(feature = "serde_cbor")]
impl<W1, W2> serde_cbor::ser::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = serde_cbor::error::Error;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf).unwrap();
Ok(())
}
}
#[cfg(feature = "with_minicbor")]
impl<W1, W2> minicbor::encode::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)
}
}
#[cfg(feature = "with_minicbor_0_19")]
impl<W1, W2> minicbor_0_19::encode::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)
}
}
#[cfg(feature = "with_minicbor_0_24")]
impl<W1, W2> minicbor_0_24::encode::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)
}
}
#[cfg(feature = "with_minicbor_0_25")]
impl<W1, W2> minicbor_0_25::encode::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)
}
}
#[cfg(feature = "with_ciborium")]
impl<W1, W2> ciborium_io::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
self.write(buf)
}
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
#[cfg(feature = "with_embedded_io_0_4")]
impl<W1, W2> embedded_io::Io for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
type Error = core::convert::Infallible;
}
#[cfg(feature = "with_embedded_io_0_4")]
impl<W1, W2> embedded_io::blocking::Write for Tee<W1, W2>
where
W1: MyWrite<Error = core::convert::Infallible>,
W2: MyWrite<Error = core::convert::Infallible>,
{
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
MyWrite::write(self, buf).map(|()| buf.len())
}
fn flush(&mut self) -> Result<(), Self::Error> {
MyWrite::flush(self)
}
}