v2 / vlib / io / multi_writer.v
33 lines · 30 sloc · 1.1 KB · f6844e97661d55094833fd8a0e44589aeca62ebf
Raw
1module io
2
3// new_multi_writer returns a Writer that writes to all writers. The write
4// function of the returned Writer writes to all writers of the MultiWriter,
5// returns the length of bytes written, and if any writer fails to write the
6// full length an error is returned and writing to other writers stops, and if
7// any writer returns an error the error is returned immediately and writing to
8// other writers stops.
9pub fn new_multi_writer(writers ...Writer) Writer {
10 return &MultiWriter{
11 writers: writers
12 }
13}
14
15// MultiWriter writes to all its writers.
16pub struct MultiWriter {
17pub mut:
18 writers []Writer
19}
20
21// write writes to all writers of the MultiWriter. Returns the length of bytes
22// written. If any writer fails to write the full length an error is returned
23// and writing to other writers stops. If any writer returns an error the error
24// is returned immediately and writing to other writers stops.
25pub fn (mut m MultiWriter) write(buf []u8) !int {
26 for mut w in m.writers {
27 n := w.write(buf)!
28 if n != buf.len {
29 return error('io: incomplete write to writer of MultiWriter')
30 }
31 }
32 return buf.len
33}
34