From 76ad1c7eada8987678d5fd3ee48f714cff358943 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 26 Sep 2023 16:55:07 +0200 Subject: [PATCH] Added to_vec method for SerializablePusPacket --- CHANGELOG.md | 1 + src/ecss/mod.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca03124..de84770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added - `PacketId` trait impls: `Ord`, `PartialOrd` and `Hash` +- `SerializablePusPacket` trait: Add `to_vec` method with default implementation. # [v0.7.0-beta.1] 2023-08-28 diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index 39c4545..983ad02 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -4,6 +4,8 @@ //! You can find the PUS telecommand definitions in the [tc] module and ithe PUS telemetry definitions //! inside the [tm] module. use crate::{ByteConversionError, CcsdsPacket, CRC_CCITT_FALSE}; +#[cfg(feature = "alloc")] +use alloc::vec::Vec; use core::fmt::{Debug, Display, Formatter}; use core::mem::size_of; use num_enum::{IntoPrimitive, TryFromPrimitive}; @@ -361,6 +363,15 @@ pub type EcssEnumU64 = GenericEcssEnumWrapper; pub trait SerializablePusPacket { fn len_packed(&self) -> usize; fn write_to_bytes(&self, slice: &mut [u8]) -> Result; + #[cfg(feature = "alloc")] + fn to_vec(&self) -> Result, PusError> { + // This is the correct way to do this. See + // [this issue](https://github.com/rust-lang/rust-clippy/issues/4483) for caveats of more + // "efficient" implementations. + let mut vec = alloc::vec![0; self.len_packed()]; + self.write_to_bytes(&mut vec)?; + Ok(vec) + } } #[cfg(test)]