Skip to content

Commit b5d5a24

Browse files
committed
der: add new and derives to SequenceRef
1 parent 1131521 commit b5d5a24

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

der/src/asn1/sequence.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! `SEQUENCE`s to Rust structs.
33
44
use crate::{
5-
BytesRef, DecodeValue, EncodeValue, Error, FixedTag, Header, Length, Reader, Result, Tag,
6-
Writer,
5+
BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, Result,
6+
Tag, Writer,
77
};
88

99
#[cfg(feature = "alloc")]
@@ -28,12 +28,20 @@ impl<'a, T> Sequence<'a> for Box<T> where T: Sequence<'a> {}
2828
/// DER-encoded `SEQUENCE`.
2929
///
3030
/// This is a zero-copy reference type which borrows from the input data.
31+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
3132
pub struct SequenceRef<'a> {
3233
/// Body of the `SEQUENCE`.
3334
body: &'a BytesRef,
3435
}
3536

3637
impl<'a> SequenceRef<'a> {
38+
/// Create a new [`SequenceRef`] from the provided DER bytes.
39+
pub fn new(slice: &'a [u8]) -> Result<Self> {
40+
BytesRef::new(slice)
41+
.map(|body| Self { body })
42+
.map_err(|_| ErrorKind::Length { tag: Self::TAG }.into())
43+
}
44+
3745
/// Borrow the inner byte slice.
3846
pub fn as_bytes(&self) -> &'a [u8] {
3947
self.body.as_slice()

0 commit comments

Comments
 (0)