Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion der/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions der/src/asn1/bit_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitStringRef>`], 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;
Expand Down Expand Up @@ -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`].
Expand Down