1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

io: add MultiWriter (#10021)

This commit is contained in:
Leigh McCulloch
2021-05-08 04:21:53 -07:00
committed by GitHub
parent 3dfbd2351b
commit 68c8709343
2 changed files with 99 additions and 0 deletions

33
vlib/io/multi_writer.v Normal file
View File

@ -0,0 +1,33 @@
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.
pub fn new_multi_writer(writers []Writer) Writer {
return &MultiWriter{
writers: writers
}
}
// MultiWriter writes to all its writers.
pub struct MultiWriter {
pub:
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.
pub fn (m MultiWriter) write(buf []byte) ?int {
for w in m.writers {
n := w.write(buf) ?
if n != buf.len {
return error('io: incomplete write to writer of MultiWriter')
}
}
return buf.len
}