From 0a5071d34b8298ba8664764337c90aa4c8d6682c Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Wed, 22 May 2024 20:23:59 -0700 Subject: [PATCH] der: introduce an `AnyLike` trait to mark parameters --- der/src/asn1.rs | 2 +- der/src/asn1/any.rs | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/der/src/asn1.rs b/der/src/asn1.rs index b04b1b58f..f04d1803f 100644 --- a/der/src/asn1.rs +++ b/der/src/asn1.rs @@ -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}, diff --git a/der/src/asn1/any.rs b/der/src/asn1/any.rs index 1ed9286c0..93a2e4409 100644 --- a/der/src/asn1/any.rs +++ b/der/src/asn1/any.rs @@ -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. @@ -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(self, f: F) -> Result @@ -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 @@ -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 } }