Skip to main content

OutputTarget

Trait OutputTarget 

Source
pub trait OutputTarget {
    // Required methods
    fn remaining(&self) -> usize;
    fn write_byte(&mut self, byte: u8) -> Result<()>;
    fn write_bytes_exact(&mut self, bytes: &[u8]) -> Result<()>;
    fn write_bytes_into_reserved_exact(
        &mut self,
        reservation: &mut Reservation,
        bytes: &[u8],
    ) -> Result<()>;
    fn reserve_space(&mut self, count: usize) -> Result<Reservation>;
}
Expand description

A trait for types that can be written to by a Slice encoder.

Required Methods§

Source

fn remaining(&self) -> usize

Returns the number of unwritten bytes currently remaining in the target.

Note: some implementations are capable of growing their underlying buffers as needed. For these implementations, this function returns how many bytes can be written without needing to grow ie. “… currently remaining …”. For these types, it’s generally safe to write more than remaining() bytes to the target, since it will simply grow as needed. But these writes can still fail if an allocation error occurs.

Source

fn write_byte(&mut self, byte: u8) -> Result<()>

Attempts to write the provided byte into this target.

Source

fn write_bytes_exact(&mut self, bytes: &[u8]) -> Result<()>

Attempts to write the provided bytes into this target.

This function will not return until either all the provided bytes have been written, or an unrecoverable error has occurred. If such an error occurs, no guarantees are made about how many bytes were written, or the state of the underlying target.

Source

fn write_bytes_into_reserved_exact( &mut self, reservation: &mut Reservation, bytes: &[u8], ) -> Result<()>

Attempts to write the provided bytes into a reserved chunk of memory within this target.

This is used alongside Self::reserve_space to write data into the target without appending it at the end. However, this function cannot be used to write to arbitrary positions in the target; reservations must be made in advance.

It is legal to call this function multiple times with the same Reservation, but as bytes are written, the reservation will be shrunk accordingly (starting from the front) to ensure bytes are never over-written.

Like Self::write_bytes_exact, this function will not return until either all the provided bytes have been written, or an unrecoverable error has occurred. If such an error occurs, no guarantees are made about how many bytes were written, or the state of the underlying target.

Source

fn reserve_space(&mut self, count: usize) -> Result<Reservation>

Reserves a chunk of memory in the target that can be written to later, and advances past it.

The target’s cursor is advanced by count-many bytes (so it points to the end of the reservation) meaning additional writes to this target will continue normally outside the reserved memory.

It returns a typed range (Reservation) specifying the beginning and end of this reserved memory. This memory can written to later by passing the returned Reservation into Self::write_bytes_into_reserved_exact.

If the underlying target has insufficient memory (and couldn’t allocate more) an error is returned instead.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§