Trait Codec

Source
pub trait Codec {
    type ExpandError: Error;

    // Required methods
    fn compress_line(&self, line: &str) -> String;
    fn expand_line(&self, line: &str) -> Result<String, Self::ExpandError>;

    // Provided methods
    fn compress(
        &self,
        r: &mut impl BufRead,
        w: &mut impl Write,
    ) -> Result<(), Error> { ... }
    fn expand(
        &self,
        r: &mut impl BufRead,
        w: &mut impl Write,
    ) -> Result<(), TranscodeError<Self::ExpandError>> { ... }
}
Expand description

The specification of a codec.

Codecs work line by line. As such, they require two method implementations: compress_line — to encode one line of input text, and expand_line — to perform the reverse operation.

Compression is not allowed to return an error; the thinking is that all text should be compressible. Expansion, however, may return an error. It is possible that the output of compression may have been mangled with, in which case expansion is not possible.

Required Associated Types§

Source

type ExpandError: Error

The type of error returned during expansion.

Required Methods§

Source

fn compress_line(&self, line: &str) -> String

Compresses a line of text, returning its encoded representation as a String.

Source

fn expand_line(&self, line: &str) -> Result<String, Self::ExpandError>

Expands a line of text, returning its decoded representation as a String.

§Errors

Expansion is not always possible, in which case an ExpandError is returned.

Provided Methods§

Source

fn compress( &self, r: &mut impl BufRead, w: &mut impl Write, ) -> Result<(), Error>

A helper method for compressing a series of lines from a given buffered reader, outputting the result into the given writer.

§Errors

io::Error if the reader or the writer encountered an I/O error.

Source

fn expand( &self, r: &mut impl BufRead, w: &mut impl Write, ) -> Result<(), TranscodeError<Self::ExpandError>>

A helper method for expanding a series of lines from a given buffered reader, outputting the result into the given writer.

§Errors

TranscodeError if the reader or the writer encountered an I/O error, or if the codec was unable to expand a line.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§