From 2da59557216e92863d5f66ccc469e2e23d41c0ee Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Tue, 1 Oct 2024 12:36:08 -0600 Subject: [PATCH] der: conversions from `OctetString(Ref)` to `Vec`/`Bytes` (#1540) Adds `From` conversions between these types --- der/src/asn1/octet_string.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/der/src/asn1/octet_string.rs b/der/src/asn1/octet_string.rs index 3a0a87a2d..790122d93 100644 --- a/der/src/asn1/octet_string.rs +++ b/der/src/asn1/octet_string.rs @@ -204,6 +204,18 @@ mod allocating { } } + impl From> for Vec { + fn from(octet_string: OctetStringRef<'_>) -> Vec { + Vec::from(octet_string.as_bytes()) + } + } + + impl From for Vec { + fn from(octet_string: OctetString) -> Vec { + octet_string.into_bytes() + } + } + // Implement by hand because the derive would create invalid values. // Use the constructor to create a valid value. #[cfg(feature = "arbitrary")] @@ -220,10 +232,11 @@ mod allocating { #[cfg(feature = "bytes")] mod bytes { - use super::OctetString; + use super::{OctetString, OctetStringRef}; use crate::{ DecodeValue, EncodeValue, Error, FixedTag, Header, Length, Reader, Result, Tag, Writer, }; + use alloc::vec::Vec; use bytes::Bytes; impl<'a> DecodeValue<'a> for Bytes { @@ -247,6 +260,18 @@ mod bytes { impl FixedTag for Bytes { const TAG: Tag = Tag::OctetString; } + + impl From> for Bytes { + fn from(octet_string: OctetStringRef<'_>) -> Bytes { + Vec::from(octet_string).into() + } + } + + impl From for Bytes { + fn from(octet_string: OctetString) -> Bytes { + Vec::from(octet_string).into() + } + } } #[cfg(test)]