Skip to content

Commit

Permalink
der: conversions from OctetString(Ref) to Vec/Bytes
Browse files Browse the repository at this point in the history
Adds `From` conversions between these types
  • Loading branch information
tarcieri committed Oct 1, 2024
1 parent 5e2fe89 commit 6a64b93
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ mod allocating {
}
}

impl From<OctetStringRef<'_>> for Vec<u8> {
fn from(octet_string: OctetStringRef<'_>) -> Vec<u8> {
Vec::from(octet_string.as_bytes())
}
}

impl From<OctetString> for Vec<u8> {
fn from(octet_string: OctetString) -> Vec<u8> {
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")]
Expand All @@ -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 {
Expand All @@ -247,6 +260,18 @@ mod bytes {
impl FixedTag for Bytes {
const TAG: Tag = Tag::OctetString;
}

impl From<OctetStringRef<'_>> for Bytes {
fn from(octet_string: OctetStringRef<'_>) -> Bytes {
Vec::from(octet_string).into()
}
}

impl From<OctetString> for Bytes {
fn from(octet_string: OctetString) -> Bytes {
Vec::from(octet_string).into()
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 6a64b93

Please sign in to comment.