2021-05-08 14:21:53 +03:00
|
|
|
module io
|
|
|
|
|
|
|
|
// new_multi_writer returns a Writer that writes to all writers. The write
|
|
|
|
// function of the returned Writer writes to all writers of the MultiWriter,
|
|
|
|
// returns the length of bytes written, and if any writer fails to write the
|
|
|
|
// full length an error is returned and writing to other writers stops, and if
|
|
|
|
// any writer returns an error the error is returned immediately and writing to
|
|
|
|
// other writers stops.
|
2021-05-09 09:21:08 +03:00
|
|
|
pub fn new_multi_writer(writers ...Writer) Writer {
|
2021-05-08 14:21:53 +03:00
|
|
|
return &MultiWriter{
|
|
|
|
writers: writers
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MultiWriter writes to all its writers.
|
|
|
|
pub struct MultiWriter {
|
2021-05-13 13:06:42 +03:00
|
|
|
pub mut:
|
2021-05-08 14:21:53 +03:00
|
|
|
writers []Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes to all writers of the MultiWriter. Returns the length of bytes
|
|
|
|
// written. If any writer fails to write the full length an error is returned
|
|
|
|
// and writing to other writers stops. If any writer returns an error the error
|
|
|
|
// is returned immediately and writing to other writers stops.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (mut m MultiWriter) write(buf []u8) !int {
|
2021-05-13 13:06:42 +03:00
|
|
|
for mut w in m.writers {
|
2022-10-16 09:28:57 +03:00
|
|
|
n := w.write(buf)!
|
2021-05-08 14:21:53 +03:00
|
|
|
if n != buf.len {
|
|
|
|
return error('io: incomplete write to writer of MultiWriter')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.len
|
|
|
|
}
|