summaryrefslogtreecommitdiffstats
path: root/vendor/der/src/asn1/sequence.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/der/src/asn1/sequence.rs')
-rw-r--r--vendor/der/src/asn1/sequence.rs63
1 files changed, 16 insertions, 47 deletions
diff --git a/vendor/der/src/asn1/sequence.rs b/vendor/der/src/asn1/sequence.rs
index d2f6bc5d1..ad4a5d52e 100644
--- a/vendor/der/src/asn1/sequence.rs
+++ b/vendor/der/src/asn1/sequence.rs
@@ -2,69 +2,40 @@
//! `SEQUENCE`s to Rust structs.
use crate::{
- ByteSlice, Decode, DecodeValue, Encode, EncodeValue, FixedTag, Header, Length, Reader, Result,
- Tag, Writer,
+ BytesRef, DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Result, Tag, Writer,
};
-/// ASN.1 `SEQUENCE` trait.
-///
-/// Types which impl this trait receive blanket impls for the [`Decode`],
-/// [`Encode`], and [`FixedTag`] traits.
-pub trait Sequence<'a>: Decode<'a> {
- /// Call the provided function with a slice of [`Encode`] trait objects
- /// representing the fields of this `SEQUENCE`.
- ///
- /// This method uses a callback because structs with fields which aren't
- /// directly [`Encode`] may need to construct temporary values from
- /// their fields prior to encoding.
- fn fields<F, T>(&self, f: F) -> Result<T>
- where
- F: FnOnce(&[&dyn Encode]) -> Result<T>;
-}
-
-impl<'a, M> EncodeValue for M
-where
- M: Sequence<'a>,
-{
- fn value_len(&self) -> Result<Length> {
- self.fields(|fields| {
- fields
- .iter()
- .try_fold(Length::ZERO, |acc, field| acc + field.encoded_len()?)
- })
- }
+#[cfg(feature = "alloc")]
+use alloc::boxed::Box;
- fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> {
- self.fields(|fields| {
- for &field in fields {
- field.encode(writer)?;
- }
-
- Ok(())
- })
- }
-}
+/// Marker trait for ASN.1 `SEQUENCE`s.
+///
+/// This is mainly used for custom derive.
+pub trait Sequence<'a>: DecodeValue<'a> + EncodeValue {}
-impl<'a, M> FixedTag for M
+impl<'a, S> FixedTag for S
where
- M: Sequence<'a>,
+ S: Sequence<'a>,
{
const TAG: Tag = Tag::Sequence;
}
+#[cfg(feature = "alloc")]
+impl<'a, T> Sequence<'a> for Box<T> where T: Sequence<'a> {}
+
/// The [`SequenceRef`] type provides raw access to the octets which comprise a
/// DER-encoded `SEQUENCE`.
///
/// This is a zero-copy reference type which borrows from the input data.
pub struct SequenceRef<'a> {
/// Body of the `SEQUENCE`.
- body: ByteSlice<'a>,
+ body: BytesRef<'a>,
}
impl<'a> DecodeValue<'a> for SequenceRef<'a> {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
Ok(Self {
- body: ByteSlice::decode_value(reader, header)?,
+ body: BytesRef::decode_value(reader, header)?,
})
}
}
@@ -74,11 +45,9 @@ impl EncodeValue for SequenceRef<'_> {
Ok(self.body.len())
}
- fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> {
+ fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
self.body.encode_value(writer)
}
}
-impl<'a> FixedTag for SequenceRef<'a> {
- const TAG: Tag = Tag::Sequence;
-}
+impl<'a> Sequence<'a> for SequenceRef<'a> {}