use super::TeeWriter;
#[cfg(feature = "io-std")]
impl<This, Other> std::io::Write for TeeWriter<This, Other>
where
    This: std::io::Write,
    Other: std::io::Write,
{
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if self.offset > 0 {
            let offset = self.offset as usize;
            let offset = std::cmp::min(offset, buf.len());
            let buf = &buf[offset..];
            let written = self.this.write(buf)?;
            self.offset -= written as isize;
            return Ok(written);
        } else if self.offset < 0 {
            let offset = (-self.offset) as usize;
            let offset = std::cmp::min(offset, buf.len());
            let buf = &buf[offset..];
            let written = self.other.write(buf)?;
            self.offset += written as isize;
            return Ok(written);
        }
        let this_written = self.this.write(buf)?;
        let other_written = self.other.write(buf)?;

        if this_written == other_written {
            Ok(this_written)
        } else {
            let offset = this_written as isize - other_written as isize;
            self.offset += offset;
            let min = std::cmp::min(this_written, other_written);
            // next call should write to the one that wrote less
            Ok(min)
        }
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.this.flush()?;
        self.other.flush()
    }
}
