From a7b97e22f5c7a183372bd30933e66b8bf3fc6a7b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 8 Aug 2022 01:23:38 +0200 Subject: [PATCH] simplifications + helper function --- src/lib.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5b2e1df..19c7b2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -369,6 +369,17 @@ impl SpHeader { pub fn set_packet_type(&mut self, packet_type: PacketType) { self.packet_id.ptype = packet_type; } + + pub fn from_raw_slice(buf: &[u8]) -> Result { + if buf.len() < CCSDS_HEADER_LEN + 1 { + return Err(PacketError::FromBytesSliceTooSmall(SizeMissmatch { + found: buf.len(), + expected: CCSDS_HEADER_LEN + 1, + })); + } + let zc_header = zc::SpHeader::from_bytes(buf).ok_or(PacketError::FromBytesZeroCopyError)?; + Ok(Self::from(zc_header)) + } } impl CcsdsPacket for SpHeader { @@ -446,12 +457,12 @@ pub mod zc { } } - pub fn from_bytes(slice: &(impl AsRef<[u8]> + ?Sized)) -> Option { - SpHeader::read_from(slice.as_ref()) + pub fn from_bytes(slice: &[u8]) -> Option { + SpHeader::read_from(slice) } - pub fn to_bytes(&self, slice: &mut (impl AsMut<[u8]> + ?Sized)) -> Option<()> { - self.write_to(slice.as_mut()) + pub fn to_bytes(&self, slice: &mut [u8]) -> Option<()> { + self.write_to(slice) } }