Skip to content

Commit

Permalink
tls_codec: implement traits for Box<T> (#1586)
Browse files Browse the repository at this point in the history
* implement traits for Box<T>
* restrict impls to "std"
* allow use of alloc::boxed::Box
  • Loading branch information
kkohbrok authored Nov 1, 2024
1 parent 59dc02b commit 2492851
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion tls_codec/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Codec implementations for unsigned integer primitives.
use alloc::vec::Vec;
use alloc::{boxed::Box, vec::Vec};

use crate::{DeserializeBytes, SerializeBytes, U24};

Expand Down Expand Up @@ -371,3 +371,40 @@ impl<T> SerializeBytes for PhantomData<T> {
Ok(vec![])
}
}

impl<T: Size> Size for Box<T> {
#[inline(always)]
fn tls_serialized_len(&self) -> usize {
self.as_ref().tls_serialized_len()
}
}

impl<T: Serialize> Serialize for Box<T> {
#[cfg(feature = "std")]
#[inline(always)]
fn tls_serialize<W: Write>(&self, writer: &mut W) -> Result<usize, Error> {
self.as_ref().tls_serialize(writer)
}
}

impl<T: SerializeBytes> SerializeBytes for Box<T> {
#[inline(always)]
fn tls_serialize(&self) -> Result<Vec<u8>, Error> {
self.as_ref().tls_serialize()
}
}

impl<T: Deserialize> Deserialize for Box<T> {
#[cfg(feature = "std")]
#[inline(always)]
fn tls_deserialize<R: Read>(bytes: &mut R) -> Result<Self, Error> {
T::tls_deserialize(bytes).map(Box::new)
}
}

impl<T: DeserializeBytes> DeserializeBytes for Box<T> {
#[inline(always)]
fn tls_deserialize_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
T::tls_deserialize_bytes(bytes).map(|(v, r)| (Box::new(v), r))
}
}

0 comments on commit 2492851

Please sign in to comment.