From 245388fafaf92aa96f6c5b2c82c44147252ff20a Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 3 Jan 2023 17:20:17 +0100 Subject: [PATCH] remove mut bound for seq counter impl --- satrs-core/src/pus/verification.rs | 16 +++---- satrs-core/src/seq_count.rs | 62 ++++++++++++++++++++-------- satrs-core/tests/pus_verification.rs | 4 +- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/satrs-core/src/pus/verification.rs b/satrs-core/src/pus/verification.rs index 461bb4b..5d2880e 100644 --- a/satrs-core/src/pus/verification.rs +++ b/satrs-core/src/pus/verification.rs @@ -340,7 +340,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), time_stamp: &[u8], ) -> Result, VerificationErrorWithToken> { @@ -371,7 +371,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { let tm = self @@ -399,7 +399,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), time_stamp: &[u8], ) -> Result, VerificationErrorWithToken> { @@ -432,7 +432,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { let tm = self @@ -460,7 +460,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: &VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), time_stamp: &[u8], step: impl EcssEnumeration, ) -> Result<(), EcssTmError> { @@ -486,7 +486,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), params: FailParamsWithStep, ) -> Result<(), VerificationErrorWithToken> { let tm = self @@ -515,7 +515,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), time_stamp: &[u8], ) -> Result<(), VerificationErrorWithToken> { let tm = self @@ -544,7 +544,7 @@ impl VerificationReporterCore { buf: &mut [u8], token: VerificationToken, sender: &mut (impl EcssTmSenderCore + ?Sized), - seq_counter: &mut (impl SequenceCountProviderCore + ?Sized), + seq_counter: &(impl SequenceCountProviderCore + ?Sized), params: FailParams, ) -> Result<(), VerificationErrorWithToken> { let tm = self diff --git a/satrs-core/src/seq_count.rs b/satrs-core/src/seq_count.rs index 24c4736..6871491 100644 --- a/satrs-core/src/seq_count.rs +++ b/satrs-core/src/seq_count.rs @@ -1,17 +1,35 @@ +use core::cell::Cell; +use core::sync::atomic::{AtomicU16, Ordering}; #[cfg(feature = "alloc")] use dyn_clone::DynClone; #[cfg(feature = "std")] pub use stdmod::*; /// Core trait for objects which can provide a sequence count. +/// +/// The core functions are not mutable on purpose to allow easier usage with +/// static structs when using the interior mutability pattern. This can be achieved by using +/// [Cell], [RefCell] or atomic types. pub trait SequenceCountProviderCore { fn get(&self) -> Raw; - fn increment(&mut self); - fn get_and_increment(&mut self) -> Raw { + + fn increment(&self); + + // TODO: Maybe remove this? + fn increment_mut(&mut self) { + self.increment(); + } + + fn get_and_increment(&self) -> Raw { let val = self.get(); self.increment(); val } + + // TODO: Maybe remove this? + fn get_and_increment_mut(&mut self) -> Raw { + self.get_and_increment() + } } /// Extension trait which allows cloning a sequence count provider after it was turned into @@ -25,25 +43,30 @@ impl SequenceCountProvider for T where T: SequenceCountProviderCore #[derive(Default, Clone)] pub struct SeqCountProviderSimple { - seq_count: u16, + seq_count: Cell, } impl SequenceCountProviderCore for SeqCountProviderSimple { fn get(&self) -> u16 { - self.seq_count + self.seq_count.get() } - fn increment(&mut self) { - if self.seq_count == u16::MAX { - self.seq_count = 0; - return; + fn increment(&self) { + self.get_and_increment(); + } + + fn get_and_increment(&self) -> u16 { + let curr_count = self.seq_count.get(); + + if curr_count == u16::MAX { + self.seq_count.set(0); + } else { + self.seq_count.set(curr_count + 1); } - self.seq_count += 1; + curr_count } } -use core::sync::atomic::{AtomicU16, Ordering}; - pub struct SeqCountProviderAtomicRef { atomic: AtomicU16, ordering: Ordering, @@ -51,7 +74,10 @@ pub struct SeqCountProviderAtomicRef { impl SeqCountProviderAtomicRef { pub const fn new(ordering: Ordering) -> Self { - Self { atomic: AtomicU16::new(0), ordering } + Self { + atomic: AtomicU16::new(0), + ordering, + } } } @@ -60,11 +86,11 @@ impl SequenceCountProviderCore for SeqCountProviderAtomicRef { self.atomic.load(self.ordering) } - fn increment(&mut self) { + fn increment(&self) { self.atomic.fetch_add(1, self.ordering); } - fn get_and_increment(&mut self) -> u16 { + fn get_and_increment(&self) -> u16 { self.atomic.fetch_add(1, self.ordering) } } @@ -75,20 +101,20 @@ pub mod stdmod { use std::sync::Arc; #[derive(Clone, Default)] - pub struct SeqCountProviderSync { + pub struct SeqCountProviderSyncClonable { seq_count: Arc, } - impl SequenceCountProviderCore for SeqCountProviderSync { + impl SequenceCountProviderCore for SeqCountProviderSyncClonable { fn get(&self) -> u16 { self.seq_count.load(Ordering::SeqCst) } - fn increment(&mut self) { + fn increment(&self) { self.seq_count.fetch_add(1, Ordering::SeqCst); } - fn get_and_increment(&mut self) -> u16 { + fn get_and_increment(&self) -> u16 { self.seq_count.fetch_add(1, Ordering::SeqCst) } } diff --git a/satrs-core/tests/pus_verification.rs b/satrs-core/tests/pus_verification.rs index 57c43ad..6023dfb 100644 --- a/satrs-core/tests/pus_verification.rs +++ b/satrs-core/tests/pus_verification.rs @@ -7,7 +7,7 @@ pub mod crossbeam_test { CrossbeamVerifSender, FailParams, RequestId, VerificationReporterCfg, VerificationReporterWithSender, }; - use satrs_core::seq_count::SeqCountProviderSync; + use satrs_core::seq_count::SeqCountProviderSyncClonable; use spacepackets::ecss::{EcssEnumU16, EcssEnumU8, PusPacket}; use spacepackets::tc::{PusTc, PusTcSecondaryHeader}; use spacepackets::tm::PusTm; @@ -34,7 +34,7 @@ pub mod crossbeam_test { // each reporter have an own sequence count provider. let cfg = VerificationReporterCfg::new( TEST_APID, - Box::new(SeqCountProviderSync::default()), + Box::new(SeqCountProviderSyncClonable::default()), 1, 2, 8,