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
12 changes: 12 additions & 0 deletions der/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Trait definition for [`Decode`].

use crate::{FixedTag, Header, Reader, Result, SliceReader};
use core::marker::PhantomData;

#[cfg(feature = "pem")]
use crate::{pem::PemLabel, PemReader};
Expand Down Expand Up @@ -38,6 +39,17 @@ where
}
}

/// Dummy implementation for [`PhantomData`] which allows deriving
/// implementations on structs with phantom fields.
impl<'a, T> Decode<'a> for PhantomData<T>
where
T: ?Sized,
{
fn decode<R: Reader<'a>>(_reader: &mut R) -> Result<PhantomData<T>> {
Ok(PhantomData)
}
}

/// Marker trait for data structures that can be decoded from DER without
/// borrowing any data from the decoder.
///
Expand Down
16 changes: 16 additions & 0 deletions der/src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Trait definition for [`Encode`].

use crate::{Header, Length, Result, SliceWriter, Tagged, Writer};
use core::marker::PhantomData;

#[cfg(feature = "alloc")]
use {alloc::boxed::Box, alloc::vec::Vec, core::iter};
Expand Down Expand Up @@ -82,6 +83,21 @@ where
}
}

/// Dummy implementation for [`PhantomData`] which allows deriving
/// implementations on structs with phantom fields.
impl<T> Encode for PhantomData<T>
where
T: ?Sized,
{
fn encoded_len(&self) -> Result<Length> {
Ok(Length::ZERO)
}

fn encode(&self, _writer: &mut impl Writer) -> Result<()> {
Ok(())
}
}

/// PEM encoding trait.
///
/// This trait is automatically impl'd for any type which impls both
Expand Down
4 changes: 4 additions & 0 deletions der/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ mod enumerated {
/// Custom derive test cases for the `Sequence` macro.
#[cfg(feature = "oid")]
mod sequence {
use core::marker::PhantomData;
use der::{
asn1::{AnyRef, ObjectIdentifier, SetOf},
Decode, Encode, Sequence, ValueOrd,
Expand Down Expand Up @@ -266,6 +267,9 @@ mod sequence {
tag_mode = "IMPLICIT"
)]
pub only_contains_attribute_certs: bool,

/// Test handling of PhantomData.
pub phantom: PhantomData<()>,
Comment on lines +271 to +272
Copy link
Member Author

Choose a reason for hiding this comment

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

Added this as a spot check. It seems to work?

}

// Extension as defined in [RFC 5280 Section 4.1.2.9].
Expand Down