Skip to main content

Writer

Struct Writer 

Source
pub struct Writer<W, S: StreamSerializer> { /* private fields */ }
Available on crate feature 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>

Source

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).

Source

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.

Source

pub fn context(&self) -> &Context

Returns the context the values are serialized in.

Source

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);
Source

pub fn buffer_limit(&self) -> usize

Returns how much output of a value is buffered before it’s written.

Source

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).

Source

pub fn write_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
where T: Serialize + ?Sized, F: FnOnce(&mut SerializeDriver<'_>),

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.

Source

pub fn flush(&mut self) -> Result<(), Error>

Flushes the underlying writer.

Source

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.

Source

pub fn get_ref(&self) -> &W

Returns a reference to the underlying writer.

Source

pub fn get_mut(&mut self) -> &mut W

Returns a mutable reference to the underlying writer.

Source

pub fn into_inner(self) -> W

Returns the underlying writer.

Source

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.

Source§

fn drive(&mut self, driver: &mut SerializeDriver<'_>) -> Result<(), Error>

Receives the events of a value from the driver and writes them into the output.
Source§

fn serialize<T: Serialize + ?Sized>(&mut self, value: &T) -> Result<(), Error>
where Self: Sized,

Serializes a value.
Source§

fn serialize_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
where T: Serialize + ?Sized, F: FnOnce(&mut SerializeDriver<'_>), Self: Sized,

Serializes a value with a configured driver. Read more
Source§

fn serialize_ref(&mut self, value: SerializeRef<'_>) -> Result<(), Error>

Serializes the value of a reference. Read more

Auto Trait Implementations§

§

impl<W, S> Freeze for Writer<W, S>
where W: Freeze, S: Freeze,

§

impl<W, S> RefUnwindSafe for Writer<W, S>

§

impl<W, S> Send for Writer<W, S>
where W: Send, S: Send,

§

impl<W, S> Sync for Writer<W, S>
where W: Sync, S: Sync,

§

impl<W, S> Unpin for Writer<W, S>
where W: Unpin, S: Unpin,

§

impl<W, S> UnsafeUnpin for Writer<W, S>
where W: UnsafeUnpin, S: UnsafeUnpin,

§

impl<W, S> UnwindSafe for Writer<W, S>
where W: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.