/rust/registry/src/index.crates.io-1949cf8c6b5b557f/der-0.7.10/src/asn1/sequence.rs
Line | Count | Source |
1 | | //! The [`Sequence`] trait simplifies writing decoders/encoders which map ASN.1 |
2 | | //! `SEQUENCE`s to Rust structs. |
3 | | |
4 | | use crate::{ |
5 | | BytesRef, DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Result, Tag, Writer, |
6 | | }; |
7 | | |
8 | | #[cfg(feature = "alloc")] |
9 | | use alloc::boxed::Box; |
10 | | |
11 | | /// Marker trait for ASN.1 `SEQUENCE`s. |
12 | | /// |
13 | | /// This is mainly used for custom derive. |
14 | | pub trait Sequence<'a>: DecodeValue<'a> + EncodeValue {} |
15 | | |
16 | | impl<'a, S> FixedTag for S |
17 | | where |
18 | | S: Sequence<'a>, |
19 | | { |
20 | | const TAG: Tag = Tag::Sequence; |
21 | | } |
22 | | |
23 | | #[cfg(feature = "alloc")] |
24 | | impl<'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. |
30 | | pub struct SequenceRef<'a> { |
31 | | /// Body of the `SEQUENCE`. |
32 | | body: BytesRef<'a>, |
33 | | } |
34 | | |
35 | | impl<'a> DecodeValue<'a> for SequenceRef<'a> { |
36 | 0 | fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
37 | | Ok(Self { |
38 | 0 | body: BytesRef::decode_value(reader, header)?, |
39 | | }) |
40 | 0 | } |
41 | | } |
42 | | |
43 | | impl EncodeValue for SequenceRef<'_> { |
44 | 0 | fn value_len(&self) -> Result<Length> { |
45 | 0 | Ok(self.body.len()) |
46 | 0 | } |
47 | | |
48 | 0 | fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
49 | 0 | self.body.encode_value(writer) |
50 | 0 | } |
51 | | } |
52 | | |
53 | | impl<'a> Sequence<'a> for SequenceRef<'a> {} |