Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/der/src/asn1/optional.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / der / src / asn1 / optional.rs` 66 lines ยท 1556 bytes Raw Blame History 1 //! ASN.1 `OPTIONAL` as mapped to Rust's `Option` type 2 3 use crate::{Choice, Decode, DerOrd, Encode, Length, Reader, Result, Tag, Writer}; 4 use core::cmp::Ordering; 5 6 impl<'a, T> Decode<'a> for Option 7 where 8 T: Choice<'a>, // NOTE: all `Decode + Tagged` types receive a blanket `Choice` impl 9 { 10 fn decode>(reader: &mut R) -> Result> { 11 if let Some(byte) = reader.peek_byte() { 12 if T::can_decode(Tag::try_from(byte)?) { 13 return T::decode(reader).map(Some); 14 } 15 } 16 17 Ok(None) 18 } 19 } 20 21 impl DerOrd for Option 22 where 23 T: DerOrd, 24 { 25 fn der_cmp(&self, other: &Self) -> Result { 26 match self { 27 Some(a) => match other { 28 Some(b) => a.der_cmp(b), 29 None => Ok(Ordering::Greater), 30 }, 31 None => Ok(Ordering::Less), 32 } 33 } 34 } 35 36 impl Encode for Option 37 where 38 T: Encode, 39 { 40 fn encoded_len(&self) -> Result { 41 (&self).encoded_len() 42 } 43 44 fn encode(&self, writer: &mut impl Writer) -> Result<()> { 45 (&self).encode(writer) 46 } 47 } 48 49 impl Encode for &Option 50 where 51 T: Encode, 52 { 53 fn encoded_len(&self) -> Result { 54 match self { 55 Some(encodable) => encodable.encoded_len(), 56 None => Ok(0u8.into()), 57 } 58 } 59 60 fn encode(&self, encoder: &mut impl Writer) -> Result<()> { 61 match self { 62 Some(encodable) => encodable.encode(encoder), 63 None => Ok(()), 64 } 65 } 66 }