pub struct Writer<W, S: StreamSerializer> { /* private fields */ }io only.Expand description
Writes values to a Write.
The values are serialized with a StreamSerializer (the serializer
of a format, which the configuration of the format creates with
config.writer(output)). The output of a value is written with
write_all, wrap the writer in a
BufWriter when writing many small values. If
the format supports it, the output of large values is written in parts
while they are serialized (see
set_buffer_limit).
Writers implement Serializer: serialize
is the same as write.
Implementations§
Source§impl<W: Write, S: StreamSerializer> Writer<W, S>
impl<W: Write, S: StreamSerializer> Writer<W, S>
Sourcepub fn new(writer: W, serializer: S) -> Writer<W, S>
pub fn new(writer: W, serializer: S) -> Writer<W, S>
Creates a writer.
To append to a stream that was written before, create the serializer with the state of the stream (for instance the number of values that were written).
Sourcepub fn set_context(&mut self, context: Context)
pub fn set_context(&mut self, context: Context)
Sets the context the values are serialized in.
The values of the context are the defaults of the extension values
of the state (see Context). It takes precedence over the
context of the serializer (which is the one of the configuration it
was created with), a context set by the callback of
write_with takes precedence over both.
Sourcepub fn set_buffer_limit(&mut self, limit: usize)
pub fn set_buffer_limit(&mut self, limit: usize)
Sets how much output of a value is buffered before it’s written.
If the format supports it (see
StreamSerializer::supports_partial), the output of a value is
written once it exceeds the limit, the serialization continues
after that. This way the memory used does not depend on the size of
the values. The default is DEFAULT_BUFFER_LIMIT (8 KiB). With
usize::MAX every value is serialized completely before it’s
written.
use deser::io::Writer;
/// Counts the writes.
struct Counter(usize);
impl std::io::Write for Counter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0 += 1;
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
// `Xs` is a format which writes an `x` for every event
let mut writer = Writer::new(Counter(0), Xs::default());
writer.set_buffer_limit(100);
writer.write(&vec![0; 20_000]).unwrap();
assert!(writer.get_ref().0 > 50);Sourcepub fn buffer_limit(&self) -> usize
pub fn buffer_limit(&self) -> usize
Returns how much output of a value is buffered before it’s written.
Sourcepub fn write<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error>
pub fn write<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error>
Serializes a value and writes it.
If the value fails to serialize before any of it was written (which
is always the case for values whose output is below the
buffer limit), nothing is written and the
next value can be written. If a value is abandoned after a part of
it was written, because it fails to serialize or a write fails, the
stream holds an incomplete value and the writer refuses to write
more values (see StreamSerializer::in_progress).
Sourcepub fn write_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
pub fn write_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
Serializes a value with a configured driver and writes it.
The callback is invoked with the driver before the value is
serialized, for instance to add Layers.
Sourcepub fn serializer(&self) -> &S
pub fn serializer(&self) -> &S
Returns the stream serializer.
This gives access to the state of the stream, for instance the number of values that were written.
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Returns the underlying writer.
Sourcepub fn into_parts(self) -> (W, S)
pub fn into_parts(self) -> (W, S)
Returns the underlying writer and the stream serializer.
Trait Implementations§
Source§impl<W: Write, S: StreamSerializer> Serializer for Writer<W, S>
Serializes values and writes them, like Writer::write.
impl<W: Write, S: StreamSerializer> Serializer for Writer<W, S>
Serializes values and writes them, like Writer::write.