Skip to content

Commit

Permalink
der: introduce an AnyLike trait to mark parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed May 23, 2024
1 parent 6c42217 commit 0a5071d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion der/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod utf8_string;
mod videotex_string;

pub use self::{
any::AnyRef,
any::{AnyLike, AnyRef},
bit_string::{BitStringIter, BitStringRef},
choice::Choice,
context_specific::{ContextSpecific, ContextSpecificRef},
Expand Down
22 changes: 14 additions & 8 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use core::cmp::Ordering;
#[cfg(feature = "alloc")]
use crate::SliceWriter;

/// Trait representing value that will be serialized as Any
pub trait AnyLike {
/// Is this value an ASN.1 `NULL` value?
fn is_null(&self) -> bool;
}

/// ASN.1 `ANY`: represents any explicitly tagged ASN.1 value.
///
/// This is a zero-copy reference type which borrows from the input data.
Expand Down Expand Up @@ -74,11 +80,6 @@ impl<'a> AnyRef<'a> {
Ok(decoder.finish(result)?)
}

/// Is this value an ASN.1 `NULL` value?
pub fn is_null(self) -> bool {
self == Self::NULL
}

/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
/// nested reader and calling the provided argument with it.
pub fn sequence<F, T, E>(self, f: F) -> Result<T, E>
Expand All @@ -93,6 +94,12 @@ impl<'a> AnyRef<'a> {
}
}

impl<'a> AnyLike for AnyRef<'a> {
fn is_null(&self) -> bool {
*self == Self::NULL
}
}

impl<'a> Choice<'a> for AnyRef<'a> {
fn can_decode(_: Tag) -> bool {
true
Expand Down Expand Up @@ -316,9 +323,8 @@ mod allocating {
}
}

impl Any {
/// Is this value an ASN.1 `NULL` value?
pub fn is_null(&self) -> bool {
impl AnyLike for Any {
fn is_null(&self) -> bool {
self.owned_to_ref() == AnyRef::NULL
}
}
Expand Down

0 comments on commit 0a5071d

Please sign in to comment.