[][src]Struct picky_asn1::wrapper::Implicit

pub struct Implicit<T>(pub T);

Wrapper for ASN.1 implicits (optionals) fields

Wrapped type has to implement the Default trait to be deserializable (on deserialization failure a default value is returned).

Examples:

use picky_asn1::wrapper::{Implicit, ApplicationTag0};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct MyWrapper(u8);

impl Default for MyWrapper {
    fn default() -> Self {
        Self(10)
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct ComplexType {
    // skip if default to reduce encoded size
    #[serde(skip_serializing_if = "implicit_field_is_default")]
    optional_field: Implicit<MyWrapper>,
    // behind application tag 0 to distinguish from optional_field that is a ASN.1 integer too.
    explicit_field: ApplicationTag0<u8>,
}

fn implicit_field_is_default(wrapper: &Implicit<MyWrapper>) -> bool {
    wrapper.0 == MyWrapper::default()
}

let complex_type = ComplexType {
    optional_field: MyWrapper::default().into(),
    explicit_field: 5.into(),
};

let buffer = [
    0x30, 0x05, // sequence
    // optional field isn't present
    0xA0, 0x03, 0x02, 0x01, 0x05, // explicit field
];

let encoded = picky_asn1_der::to_vec(&complex_type).expect("couldn't serialize");
assert_eq!(
    encoded,
    buffer,
);

let decoded: ComplexType = picky_asn1_der::from_bytes(&buffer).expect("couldn't deserialize");
assert_eq!(
    decoded,
    complex_type,
);

Trait Implementations

impl<T: Clone> Clone for Implicit<T>[src]

impl<T: Debug> Debug for Implicit<T>[src]

impl<T> Deref for Implicit<T>[src]

type Target = T

The resulting type after dereferencing.

impl<T> DerefMut for Implicit<T>[src]

impl<'de, T> Deserialize<'de> for Implicit<T> where
    T: Deserialize<'de> + Default
[src]

impl<T> From<T> for Implicit<T>[src]

impl<T: Hash> Hash for Implicit<T>[src]

impl<T: PartialEq> PartialEq<Implicit<T>> for Implicit<T>[src]

impl<T> PartialEq<T> for Implicit<T> where
    T: PartialEq
[src]

impl<T: PartialOrd> PartialOrd<Implicit<T>> for Implicit<T>[src]

impl<T> Serialize for Implicit<T> where
    T: Serialize
[src]

impl<T> StructuralPartialEq for Implicit<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Implicit<T> where
    T: RefUnwindSafe

impl<T> Send for Implicit<T> where
    T: Send

impl<T> Sync for Implicit<T> where
    T: Sync

impl<T> Unpin for Implicit<T> where
    T: Unpin

impl<T> UnwindSafe for Implicit<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.