diff --git a/der/src/asn1.rs b/der/src/asn1.rs index 9717b395f..0546ed969 100644 --- a/der/src/asn1.rs +++ b/der/src/asn1.rs @@ -36,7 +36,7 @@ mod videotex_string; pub use self::{ any::AnyRef, application::{Application, ApplicationRef}, - bit_string::{BitStringIter, BitStringRef}, + bit_string::{AsBitStringRef, BitStringIter, BitStringRef}, choice::Choice, context_specific::{ContextSpecific, ContextSpecificRef}, general_string::GeneralStringRef, diff --git a/der/src/asn1/bit_string.rs b/der/src/asn1/bit_string.rs index 3ce19f169..a6472f396 100644 --- a/der/src/asn1/bit_string.rs +++ b/der/src/asn1/bit_string.rs @@ -248,6 +248,23 @@ impl FixedTag for BitStringRef<'_> { const TAG: Tag = Tag::BitString; } +/// [`AsBitStringRef`] marks object that will act like a `BitString`. +/// +/// It will allow to get a [`BitStringRef`] that points back to the underlying bytes. +/// +/// Note: this trait does not simplify to [`AsRef`], because `BIT STRING` +/// contains unused bits prefix and therefore cannot be a simple `&'a BitStringRef` slice. +pub trait AsBitStringRef { + /// Borrows the owned or ref `BIT STRING` as [`BitStringRef`] + fn as_bit_string(&self) -> BitStringRef<'_>; +} + +impl AsBitStringRef for BitStringRef<'_> { + fn as_bit_string(&self) -> BitStringRef<'_> { + *self + } +} + /// Sealed, so that `UnusedBits` newtype can't be created directly mod unused_bits { use core::ops::Deref; @@ -557,6 +574,12 @@ mod allocating { self.into() } } + + impl AsBitStringRef for BitString { + fn as_bit_string(&self) -> BitStringRef<'_> { + BitStringRef::from(self) + } + } } /// Iterator over the bits of a [`BitString`].