diff --git a/der/src/decode.rs b/der/src/decode.rs index 0efcae69f..fe53341b3 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -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}; @@ -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 +where + T: ?Sized, +{ + fn decode>(_reader: &mut R) -> Result> { + Ok(PhantomData) + } +} + /// Marker trait for data structures that can be decoded from DER without /// borrowing any data from the decoder. /// diff --git a/der/src/encode.rs b/der/src/encode.rs index d480b7bbd..28d7cba77 100644 --- a/der/src/encode.rs +++ b/der/src/encode.rs @@ -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}; @@ -82,6 +83,21 @@ where } } +/// Dummy implementation for [`PhantomData`] which allows deriving +/// implementations on structs with phantom fields. +impl Encode for PhantomData +where + T: ?Sized, +{ + fn encoded_len(&self) -> Result { + 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 diff --git a/der/tests/derive.rs b/der/tests/derive.rs index cb6fe183d..a8c77febc 100644 --- a/der/tests/derive.rs +++ b/der/tests/derive.rs @@ -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, @@ -266,6 +267,9 @@ mod sequence { tag_mode = "IMPLICIT" )] pub only_contains_attribute_certs: bool, + + /// Test handling of PhantomData. + pub phantom: PhantomData<()>, } // Extension as defined in [RFC 5280 Section 4.1.2.9].