diff --git a/der/src/asn1/any.rs b/der/src/asn1/any.rs index 583fe0304..69ac854bf 100644 --- a/der/src/asn1/any.rs +++ b/der/src/asn1/any.rs @@ -233,6 +233,14 @@ mod allocating { value: BytesOwned::default(), } } + + /// Create a new [`AnyRef`] from the provided [`Any`] owned tag and bytes. + pub const fn to_ref(&self) -> AnyRef<'_> { + AnyRef { + tag: self.tag, + value: self.value.to_ref(), + } + } } impl Choice<'_> for Any { @@ -272,7 +280,7 @@ mod allocating { impl<'a> From<&'a Any> for AnyRef<'a> { fn from(any: &'a Any) -> AnyRef<'a> { // Ensured to parse successfully in constructor - AnyRef::new(any.tag, any.value.as_slice()).expect("invalid ANY") + any.to_ref() } } diff --git a/der/src/bytes_owned.rs b/der/src/bytes_owned.rs index 330ac8bbd..784a8235b 100644 --- a/der/src/bytes_owned.rs +++ b/der/src/bytes_owned.rs @@ -31,18 +31,26 @@ impl BytesOwned { } /// Borrow the inner byte slice - pub fn as_slice(&self) -> &[u8] { + pub const fn as_slice(&self) -> &[u8] { &self.inner } /// Get the [`Length`] of this [`BytesRef`] - pub fn len(&self) -> Length { + pub const fn len(&self) -> Length { self.length } /// Is this [`BytesOwned`] empty? - pub fn is_empty(&self) -> bool { - self.len() == Length::ZERO + pub const fn is_empty(&self) -> bool { + self.len().is_zero() + } + + /// Create [`BytesRef`] from allocated [`BytesOwned`]. + pub const fn to_ref(&self) -> BytesRef<'_> { + BytesRef { + length: self.length, + inner: &self.inner, + } } } diff --git a/der/src/length.rs b/der/src/length.rs index e1681e2c4..15d306994 100644 --- a/der/src/length.rs +++ b/der/src/length.rs @@ -52,8 +52,9 @@ impl Length { } /// Is this length equal to zero? - pub fn is_zero(self) -> bool { - self == Self::ZERO + pub const fn is_zero(self) -> bool { + let value = self.0; + value == 0 } /// Get the length of DER Tag-Length-Value (TLV) encoded data if `self`