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§
Sourcefn remaining(&self) -> usize
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.
Sourcefn write_byte(&mut self, byte: u8) -> Result<()>
fn write_byte(&mut self, byte: u8) -> Result<()>
Attempts to write the provided byte into this target.
Sourcefn write_bytes_exact(&mut self, bytes: &[u8]) -> Result<()>
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.
Sourcefn write_bytes_into_reserved_exact(
&mut self,
reservation: &mut Reservation,
bytes: &[u8],
) -> Result<()>
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.
Sourcefn reserve_space(&mut self, count: usize) -> Result<Reservation>
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".