Skip to content

der: improved internal ref types #1921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
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
18 changes: 9 additions & 9 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ pub struct AnyRef<'a> {
tag: Tag,

/// Inner value encoded as bytes.
value: BytesRef<'a>,
value: &'a BytesRef,
}

impl<'a> AnyRef<'a> {
/// [`AnyRef`] representation of the ASN.1 `NULL` type.
pub const NULL: Self = Self {
tag: Tag::Null,
value: BytesRef::EMPTY,
value: BytesRef::new_unchecked(&[]),
};

/// Create a new [`AnyRef`] from the provided [`Tag`] and DER bytes.
Expand All @@ -48,7 +48,7 @@ impl<'a> AnyRef<'a> {
}

/// Infallible creation of an [`AnyRef`] from a [`BytesRef`].
pub(crate) fn from_tag_and_value(tag: Tag, value: BytesRef<'a>) -> Self {
pub(crate) fn from_tag_and_value(tag: Tag, value: &'a BytesRef) -> Self {
Self { tag, value }
}

Expand Down Expand Up @@ -129,7 +129,7 @@ impl<'a> DecodeValue<'a> for AnyRef<'a> {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Error> {
Ok(Self {
tag: header.tag,
value: BytesRef::decode_value(reader, header)?,
value: <&'a BytesRef>::decode_value(reader, header)?,
})
}
}
Expand All @@ -152,12 +152,12 @@ impl Tagged for AnyRef<'_> {

impl ValueOrd for AnyRef<'_> {
fn value_cmp(&self, other: &Self) -> Result<Ordering, Error> {
self.value.der_cmp(&other.value)
self.value.der_cmp(other.value)
}
}

impl<'a> From<AnyRef<'a>> for BytesRef<'a> {
fn from(any: AnyRef<'a>) -> BytesRef<'a> {
impl<'a> From<AnyRef<'a>> for &'a BytesRef {
fn from(any: AnyRef<'a>) -> &'a BytesRef {
any.value
}
}
Expand Down Expand Up @@ -256,10 +256,10 @@ mod allocating {
}

/// Create a new [`AnyRef`] from the provided [`Any`] owned tag and bytes.
pub const fn to_ref(&self) -> AnyRef<'_> {
pub fn to_ref(&self) -> AnyRef<'_> {
AnyRef {
tag: self.tag,
value: self.value.to_ref(),
value: self.value.as_ref(),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions der/src/asn1/bit_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct BitStringRef<'a> {
bit_length: usize,

/// Bitstring represented as a slice of bytes.
inner: BytesRef<'a>,
inner: &'a BytesRef,
}

impl<'a> BitStringRef<'a> {
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'a> DecodeValue<'a> for BitStringRef<'a> {
};

let unused_bits = reader.read_byte()?;
let inner = BytesRef::decode_value(reader, header)?;
let inner = <&'a BytesRef>::decode_value(reader, header)?;
Self::new(unused_bits, inner.as_slice())
}
}
Expand All @@ -164,7 +164,7 @@ impl EncodeValue for BitStringRef<'_> {
impl ValueOrd for BitStringRef<'_> {
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
match self.unused_bits.cmp(&other.unused_bits) {
Ordering::Equal => self.inner.der_cmp(&other.inner),
Ordering::Equal => self.inner.der_cmp(other.inner),
ordering => Ok(ordering),
}
}
Expand Down Expand Up @@ -233,13 +233,13 @@ impl<'a> arbitrary::Arbitrary<'a> for BitStringRef<'a> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Self::new(
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
BytesRef::arbitrary(u)?.as_slice(),
<&'a BytesRef>::arbitrary(u)?.as_slice(),
)
.map_err(|_| arbitrary::Error::IncorrectFormat)
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
arbitrary::size_hint::and(u8::size_hint(depth), <&'a BytesRef>::size_hint(depth))
}
}

