Skip to main content

Decode

Trait Decode 

Source
pub trait Decode<'a>: Sized + 'a {
    type Error: Error + From<Error> + 'static;

    // Required method
    fn decode<R: Reader<'a>>(decoder: &mut R) -> Result<Self, Self::Error>;

    // Provided methods
    fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error> { ... }
    fn from_der_partial(
        bytes: &'a [u8],
    ) -> Result<(Self, &'a [u8]), Self::Error> { ... }
}
Expand description

Decode trait parses a complete TLV (Tag-Length-Value) structure.

This trait provides the core abstraction upon which all decoding operations are based.

When decoding fails, a Decode::Error type is thrown. Most ASN.1 DER objects return a builtin der Error type as Decode::Error, which can be made from ErrorKind.

§Example

use der::{Any, Decode, Reader};

/// Wrapper around Any, with custom foreign trait support.
///
/// For example: serde Serialize/Deserialize
pub struct AnySerde(pub Any);

impl<'a> Decode<'a> for AnySerde {
    type Error = der::Error;

    fn decode<R: Reader<'a>>(reader: &mut R) -> der::Result<Self> {
        // calls impl Decode for Any
        Ok(Self(Any::decode(reader)?))
    }
}

Required Associated Types§

Source

type Error: Error + From<Error> + 'static

Type returned in the event of a decoding error.

Required Methods§

Source

fn decode<R: Reader<'a>>(decoder: &mut R) -> Result<Self, Self::Error>

Attempt to decode this TLV message using the provided decoder.

§Errors

Returns Self::Error in the event a decoding error occurred.

Provided Methods§

Source

fn from_der(bytes: &'a [u8]) -> Result<Self, Self::Error>

Parse Self from the provided DER-encoded byte slice.

§Errors

Returns Self::Error in the event a decoding error occurred.

Source

fn from_der_partial(bytes: &'a [u8]) -> Result<(Self, &'a [u8]), Self::Error>

Parse Self from the provided DER-encoded byte slice.

Returns remaining byte slice, without checking for incomplete message.

§Errors

Returns Self::Error in the event a decoding error occurred.

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.

Implementations on Foreign Types§

Source§

impl<'a, T> Decode<'a> for Option<T>
where T: Choice<'a>,

Source§

type Error = <T as Decode<'a>>::Error

Source§

fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Option<T>, Self::Error>

Source§

impl<'a, T> Decode<'a> for PhantomData<T>
where T: ?Sized + 'a,

Dummy implementation for PhantomData which allows deriving implementations on structs with phantom fields.

Implementors§

Source§

impl<'a> Decode<'a> for Tag

Source§

impl<'a> Decode<'a> for Any

Source§

impl<'a> Decode<'a> for AnyRef<'a>

Source§

impl<'a> Decode<'a> for Document

Source§

impl<'a> Decode<'a> for Header

Source§

impl<'a> Decode<'a> for Length

Source§

impl<'a, T> Decode<'a> for Application<T>
where T: Decode<'a>,

Source§

type Error = <T as Decode<'a>>::Error

Source§

impl<'a, T> Decode<'a> for ContextSpecific<T>
where T: Decode<'a>,

Source§

type Error = <T as Decode<'a>>::Error

Source§

impl<'a, T> Decode<'a> for Private<T>
where T: Decode<'a>,

Source§

type Error = <T as Decode<'a>>::Error

Source§

impl<'a, T> Decode<'a> for T
where T: DecodeValue<'a> + FixedTag + 'a,

Source§

type Error = <T as DecodeValue<'a>>::Error