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 @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions der/src/asn1/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 16 additions & 2 deletions der/src/asn1/integer/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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},
};
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion der/src/asn1/integer/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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},
};
Expand Down Expand Up @@ -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),+) => {
$(
Expand Down