Expand Down Expand Up @@ -421,13 +421,13 @@ mod allocating {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Self::new(
u.int_in_range(0..=Self::MAX_UNUSED_BITS)?,
BytesRef::arbitrary(u)?.as_slice(),
<&'a BytesRef>::arbitrary(u)?.as_slice(),
)
.map_err(|_| arbitrary::Error::IncorrectFormat)
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(u8::size_hint(depth), BytesRef::size_hint(depth))
arbitrary::size_hint::and(u8::size_hint(depth), <&'a BytesRef>::size_hint(depth))
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/general_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{BytesRef, DecodeValue, EncodeValue, FixedTag, Header, Length, Reader
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct GeneralStringRef<'a> {
/// Raw contents, unchecked
inner: BytesRef<'a>,
inner: &'a BytesRef,
}
impl<'a> GeneralStringRef<'a> {
/// This is currently `&[u8]` internally, as `GeneralString` is not fully implemented yet
Expand All @@ -21,7 +21,7 @@ impl<'a> DecodeValue<'a> for GeneralStringRef<'a> {

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error> {
Ok(Self {
inner: BytesRef::decode_value(reader, header)?,
inner: <&'a BytesRef>::decode_value(reader, header)?,
})
}
}
Expand Down
26 changes: 16 additions & 10 deletions der/src/asn1/ia5_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ macro_rules! impl_ia5_string {
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Ia5StringRef<'a> {
/// Inner value
inner: StringRef<'a>,
inner: &'a StringRef,
}

impl<'a> Ia5StringRef<'a> {
Expand All @@ -57,15 +57,20 @@ impl<'a> Ia5StringRef<'a> {
.map(|inner| Self { inner })
.map_err(|_| Self::TAG.value_error().into())
}

/// Borrow the inner `str`.
pub fn as_str(&self) -> &'a str {
self.inner.as_str()
}
}

impl_ia5_string!(Ia5StringRef<'a>, 'a);

impl<'a> Deref for Ia5StringRef<'a> {
type Target = StringRef<'a>;
type Target = StringRef;

fn deref(&self) -> &Self::Target {
&self.inner
self.inner
}
}

Expand All @@ -77,7 +82,7 @@ impl<'a> From<&Ia5StringRef<'a>> for Ia5StringRef<'a> {

impl<'a> From<Ia5StringRef<'a>> for AnyRef<'a> {
fn from(internationalized_string: Ia5StringRef<'a>) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Ia5String, internationalized_string.inner.into())
AnyRef::from_tag_and_value(Tag::Ia5String, internationalized_string.inner.as_ref())
}
}

Expand All @@ -92,7 +97,7 @@ mod allocation {
asn1::AnyRef,
referenced::{OwnedToRef, RefToOwned},
};
use alloc::string::String;
use alloc::{borrow::ToOwned, string::String};
use core::{fmt, ops::Deref};

/// ASN.1 `IA5String` type.
Expand Down Expand Up @@ -138,14 +143,15 @@ mod allocation {

impl<'a> From<Ia5StringRef<'a>> for Ia5String {
fn from(ia5_string: Ia5StringRef<'a>) -> Ia5String {
let inner = ia5_string.inner.into();
Self { inner }
Self {
inner: ia5_string.inner.to_owned(),
}
}
}

impl<'a> From<&'a Ia5String> for AnyRef<'a> {
fn from(ia5_string: &'a Ia5String) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Ia5String, (&ia5_string.inner).into())
AnyRef::from_tag_and_value(Tag::Ia5String, ia5_string.inner.as_ref())
}
}

