diff --git a/der/src/asn1.rs b/der/src/asn1.rs index 9717b395f..ff2dc5bf7 100644 --- a/der/src/asn1.rs +++ b/der/src/asn1.rs @@ -42,7 +42,7 @@ pub use self::{ general_string::GeneralStringRef, generalized_time::GeneralizedTime, ia5_string::Ia5StringRef, - integer::{int::IntRef, uint::UintRef}, + integer::{AsIntRef, AsUintRef, int::IntRef, uint::UintRef}, null::Null, octet_string::OctetStringRef, printable_string::PrintableStringRef, diff --git a/der/src/asn1/integer.rs b/der/src/asn1/integer.rs index e00e3fcd7..c7dbc5fc6 100644 --- a/der/src/asn1/integer.rs +++ b/der/src/asn1/integer.rs @@ -3,6 +3,9 @@ pub(super) mod int; pub(super) mod uint; +use int::IntRef; +use uint::UintRef; + use core::{cmp::Ordering, mem::size_of}; use crate::{EncodeValue, Result, encode::encode_value_to_slice}; @@ -30,6 +33,20 @@ where Ok(buf1.cmp(buf2)) } +/// Borrow the owned or ref `INTEGER` as [`IntRef`] +pub trait AsIntRef { + /// Borrows the owned or ref `INTEGER` as [`IntRef`] + #[must_use] + fn as_int_ref<'a>(&'a self) -> IntRef<'a>; +} + +/// Borrow the owned or ref `INTEGER` as [`UintRef`] +pub trait AsUintRef { + /// Borrows the owned or ref `INTEGER` as [`UintRef`] + #[must_use] + fn as_uint_ref<'a>(&'a self) -> UintRef<'a>; +} + #[cfg(test)] #[allow(clippy::unwrap_used)] pub(crate) mod tests { diff --git a/der/src/asn1/integer/int.rs b/der/src/asn1/integer/int.rs index 0da6c93de..540fced5f 100644 --- a/der/src/asn1/integer/int.rs +++ b/der/src/asn1/integer/int.rs @@ -3,7 +3,7 @@ use super::{is_highest_bit_set, uint, value_cmp}; use crate::{ AnyRef, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, - Result, Tag, ValueOrd, Writer, ord::OrdIsValueOrd, + Result, Tag, ValueOrd, Writer, asn1::integer::AsIntRef, ord::OrdIsValueOrd, }; use core::cmp::Ordering; @@ -180,13 +180,19 @@ impl FixedTag for IntRef<'_> { impl OrdIsValueOrd for IntRef<'_> {} +impl AsIntRef for IntRef<'_> { + fn as_int_ref<'a>(&'a self) -> IntRef<'a> { + *self + } +} + #[cfg(feature = "alloc")] mod allocating { use super::{IntRef, strip_leading_ones, validate_canonical}; use crate::{ BytesOwned, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, Result, Tag, Writer, - asn1::Uint, + asn1::{Uint, integer::AsIntRef}, ord::OrdIsValueOrd, referenced::{OwnedToRef, RefToOwned}, }; @@ -237,6 +243,14 @@ mod allocating { } } + impl AsIntRef for Int { + fn as_int_ref<'a>(&'a self) -> IntRef<'a> { + let inner = self.inner.as_ref(); + + IntRef { inner } + } + } + impl_any_conversions!(Int); impl<'a> DecodeValue<'a> for Int { diff --git a/der/src/asn1/integer/uint.rs b/der/src/asn1/integer/uint.rs index c0b6f87c9..9f6d552e6 100644 --- a/der/src/asn1/integer/uint.rs +++ b/der/src/asn1/integer/uint.rs @@ -3,7 +3,7 @@ use super::value_cmp; use crate::{ AnyRef, BytesRef, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, - Result, Tag, ValueOrd, Writer, ord::OrdIsValueOrd, + Result, Tag, ValueOrd, Writer, asn1::integer::AsUintRef, ord::OrdIsValueOrd, }; use core::cmp::Ordering; @@ -169,12 +169,19 @@ impl FixedTag for UintRef<'_> { impl OrdIsValueOrd for UintRef<'_> {} +impl AsUintRef for UintRef<'_> { + fn as_uint_ref<'a>(&'a self) -> UintRef<'a> { + *self + } +} + #[cfg(feature = "alloc")] mod allocating { use super::{UintRef, decode_to_slice, encoded_len, strip_leading_zeroes}; use crate::{ BytesOwned, DecodeValue, EncodeValue, Error, ErrorKind, FixedTag, Header, Length, Reader, Result, Tag, Writer, + asn1::integer::AsUintRef, ord::OrdIsValueOrd, referenced::{OwnedToRef, RefToOwned}, }; @@ -290,6 +297,14 @@ mod allocating { } } + impl AsUintRef for Uint { + fn as_uint_ref<'a>(&'a self) -> UintRef<'a> { + let inner = self.inner.as_ref(); + + UintRef { inner } + } + } + macro_rules! impl_from_traits { ($($uint:ty),+) => { $(