Skip to content
Merged
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
10 changes: 9 additions & 1 deletion der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}

Expand Down
16 changes: 12 additions & 4 deletions der/src/bytes_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions der/src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down