Expand All @@ -159,7 +165,7 @@ mod allocation {
type Owned = Ia5String;
fn ref_to_owned(&self) -> Self::Owned {
Ia5String {
inner: self.inner.ref_to_owned(),
inner: self.inner.to_owned(),
}
}
}
Expand All @@ -168,7 +174,7 @@ mod allocation {
type Borrowed<'a> = Ia5StringRef<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
Ia5StringRef {
inner: self.inner.owned_to_ref(),
inner: self.inner.as_ref(),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions der/src/asn1/integer/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl_encoding_traits!(i8 => u8, i16 => u16, i32 => u32, i64 => u64, i128 => u128
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct IntRef<'a> {
/// Inner value
inner: BytesRef<'a>,
inner: &'a BytesRef,
}

impl<'a> IntRef<'a> {
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<'a> DecodeValue<'a> for IntRef<'a> {
type Error = Error;

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
let bytes = BytesRef::decode_value(reader, header)?;
let bytes = <&'a BytesRef>::decode_value(reader, header)?;
validate_canonical(bytes.as_slice())?;

let result = Self::new(bytes.as_slice())?;
Expand Down Expand Up @@ -184,7 +184,7 @@ mod allocating {
ord::OrdIsValueOrd,
referenced::{OwnedToRef, RefToOwned},
};
use alloc::vec::Vec;
use alloc::{borrow::ToOwned, vec::Vec};

/// Signed arbitrary precision ASN.1 `INTEGER` type.
///
Expand Down Expand Up @@ -288,7 +288,7 @@ mod allocating {
impl<'a> RefToOwned<'a> for IntRef<'a> {
type Owned = Int;
fn ref_to_owned(&self) -> Self::Owned {
let inner = self.inner.ref_to_owned();
let inner = self.inner.to_owned();

Int { inner }
}
Expand All @@ -297,7 +297,7 @@ mod allocating {
impl OwnedToRef for Int {
type Borrowed<'a> = IntRef<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
let inner = self.inner.owned_to_ref();
let inner = self.inner.as_ref();

IntRef { inner }
}
Expand Down
9 changes: 5 additions & 4 deletions der/src/asn1/integer/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl_encoding_traits!(u8, u16, u32, u64, u128);
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct UintRef<'a> {
/// Inner value
inner: BytesRef<'a>,
inner: &'a BytesRef,
}

impl<'a> UintRef<'a> {
Expand Down Expand Up @@ -122,7 +122,7 @@ impl<'a> DecodeValue<'a> for UintRef<'a> {
type Error = Error;

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
let bytes = BytesRef::decode_value(reader, header)?.as_slice();
let bytes = <&'a BytesRef>::decode_value(reader, header)?.as_slice();
let result = Self::new(decode_to_slice(bytes)?)?;

// Ensure we compute the same encoded length as the original any value.
Expand Down Expand Up @@ -170,6 +170,7 @@ mod allocating {
ord::OrdIsValueOrd,
referenced::{OwnedToRef, RefToOwned},
};
use alloc::borrow::ToOwned;

/// Unsigned arbitrary precision ASN.1 `INTEGER` type.
///
Expand Down Expand Up @@ -260,7 +261,7 @@ mod allocating {
impl<'a> RefToOwned<'a> for UintRef<'a> {
type Owned = Uint;
fn ref_to_owned(&self) -> Self::Owned {
let inner = self.inner.ref_to_owned();
let inner = self.inner.to_owned();

Uint { inner }
}
Expand All @@ -269,7 +270,7 @@ mod allocating {
impl OwnedToRef for Uint {
type Borrowed<'a> = UintRef<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
let inner = self.inner.owned_to_ref();
let inner = self.inner.as_ref();

UintRef { inner }
}
Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! impl_string_type {
type Error = $crate::Error;

fn decode_value<R: Reader<'__der>>(reader: &mut R, header: Header) -> $crate::Result<Self> {
Self::new(BytesRef::decode_value(reader, header)?.as_slice())
Self::new(<&'__der BytesRef>::decode_value(reader, header)?.as_slice())
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl OrdIsValueOrd for Null {}

impl<'a> From<Null> for AnyRef<'a> {
fn from(_: Null) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::Null, BytesRef::default())
AnyRef::from_tag_and_value(Tag::Null, BytesRef::EMPTY)
}
}

Expand Down
6 changes: 3 additions & 3 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct OctetStringRef<'a> {
/// Inner value
inner: BytesRef<'a>,
inner: &'a BytesRef,
}

impl<'a> OctetStringRef<'a> {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl<'a> DecodeValue<'a> for OctetStringRef<'a> {
type Error = Error;

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Error> {
let inner = BytesRef::decode_value(reader, header)?;
let inner = <&'a BytesRef>::decode_value(reader, header)?;
Ok(Self { inner })
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ mod allocating {
impl<'a> From<&'a OctetString> for OctetStringRef<'a> {
fn from(octet_string: &'a OctetString) -> OctetStringRef<'a> {
OctetStringRef {
inner: octet_string.inner.owned_to_ref(),
inner: octet_string.inner.as_ref(),
}
}
}
Expand Down
Loading