Struct Encoder
pub struct Encoder { /* private fields */ }
The state for encoding data with an LZW algorithm.
The same structure can be utilized with streams as well as your own buffers and driver logic. It may even be possible to mix them if you are sufficiently careful not to lose any written data in the process.
This is a sans-IO implementation, meaning that it only contains the state of the encoder and
the caller will provide buffers for input and output data when calling the basic
encode_bytes method. Nevertheless, a number of adapters are provided in the into_*
methods for enoding with a particular style of common IO.
encodefor encoding once without any IO-loop.into_asyncfor encoding with thefuturestraits for asynchronous IO.into_streamfor encoding with the standardiotraits.into_vecfor in-memory encoding.
Implementations
impl Encoder
fn new(order: BitOrder, size: u8) -> SelfCreate a new encoder with the specified bit order and symbol size.
The algorithm for dynamically increasing the code symbol bit width is compatible with the original specification. In particular you will need to specify an
Lsbbit oder to encode the data portion of a compressedgifimage.Panics
The
sizeneeds to be in the interval2..=12.fn with_tiff_size_switch(order: BitOrder, size: u8) -> SelfCreate a TIFF compatible encoder with the specified bit order and symbol size.
The algorithm for dynamically increasing the code symbol bit width is compatible with the TIFF specification, which is a misinterpretation of the original algorithm for increasing the code size. It switches one symbol sooner.
Panics
The
sizeneeds to be in the interval2..=12.fn encode_bytes(&mut self, inp: &[u8], out: &mut [u8]) -> BufferResultEncode some bytes from
inpintoout.See
into_streamfor high-level functions (this interface is only available with thestdfeature) andfinishfor marking the input data as complete.When some input byte is invalid, i.e. is not smaller than
1 << size, then that byte and all following ones will not be consumed and thestatusof the result will signal an error. The result will also indicate that all bytes up to but not including the offending byte have been consumed. You may try again with a fixed byte.fn encode(&mut self, data: &[u8]) -> Result<Vec<u8>, LzwError>Encode a single chunk of data.
This method will add an end marker to the encoded chunk.
This is a convenience wrapper around
into_vec. Use theinto_vecadapter to customize buffer size, to supply an existing vector, to control whether an end marker is required, or to preserve partial data in the case of a decoding error.Example
use ; let data = b"Hello, world"; let encoded = new .encode .expect;fn into_stream<W: Write>(&mut self, writer: W) -> IntoStream<'_, W>Construct a encoder into a writer.
fn into_vec<'lt>(&'lt mut self, vec: &'lt mut Vec<u8>) -> IntoVec<'lt>Construct an encoder into a vector.
All encoded data is appended and the vector is not cleared.
Compared to
into_streamthis interface allows a high-level access to encoding without requires thestd-feature. Also, it can make full use of the extra buffer control that the special target exposes.fn finish(&mut self)Mark the encoding as in the process of finishing.
The next following call to
encode_byteswhich is able to consume the complete input will also try to emit an end code. It's not recommended, but also not unsound, to use different byte slices in different calls from this point forward and thus to 'delay' the actual end of the data stream. The behaviour after the end marker has been written is unspecified but sound.fn reset(&mut self)Reset all internal state.
This produce an encoder as if just constructed with
newbut taking slightly less work. In particular it will not deallocate any internal allocations. It will also avoid some duplicate setup work.
Auto Trait Implementations
impl !RefUnwindSafe for Encoder
impl !Sync for Encoder
impl !UnwindSafe for Encoder
impl Freeze for Encoder
impl Send for Encoder
impl Unpin for Encoder
impl UnsafeUnpin for Encoder
Blanket Implementations
impl<T> Any for Encoder
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Encoder
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Encoder
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for Encoder
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for Encoder
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Encoder
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Encoder
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>