Skip to content
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
14 changes: 10 additions & 4 deletions der/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,20 @@ where
}

#[cfg(feature = "alloc")]
impl<'a, T> DecodeValue<'a> for Cow<'a, T>
impl<'a, T, E> DecodeValue<'a> for Cow<'a, T>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still somewhat annoyingly ties the lifetime of the Cow to the lifetime of the Reader, which for PemReader is still the lifetime of the input buffer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, that's easily fixable by making it impl Reader<'static> instead

where
T: ToOwned + ?Sized,
&'a T: DecodeValue<'a>,
&'a T: DecodeValue<'a, Error = E>,
T::Owned: for<'b> DecodeValue<'b, Error = E>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, it's HRTB

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's really necessary here to show that the output lifetime is independent of the input lifetimes, which should let Reader<'static> work when decoding Cow<'static, T>

E: From<Error> + 'static,
{
type Error = <&'a T as DecodeValue<'a>>::Error;
type Error = E;

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Self::Error> {
Ok(Cow::Borrowed(<&'a T>::decode_value(reader, header)?))
if R::CAN_READ_SLICE {
<&'a T>::decode_value(reader, header).map(Cow::Borrowed)
} else {
T::Owned::decode_value(reader, header).map(Cow::Owned)
}
}
}
3 changes: 3 additions & 0 deletions der/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use crate::length::indefinite::read_eoc;

/// Reader trait which reads DER-encoded input.
pub trait Reader<'r>: Clone {
/// Does this reader support the `read_slice` method? (i.e. can it borrow from his input?)
const CAN_READ_SLICE: bool;

/// Get the [`EncodingRules`] which should be applied when decoding the input.
fn encoding_rules(&self) -> EncodingRules;

Expand Down
2 changes: 2 additions & 0 deletions der/src/reader/pem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ impl<'i> PemReader<'i> {

#[cfg(feature = "pem")]
impl<'i> Reader<'i> for PemReader<'i> {
const CAN_READ_SLICE: bool = false;

fn encoding_rules(&self) -> EncodingRules {
self.encoding_rules
}
Expand Down
2 changes: 2 additions & 0 deletions der/src/reader/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl<'a> SliceReader<'a> {
}

impl<'a> Reader<'a> for SliceReader<'a> {
const CAN_READ_SLICE: bool = true;

fn encoding_rules(&self) -> EncodingRules {
self.encoding_rules
}
Expand Down