Struct Writer

Source
pub struct Writer<'a, W, E>
where W: 'a + Write, E: Endian,
{ /* private fields */ }
Expand description

Wraps a seekable writer with extra functions to conveniently write data in various common binary formats and keep track of labelled offsets to help calculate section sizes and object positions.

Each writer has an endianness as part of its type, which dictates how it will write out multi-byte values. The endianness is built into the writer because most formats exclusively use a single endianness throughout, but for situations where that isn’t true you can use an endian override for a particular value and thus ignore the writer’s default.

During writing the underlying writer will contain placeholder data for any deferred values, which will then be overwritten with true values during finalization. If the underlying writer is a file on disk then other applications may be able to observe the placeholder values if they happen to inspect the file while it’s under construction.

If any operation on a Writer returns an error, the underlying stream is left in an undefined state and the user should cease further use of the writer and treat the result as invalid.

Implementations§

Source§

impl<'a, W, E> Writer<'a, W, E>
where W: Write, E: Endian,

Methods that only write to the current position in the underlying stream.

Source

pub fn write<V: IntoPack>(&mut self, v: V) -> Result<usize>

Writes a value to the current position in the output.

write can accept any value that implements IntoPack, and will write the result from packing the value to the underlying stream.

Source

pub fn skip(&mut self, count: usize) -> Result<usize>

Inserts the given number of bytes of padding.

Source

pub fn set_padding(&mut self, v: u8)

Changes the padding value used for future calls to align, and possibly for other functionality added in future that might also create padding.

Source§

impl<'a, W, E> Writer<'a, W, E>
where W: Seek + Write, E: Endian,

Methods that use std::io::Seek.

Source

pub fn position(&mut self) -> Result<u64>

Returns the current write position in the underlying writer.

Use this with resolve to resolve a deferred slot that ought to contain the offset of whatever new content you are about to write.

Source

pub fn align(&mut self, n: usize) -> Result<usize>

Moves the current stream position forward to a position aligned to the given number of bytes, writing padding bytes as necessary. Returns the number of padding bytes written.

A new Writer defaults to using zeros for padding. Use set_padding to override the padding byte for future writes, if needed.

Source

pub fn subregion<F>(&mut self, f: F) -> Result<Range<u64>>
where F: FnOnce(&mut Self) -> Result<()>,

Creates a region of the output whose final bounds must be known for use elsewhere in the output.

If the given function completes successfully, subregion returns a range describing the start and end positions of the subregion in the underlying stream.

Source

pub fn deferred<T>(&mut self, initial: T) -> Deferred<'a, T>

Creates a slot for a value whose resolution will come later in the process of writing all of the data.

You can use write_placeholder to reserve an area of the output where the final value will eventually be written. The reserved space will initially contain the value given in initial.

Call resolve to set the final value for this slot. That will then overwrite any placeholders written earlier with the final value.

Source

pub fn write_placeholder<T>( &mut self, deferred: Deferred<'a, T>, ) -> Result<usize>

Writes a placeholder for the given deferred slot to the current position in the output.

At some later point you should pass the same deferred slot to resolve along with its final value, at which point the placeholder will be overwritten.

Source

pub fn write_deferred<T>(&mut self, initial: T) -> Result<Deferred<'a, T>>

A shorthand combining deferred and write_placeholder, to create a new deferred slot and write a placeholder for it in a single call.

Source

pub fn resolve<T>(&mut self, deferred: Deferred<'a, T>, v: T) -> Result<T>

Assigns a final value to a deferred data slot previously established using deferred.

Source§

impl<'a, W, E> Writer<'a, W, E>
where W: Seek + Write + Read, E: Endian,

Methods that use std::io::Read and std::io::Seek.

Source

pub fn derive<F, T>(&mut self, rng: Range<u64>, f: F) -> Result<T>
where F: FnOnce(&mut DeriveRead<'_, W>) -> Result<T>,

Derive a value from an already-written region of the underlying stream.

This function is available only for streams that implement Read in addition to the usually-required Write and Seek.

The given function recieves a reader over the requested region, and can return any value derived from the contents of that region. For example, some binary formats include checksums to help with error detection, and you could potentially use derive over the relevant subregion to calculate such a checksum.

Trait Implementations§

Source§

impl<'a, T, E> Write for Writer<'a, T, E>
where T: Seek + Write, E: Endian,

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'a, W, E> Freeze for Writer<'a, W, E>
where W: Freeze,

§

impl<'a, W, E> RefUnwindSafe for Writer<'a, W, E>

§

impl<'a, W, E> Send for Writer<'a, W, E>
where W: Send, E: Sync,

§

impl<'a, W, E> Sync for Writer<'a, W, E>
where W: Sync, E: Sync,

§

impl<'a, W, E> Unpin for Writer<'a, W, E>
where W: Unpin,

§

impl<'a, W, E> UnwindSafe for Writer<'a, W, E>

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.