interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / der / src / asn1 / sequence.rs

53 lines · 1387 bytes Raw Blame History
1//! The [`Sequence`] trait simplifies writing decoders/encoders which map ASN.1
2//! `SEQUENCE`s to Rust structs.
3
4use crate::{
5 BytesRef, DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Result, Tag, Writer,
6};
7
8#[cfg(feature = "alloc")]
9use alloc::boxed::Box;
10
11/// Marker trait for ASN.1 `SEQUENCE`s.
12///
13/// This is mainly used for custom derive.
14pub trait Sequence<'a>: DecodeValue<'a> + EncodeValue {}
15
16impl<'a, S> FixedTag for S
17where
18 S: Sequence<'a>,
19{
20 const TAG: Tag = Tag::Sequence;
21}
22
23#[cfg(feature = "alloc")]
24impl<'a, T> Sequence<'a> for Box<T> where T: Sequence<'a> {}
25
26/// The [`SequenceRef`] type provides raw access to the octets which comprise a
27/// DER-encoded `SEQUENCE`.
28///
29/// This is a zero-copy reference type which borrows from the input data.
30pub struct SequenceRef<'a> {
31 /// Body of the `SEQUENCE`.
32 body: BytesRef<'a>,
33}
34
35impl<'a> DecodeValue<'a> for SequenceRef<'a> {
36 fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
37 Ok(Self {
38 body: BytesRef::decode_value(reader, header)?,
39 })
40 }
41}
42
43impl EncodeValue for SequenceRef<'_> {
44 fn value_len(&self) -> Result<Length> {
45 Ok(self.body.len())
46 }
47
48 fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
49 self.body.encode_value(writer)
50 }
51}
52
53impl<'a> Sequence<'a> for SequenceRef<'a> {}