| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::{ |
| 5 |
BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, Result, |
| 6 |
Tag, Writer, |
| 7 |
}; |
| 8 |
|
| 9 |
#[cfg(feature = "alloc")] |
| 10 |
use alloc::boxed::Box; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
pub trait Sequence<'a> {} |
| 16 |
|
| 17 |
impl<'a, S> FixedTag for S |
| 18 |
where |
| 19 |
S: Sequence<'a>, |
| 20 |
{ |
| 21 |
const TAG: Tag = Tag::Sequence; |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(feature = "alloc")] |
| 25 |
impl<'a, T> Sequence<'a> for Box<T> where T: Sequence<'a> {} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord)] |
| 32 |
#[repr(transparent)] |
| 33 |
pub struct SequenceRef { |
| 34 |
|
| 35 |
body: BytesRef, |
| 36 |
} |
| 37 |
|
| 38 |
impl SequenceRef { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
pub fn new(slice: &[u8]) -> Result<&Self> { |
| 44 |
BytesRef::new(slice) |
| 45 |
.map(Self::from_bytes_ref) |
| 46 |
.map_err(|_| ErrorKind::Length { tag: Tag::Sequence }.into()) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
fn from_bytes_ref(bytes_ref: &BytesRef) -> &Self { |
| 53 |
|
| 54 |
#[allow(unsafe_code)] |
| 55 |
unsafe { |
| 56 |
&*(bytes_ref.as_ptr() as *const Self) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
#[must_use] |
| 62 |
pub fn as_bytes(&self) -> &[u8] { |
| 63 |
self.body.as_slice() |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl AsRef<[u8]> for SequenceRef { |
| 68 |
fn as_ref(&self) -> &[u8] { |
| 69 |
self.as_bytes() |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<'a> DecodeValue<'a> for &'a SequenceRef { |
| 74 |
type Error = Error; |
| 75 |
|
| 76 |
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> { |
| 77 |
<&'a BytesRef>::decode_value(reader, header).map(SequenceRef::from_bytes_ref) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
impl EncodeValue for SequenceRef { |
| 82 |
fn value_len(&self) -> Result<Length> { |
| 83 |
Ok(self.body.len()) |
| 84 |
} |
| 85 |
|
| 86 |
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { |
| 87 |
self.body.encode_value(writer) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl<'a> Sequence<'a> for &'a SequenceRef {} |