Skip to content

Commit 2da5955

Browse files
authored
der: conversions from OctetString(Ref) to Vec/Bytes (#1540)
Adds `From` conversions between these types
1 parent d2effd1 commit 2da5955

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

der/src/asn1/octet_string.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ mod allocating {
204204
}
205205
}
206206

207+
impl From<OctetStringRef<'_>> for Vec<u8> {
208+
fn from(octet_string: OctetStringRef<'_>) -> Vec<u8> {
209+
Vec::from(octet_string.as_bytes())
210+
}
211+
}
212+
213+
impl From<OctetString> for Vec<u8> {
214+
fn from(octet_string: OctetString) -> Vec<u8> {
215+
octet_string.into_bytes()
216+
}
217+
}
218+
207219
// Implement by hand because the derive would create invalid values.
208220
// Use the constructor to create a valid value.
209221
#[cfg(feature = "arbitrary")]
@@ -220,10 +232,11 @@ mod allocating {
220232

221233
#[cfg(feature = "bytes")]
222234
mod bytes {
223-
use super::OctetString;
235+
use super::{OctetString, OctetStringRef};
224236
use crate::{
225237
DecodeValue, EncodeValue, Error, FixedTag, Header, Length, Reader, Result, Tag, Writer,
226238
};
239+
use alloc::vec::Vec;
227240
use bytes::Bytes;
228241

229242
impl<'a> DecodeValue<'a> for Bytes {
@@ -247,6 +260,18 @@ mod bytes {
247260
impl FixedTag for Bytes {
248261
const TAG: Tag = Tag::OctetString;
249262
}
263+
264+
impl From<OctetStringRef<'_>> for Bytes {
265+
fn from(octet_string: OctetStringRef<'_>) -> Bytes {
266+
Vec::from(octet_string).into()
267+
}
268+
}
269+
270+
impl From<OctetString> for Bytes {
271+
fn from(octet_string: OctetString) -> Bytes {
272+
Vec::from(octet_string).into()
273+
}
274+
}
250275
}
251276

252277
#[cfg(test)]

0 commit comments

Comments
 (0)