pub struct Optional<T>(pub T);
Expand description
Wrapper for ASN.1 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::{Optional, ExplicitContextTag0};
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 = "optional_field_is_default")]
optional_field: Optional<MyWrapper>,
// behind application tag 0 to distinguish from optional_field that is a ASN.1 integer too.
explicit_field: ExplicitContextTag0<u8>,
}
fn optional_field_is_default(wrapper: &Optional<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,
);
Tuple Fields§
§0: T
Implementations§
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for Optional<T>where
T: Deserialize<'de> + Default,
impl<'de, T> Deserialize<'de> for Optional<T>where
T: Deserialize<'de> + Default,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T: PartialOrd> PartialOrd for Optional<T>
impl<T: PartialOrd> PartialOrd for Optional<T>
impl<T: Eq> Eq for Optional<T>
impl<T> StructuralPartialEq for Optional<T>
Auto Trait Implementations§
impl<T> Freeze for Optional<T>where
T: Freeze,
impl<T> RefUnwindSafe for Optional<T>where
T: RefUnwindSafe,
impl<T> Send for Optional<T>where
T: Send,
impl<T> Sync for Optional<T>where
T: Sync,
impl<T> Unpin for Optional<T>where
T: Unpin,
impl<T> UnwindSafe for Optional<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more