From b0bb1b7b39a6e68ef28b10df8ba33ee15bfc85ed Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 25 Mar 2022 09:23:32 -0600 Subject: [PATCH] der: add `DecodeOwned` marker trait Adds a marker trait for types which can be `der::Decode`d without borrowing any data from the input. It's useful for cases where borrowing from the input isn't possible. For example, if we were to add on-the-fly PEM decoding as proposed in #525. --- der/src/decode.rs | 12 ++++++++++++ der/src/lib.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/der/src/decode.rs b/der/src/decode.rs index 61ee2fe5c..a3e854fd0 100644 --- a/der/src/decode.rs +++ b/der/src/decode.rs @@ -35,6 +35,18 @@ where } } +/// Marker trait for data structures that can be decoded from DER without +/// borrowing any data from the decoder. +/// +/// This is primarily useful for trait bounds on functions which require that +/// no data is borrowed from the decoder, for example a PEM decoder which needs +/// to first decode data from Base64. +/// +/// This trait is inspired by the [`DeserializeOwned` trait from `serde`](https://docs.rs/serde/latest/serde/de/trait.DeserializeOwned.html). +pub trait DecodeOwned: for<'a> Decode<'a> {} + +impl DecodeOwned for T where T: for<'a> Decode<'a> {} + /// Decode the value part of a Tag-Length-Value encoded field, sans the [`Tag`] /// and [`Length`]. pub trait DecodeValue<'a>: Sized { diff --git a/der/src/lib.rs b/der/src/lib.rs index 212332ddd..12ac3877e 100644 --- a/der/src/lib.rs +++ b/der/src/lib.rs @@ -353,7 +353,7 @@ mod document; pub use crate::{ asn1::{Any, Choice, Sequence}, datetime::DateTime, - decode::{Decode, DecodeValue}, + decode::{Decode, DecodeOwned, DecodeValue}, decoder::Decoder, encode::{Encode, EncodeValue}, encoder::Encoder,