a blanket impl makes this easier

This commit is contained in:
Robin Müller 2023-01-03 12:45:53 +01:00
parent 61303a9841
commit 985dca351a
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
4 changed files with 22 additions and 24 deletions

View File

@ -1,5 +1,5 @@
//! UDP server helper components //! UDP server helper components
use crate::tmtc::{ReceivesTc, ReceivesTcBase}; use crate::tmtc::{ReceivesTc, ReceivesTcCore};
use std::boxed::Box; use std::boxed::Box;
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket}; use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
@ -19,20 +19,19 @@ use std::vec::Vec;
/// ``` /// ```
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; /// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
/// use satrs_core::hal::host::udp_server::UdpTcServer; /// 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::SpHeader;
/// use spacepackets::tc::PusTc; /// use spacepackets::tc::PusTc;
/// ///
/// #[derive (Default)] /// #[derive (Default)]
/// struct PingReceiver {} /// struct PingReceiver {}
/// impl ReceivesTcBase for PingReceiver { /// impl ReceivesTcCore for PingReceiver {
/// type Error = (); /// type Error = ();
/// fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { /// fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
/// assert_eq!(tc_raw.len(), 13); /// assert_eq!(tc_raw.len(), 13);
/// Ok(()) /// Ok(())
/// } /// }
/// } /// }
/// impl ReceivesTc for PingReceiver {}
/// ///
/// let mut buf = [0; 32]; /// let mut buf = [0; 32];
/// let dest_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7777); /// let dest_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 7777);
@ -90,7 +89,7 @@ impl<E: PartialEq> PartialEq for ReceiveResult<E> {
impl<E: Eq + PartialEq> Eq for ReceiveResult<E> {} impl<E: Eq + PartialEq> Eq for ReceiveResult<E> {}
impl<E: 'static> ReceivesTcBase for UdpTcServer<E> { impl<E: 'static> ReceivesTcCore for UdpTcServer<E> {
type Error = E; type Error = E;
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
@ -98,8 +97,6 @@ impl<E: 'static> ReceivesTcBase for UdpTcServer<E> {
} }
} }
impl<E: 'static> ReceivesTc for UdpTcServer<E> {}
impl<E: 'static> UdpTcServer<E> { impl<E: 'static> UdpTcServer<E> {
pub fn new<A: ToSocketAddrs>( pub fn new<A: ToSocketAddrs>(
addr: A, addr: A,
@ -143,7 +140,7 @@ impl<E: 'static> UdpTcServer<E> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::hal::host::udp_server::{ReceiveResult, UdpTcServer}; use crate::hal::host::udp_server::{ReceiveResult, UdpTcServer};
use crate::tmtc::{ReceivesTc, ReceivesTcBase}; use crate::tmtc::ReceivesTcCore;
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::SpHeader; use spacepackets::SpHeader;
use std::boxed::Box; use std::boxed::Box;
@ -158,7 +155,7 @@ mod tests {
pub sent_cmds: VecDeque<Vec<u8>>, pub sent_cmds: VecDeque<Vec<u8>>,
} }
impl ReceivesTcBase for PingReceiver { impl ReceivesTcCore for PingReceiver {
type Error = (); type Error = ();
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
@ -169,8 +166,6 @@ mod tests {
} }
} }
impl ReceivesTc for PingReceiver {}
#[test] #[test]
fn basic_test() { fn basic_test() {
let mut buf = [0; 32]; let mut buf = [0; 32];

View File

@ -19,7 +19,7 @@
//! //!
//! ```rust //! ```rust
//! use satrs_core::tmtc::ccsds_distrib::{CcsdsPacketHandler, CcsdsDistributor}; //! 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::{CcsdsPacket, SpHeader};
//! use spacepackets::tc::PusTc; //! use spacepackets::tc::PusTc;
//! //!
@ -84,7 +84,7 @@
//! .expect("Casting back to concrete type failed"); //! .expect("Casting back to concrete type failed");
//! mutable_ref.mutable_foo(); //! mutable_ref.mutable_foo();
//! ``` //! ```
use crate::tmtc::{ReceivesCcsdsTc, ReceivesTc, ReceivesTcBase}; use crate::tmtc::{ReceivesCcsdsTc, ReceivesTcCore};
use alloc::boxed::Box; use alloc::boxed::Box;
use downcast_rs::Downcast; use downcast_rs::Downcast;
use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader}; use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader};
@ -136,7 +136,7 @@ impl<E: 'static> ReceivesCcsdsTc for CcsdsDistributor<E> {
} }
} }
impl<E: 'static> ReceivesTcBase for CcsdsDistributor<E> { impl<E: 'static> ReceivesTcCore for CcsdsDistributor<E> {
type Error = CcsdsError<E>; type Error = CcsdsError<E>;
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
@ -154,8 +154,6 @@ impl<E: 'static> ReceivesTcBase for CcsdsDistributor<E> {
} }
} }
impl<E: 'static> ReceivesTc for CcsdsDistributor<E> {}
impl<E: 'static> CcsdsDistributor<E> { impl<E: 'static> CcsdsDistributor<E> {
pub fn new(apid_handler: Box<dyn CcsdsPacketHandler<Error = E>>) -> Self { pub fn new(apid_handler: Box<dyn CcsdsPacketHandler<Error = E>>) -> Self {
CcsdsDistributor { apid_handler } CcsdsDistributor { apid_handler }

View File

@ -63,17 +63,24 @@ impl AddressableId {
/// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the /// 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 /// [crate::tmtc::ccsds_distrib::CcsdsDistributor] which allows to pass the respective packets in
/// raw byte format into them. /// raw byte format into them.
pub trait ReceivesTcBase: Send { pub trait ReceivesTcCore: Send {
type Error; type Error;
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::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")] #[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<T> ReceivesTc for T where T: ReceivesTcCore + 'static {}
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
impl_downcast!(ReceivesTc assoc Error); 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. /// for CCSDS File Delivery Protocol (CFDP) packets.
/// ///
/// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the /// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the

View File

@ -18,7 +18,7 @@
//! //!
//! ```rust //! ```rust
//! use satrs_core::tmtc::pus_distrib::{PusDistributor, PusServiceProvider}; //! 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::SpHeader;
//! use spacepackets::tc::PusTc; //! use spacepackets::tc::PusTc;
//! struct ConcretePusHandler { //! struct ConcretePusHandler {
@ -60,7 +60,7 @@
//! .expect("Casting back to concrete type failed"); //! .expect("Casting back to concrete type failed");
//! assert_eq!(concrete_handler_ref.handler_call_count, 1); //! 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 alloc::boxed::Box;
use downcast_rs::Downcast; use downcast_rs::Downcast;
use spacepackets::ecss::{PusError, PusPacket}; use spacepackets::ecss::{PusError, PusPacket};
@ -94,7 +94,7 @@ pub enum PusDistribError<E> {
PusError(PusError), PusError(PusError),
} }
impl<E: 'static> ReceivesTcBase for PusDistributor<E> { impl<E: 'static> ReceivesTcCore for PusDistributor<E> {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> {
// Convert to ccsds and call pass_ccsds // Convert to ccsds and call pass_ccsds
@ -104,8 +104,6 @@ impl<E: 'static> ReceivesTcBase for PusDistributor<E> {
} }
} }
impl<E: 'static> ReceivesTc for PusDistributor<E> {}
impl<E: 'static> ReceivesCcsdsTc for PusDistributor<E> { impl<E: 'static> ReceivesCcsdsTc for PusDistributor<E> {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), Self::Error> { fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), Self::Error> {