From b5d5a246468399043854e639e48d939cf1c7b764 Mon Sep 17 00:00:00 2001 From: Harris Kaufmann Date: Sun, 3 Aug 2025 00:02:50 +0200 Subject: [PATCH] der: add `new` and derives to SequenceRef --- der/src/asn1/sequence.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/der/src/asn1/sequence.rs b/der/src/asn1/sequence.rs index 9b7c8a70b..2013a5660 100644 --- a/der/src/asn1/sequence.rs +++ b/der/src/asn1/sequence.rs @@ -2,8 +2,8 @@ //! `SEQUENCE`s to Rust structs. use crate::{ - BytesRef, DecodeValue, EncodeValue, Error, FixedTag, Header, Length, Reader, Result, Tag, - Writer, + BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, Result, + Tag, Writer, }; #[cfg(feature = "alloc")] @@ -28,12 +28,20 @@ impl<'a, T> Sequence<'a> for Box where T: Sequence<'a> {} /// DER-encoded `SEQUENCE`. /// /// This is a zero-copy reference type which borrows from the input data. +#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct SequenceRef<'a> { /// Body of the `SEQUENCE`. body: &'a BytesRef, } impl<'a> SequenceRef<'a> { + /// Create a new [`SequenceRef`] from the provided DER bytes. + pub fn new(slice: &'a [u8]) -> Result { + BytesRef::new(slice) + .map(|body| Self { body }) + .map_err(|_| ErrorKind::Length { tag: Self::TAG }.into()) + } + /// Borrow the inner byte slice. pub fn as_bytes(&self) -> &'a [u8] { self.body.as_slice()