Trait Encoding

Source
pub trait Encoding: Send + Sync {
    // Required methods
    fn encode_to_slice(
        &self,
        src: &[u8],
        dst: &mut [u8],
    ) -> Result<usize, Error>;
    fn encoded_len(&self, bytes: &[u8]) -> usize;
    fn decode_to_slice(
        &self,
        src: &[u8],
        dst: &mut [u8],
    ) -> Result<usize, Error>;
    fn decoded_len(&self, encoded_bytes: &[u8]) -> Result<usize, Error>;

    // Provided methods
    fn encode<B: AsRef<[u8]>>(&self, bytes: B) -> Vec<u8>  { ... }
    fn encode_to_string<B: AsRef<[u8]>>(
        &self,
        bytes: B,
    ) -> Result<String, Error> { ... }
    fn encode_to_writer<B, W>(
        &self,
        bytes: B,
        writer: &mut W,
    ) -> Result<usize, Error>
       where B: AsRef<[u8]>,
             W: Write { ... }
    fn encode_to_file<B, P>(&self, bytes: B, path: P) -> Result<File, Error>
       where B: AsRef<[u8]>,
             P: AsRef<Path> { ... }
    fn decode<B: AsRef<[u8]>>(&self, encoded_bytes: B) -> Result<Vec<u8>, Error> { ... }
    fn decode_from_str<S: AsRef<str>>(
        &self,
        encoded: S,
    ) -> Result<Vec<u8>, Error> { ... }
    fn decode_from_reader<R: Read>(
        &self,
        reader: &mut R,
    ) -> Result<Vec<u8>, Error> { ... }
    fn decode_from_file<P: AsRef<Path>>(
        &self,
        path: P,
    ) -> Result<Vec<u8>, Error> { ... }
}
Expand description

All encoding types in this crate implement this trait

Required Methods§

Source

fn encode_to_slice(&self, src: &[u8], dst: &mut [u8]) -> Result<usize, Error>

Encode the given slice into the destination buffer.

Returns the size of the encoded output, or Error if the destination buffer was too small to hold the encoded output.

Source

fn encoded_len(&self, bytes: &[u8]) -> usize

Calculate the length of the given data after encoding.

Source

fn decode_to_slice(&self, src: &[u8], dst: &mut [u8]) -> Result<usize, Error>

Decode hexadecimal (upper or lower case) with branchless / secret-independent logic

Source

fn decoded_len(&self, encoded_bytes: &[u8]) -> Result<usize, Error>

Calculate the length of the given data after decoding.

Provided Methods§

Source

fn encode<B: AsRef<[u8]>>(&self, bytes: B) -> Vec<u8>

Encode the given buffer, returning a Vec<u8>

Source

fn encode_to_string<B: AsRef<[u8]>>(&self, bytes: B) -> Result<String, Error>

Encode the given slice to a String with this Encoding.

Returns an Error in the event this encoding does not produce a well-formed UTF-8 string.

Source

fn encode_to_writer<B, W>( &self, bytes: B, writer: &mut W, ) -> Result<usize, Error>
where B: AsRef<[u8]>, W: Write,

Encode the given slice with this Encoding, writing the result to the supplied io::Write type, returning the number of bytes written or a Error.

Source

fn encode_to_file<B, P>(&self, bytes: B, path: P) -> Result<File, Error>
where B: AsRef<[u8]>, P: AsRef<Path>,

Encode self and write it to a file at the given path, returning the resulting File or a Error.

If the file does not exist, it will be created with a mode of FILE_MODE (i.e. 600). If the file does exist, it will be erased and replaced.

Source

fn decode<B: AsRef<[u8]>>(&self, encoded_bytes: B) -> Result<Vec<u8>, Error>

Decode the given buffer, returning a Vec<u8>

Source

fn decode_from_str<S: AsRef<str>>(&self, encoded: S) -> Result<Vec<u8>, Error>

Decode the given string-alike type with this Encoding, returning the decoded value or a Error.

Source

fn decode_from_reader<R: Read>(&self, reader: &mut R) -> Result<Vec<u8>, Error>

Decode the data read from the given io::Read type with this Encoding, returning the decoded value or a Error.

Source

fn decode_from_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>, Error>

Read a file at the given path, decoding the data it contains using the provided Encoding, returning the decoded value or a Error.

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§