From 985dca351a3af45c6e6e7ec8a490a6dc2764aeb6 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 3 Jan 2023 12:45:53 +0100 Subject: [PATCH] a blanket impl makes this easier --- satrs-core/src/hal/host/udp_server.rs | 17 ++++++----------- satrs-core/src/tmtc/ccsds_distrib.rs | 8 +++----- satrs-core/src/tmtc/mod.rs | 13 ++++++++++--- satrs-core/src/tmtc/pus_distrib.rs | 8 +++----- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/satrs-core/src/hal/host/udp_server.rs b/satrs-core/src/hal/host/udp_server.rs index 94c5a85..70c975f 100644 --- a/satrs-core/src/hal/host/udp_server.rs +++ b/satrs-core/src/hal/host/udp_server.rs @@ -1,5 +1,5 @@ //! UDP server helper components -use crate::tmtc::{ReceivesTc, ReceivesTcBase}; +use crate::tmtc::{ReceivesTc, ReceivesTcCore}; use std::boxed::Box; use std::io::{Error, ErrorKind}; use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; @@ -19,20 +19,19 @@ use std::vec::Vec; /// ``` /// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; /// use satrs_core::hal::host::udp_server::UdpTcServer; -/// use satrs_core::tmtc::{ReceivesTc, ReceivesTcBase}; +/// use satrs_core::tmtc::{ReceivesTc, ReceivesTcCore}; /// use spacepackets::SpHeader; /// use spacepackets::tc::PusTc; /// /// #[derive (Default)] /// struct PingReceiver {} -/// impl ReceivesTcBase for PingReceiver { +/// impl ReceivesTcCore for PingReceiver { /// type Error = (); /// fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { /// assert_eq!(tc_raw.len(), 13); /// Ok(()) /// } /// } -/// impl ReceivesTc for PingReceiver {} /// /// let mut buf = [0; 32]; /// let dest_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7777); @@ -90,7 +89,7 @@ impl PartialEq for ReceiveResult { impl Eq for ReceiveResult {} -impl ReceivesTcBase for UdpTcServer { +impl ReceivesTcCore for UdpTcServer { type Error = E; fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { @@ -98,8 +97,6 @@ impl ReceivesTcBase for UdpTcServer { } } -impl ReceivesTc for UdpTcServer {} - impl UdpTcServer { pub fn new( addr: A, @@ -143,7 +140,7 @@ impl UdpTcServer { #[cfg(test)] mod tests { use crate::hal::host::udp_server::{ReceiveResult, UdpTcServer}; - use crate::tmtc::{ReceivesTc, ReceivesTcBase}; + use crate::tmtc::ReceivesTcCore; use spacepackets::tc::PusTc; use spacepackets::SpHeader; use std::boxed::Box; @@ -158,7 +155,7 @@ mod tests { pub sent_cmds: VecDeque>, } - impl ReceivesTcBase for PingReceiver { + impl ReceivesTcCore for PingReceiver { type Error = (); fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { @@ -169,8 +166,6 @@ mod tests { } } - impl ReceivesTc for PingReceiver {} - #[test] fn basic_test() { let mut buf = [0; 32]; diff --git a/satrs-core/src/tmtc/ccsds_distrib.rs b/satrs-core/src/tmtc/ccsds_distrib.rs index bf361cf..b5f0b32 100644 --- a/satrs-core/src/tmtc/ccsds_distrib.rs +++ b/satrs-core/src/tmtc/ccsds_distrib.rs @@ -19,7 +19,7 @@ //! //! ```rust //! use satrs_core::tmtc::ccsds_distrib::{CcsdsPacketHandler, CcsdsDistributor}; -//! use satrs_core::tmtc::{ReceivesTc, ReceivesTcBase}; +//! use satrs_core::tmtc::{ReceivesTc, ReceivesTcCore}; //! use spacepackets::{CcsdsPacket, SpHeader}; //! use spacepackets::tc::PusTc; //! @@ -84,7 +84,7 @@ //! .expect("Casting back to concrete type failed"); //! mutable_ref.mutable_foo(); //! ``` -use crate::tmtc::{ReceivesCcsdsTc, ReceivesTc, ReceivesTcBase}; +use crate::tmtc::{ReceivesCcsdsTc, ReceivesTcCore}; use alloc::boxed::Box; use downcast_rs::Downcast; use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader}; @@ -136,7 +136,7 @@ impl ReceivesCcsdsTc for CcsdsDistributor { } } -impl ReceivesTcBase for CcsdsDistributor { +impl ReceivesTcCore for CcsdsDistributor { type Error = CcsdsError; fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { @@ -154,8 +154,6 @@ impl ReceivesTcBase for CcsdsDistributor { } } -impl ReceivesTc for CcsdsDistributor {} - impl CcsdsDistributor { pub fn new(apid_handler: Box>) -> Self { CcsdsDistributor { apid_handler } diff --git a/satrs-core/src/tmtc/mod.rs b/satrs-core/src/tmtc/mod.rs index c4e578e..1ae4ac8 100644 --- a/satrs-core/src/tmtc/mod.rs +++ b/satrs-core/src/tmtc/mod.rs @@ -63,17 +63,24 @@ impl AddressableId { /// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the /// [crate::tmtc::ccsds_distrib::CcsdsDistributor] which allows to pass the respective packets in /// raw byte format into them. -pub trait ReceivesTcBase: Send { +pub trait ReceivesTcCore: Send { type Error; fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error>; } +/// Extension trait of [ReceivesTcCore] which allows downcasting by implementing [Downcast] #[cfg(feature = "alloc")] -pub trait ReceivesTc: ReceivesTcBase + Downcast {} +pub trait ReceivesTc: ReceivesTcCore + Downcast {} + +/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature +/// is enabled. +#[cfg(feature = "alloc")] +impl ReceivesTc for T where T: ReceivesTcCore + 'static {} + #[cfg(feature = "alloc")] impl_downcast!(ReceivesTc assoc Error); -/// Generic trait for object which can receive CCSDS space packets, for fsrc-example ECSS PUS packets +/// Generic trait for object which can receive CCSDS space packets, for example ECSS PUS packets /// for CCSDS File Delivery Protocol (CFDP) packets. /// /// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the diff --git a/satrs-core/src/tmtc/pus_distrib.rs b/satrs-core/src/tmtc/pus_distrib.rs index 2947389..d193478 100644 --- a/satrs-core/src/tmtc/pus_distrib.rs +++ b/satrs-core/src/tmtc/pus_distrib.rs @@ -18,7 +18,7 @@ //! //! ```rust //! use satrs_core::tmtc::pus_distrib::{PusDistributor, PusServiceProvider}; -//! use satrs_core::tmtc::{ReceivesTc, ReceivesTcBase}; +//! use satrs_core::tmtc::{ReceivesTc, ReceivesTcCore}; //! use spacepackets::SpHeader; //! use spacepackets::tc::PusTc; //! struct ConcretePusHandler { @@ -60,7 +60,7 @@ //! .expect("Casting back to concrete type failed"); //! assert_eq!(concrete_handler_ref.handler_call_count, 1); //! ``` -use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTc, ReceivesTcBase}; +use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTcCore}; use alloc::boxed::Box; use downcast_rs::Downcast; use spacepackets::ecss::{PusError, PusPacket}; @@ -94,7 +94,7 @@ pub enum PusDistribError { PusError(PusError), } -impl ReceivesTcBase for PusDistributor { +impl ReceivesTcCore for PusDistributor { type Error = PusDistribError; fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> { // Convert to ccsds and call pass_ccsds @@ -104,8 +104,6 @@ impl ReceivesTcBase for PusDistributor { } } -impl ReceivesTc for PusDistributor {} - impl ReceivesCcsdsTc for PusDistributor { type Error = PusDistribError; fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), Self::Error> {