put hashbrown behind alloc feature

This commit is contained in:
Robin Müller 2023-01-03 00:22:48 +01:00
parent e85e953c93
commit 1fcc8d3f57
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
5 changed files with 47 additions and 40 deletions

View File

@ -7,11 +7,14 @@ edition = "2021"
[dependencies]
delegate = "0.8"
hashbrown = "0.13"
paste = "1.0"
dyn-clone = "1.0"
embed-doc-image = "0.1"
[dependencies.hashbrown]
version = "0.13"
optional = true
[dependencies.heapless]
version = "0.7"
optional = true
@ -55,7 +58,7 @@ version = "1.0"
[features]
default = ["std"]
std = ["downcast-rs/std", "alloc", "bus", "postcard/use-std", "crossbeam-channel/std", "serde/std", "spacepackets/std"]
alloc = ["serde/alloc", "spacepackets/alloc"]
alloc = ["serde/alloc", "spacepackets/alloc", "hashbrown"]
serde = ["dep:serde", "spacepackets/serde"]
crossbeam = ["crossbeam-channel"]
heapless = ["dep:heapless"]

View File

@ -63,6 +63,7 @@ use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::slice::Iter;
#[cfg(feature = "alloc")]
use hashbrown::HashMap;
#[cfg(feature = "std")]

View File

@ -3,7 +3,9 @@ use crate::events::{EventU32, GenericEvent, Severity};
use crate::events::{EventU32TypedSev, HasSeverity};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use core::hash::Hash;
#[cfg(feature = "alloc")]
use hashbrown::HashSet;
#[cfg(feature = "alloc")]
@ -38,43 +40,6 @@ pub trait PusEventMgmtBackendProvider<Provider: GenericEvent> {
fn disable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error>;
}
/// Default backend provider which uses a hash set as the event reporting status container
/// like mentioned in the example of the [PusEventMgmtBackendProvider] documentation.
///
/// This provider is a good option for host systems or larger embedded systems where
/// the expected occasional memory allocation performed by the [HashSet] is not an issue.
pub struct DefaultPusMgmtBackendProvider<Event: GenericEvent = EventU32> {
disabled: HashSet<Event>,
}
/// Safety: All contained field are [Send] as well
unsafe impl<Event: GenericEvent + Send> Send for DefaultPusMgmtBackendProvider<Event> {}
impl<Event: GenericEvent> Default for DefaultPusMgmtBackendProvider<Event> {
fn default() -> Self {
Self {
disabled: HashSet::default(),
}
}
}
impl<Provider: GenericEvent + PartialEq + Eq + Hash + Copy + Clone>
PusEventMgmtBackendProvider<Provider> for DefaultPusMgmtBackendProvider<Provider>
{
type Error = ();
fn event_enabled(&self, event: &Provider) -> bool {
!self.disabled.contains(event)
}
fn enable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
Ok(self.disabled.remove(event))
}
fn disable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
Ok(self.disabled.insert(*event))
}
}
#[cfg(feature = "heapless")]
pub mod heapless_mod {
use super::*;
@ -145,6 +110,43 @@ impl<SenderE> From<EcssTmError<SenderE>> for EventManError<SenderE> {
pub mod alloc_mod {
use super::*;
/// Default backend provider which uses a hash set as the event reporting status container
/// like mentioned in the example of the [PusEventMgmtBackendProvider] documentation.
///
/// This provider is a good option for host systems or larger embedded systems where
/// the expected occasional memory allocation performed by the [HashSet] is not an issue.
pub struct DefaultPusMgmtBackendProvider<Event: GenericEvent = EventU32> {
disabled: HashSet<Event>,
}
/// Safety: All contained field are [Send] as well
unsafe impl<Event: GenericEvent + Send> Send for DefaultPusMgmtBackendProvider<Event> {}
impl<Event: GenericEvent> Default for DefaultPusMgmtBackendProvider<Event> {
fn default() -> Self {
Self {
disabled: HashSet::default(),
}
}
}
impl<Provider: GenericEvent + PartialEq + Eq + Hash + Copy + Clone>
PusEventMgmtBackendProvider<Provider> for DefaultPusMgmtBackendProvider<Provider>
{
type Error = ();
fn event_enabled(&self, event: &Provider) -> bool {
!self.disabled.contains(event)
}
fn enable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
Ok(self.disabled.remove(event))
}
fn disable_event_reporting(&mut self, event: &Provider) -> Result<bool, Self::Error> {
Ok(self.disabled.insert(*event))
}
}
pub struct PusEventDispatcher<BackendError, Provider: GenericEvent> {
reporter: EventReporter,
backend: Box<dyn PusEventMgmtBackendProvider<Provider, Error = BackendError>>,

View File

@ -95,7 +95,7 @@ pub use alloc_mod::{
};
use crate::seq_count::SequenceCountProvider;
#[cfg(feature = "crossbeam")]
#[cfg(all(feature = "crossbeam", feature = "std"))]
pub use stdmod::CrossbeamVerifSender;
#[cfg(feature = "std")]
pub use stdmod::{

View File

@ -1,3 +1,4 @@
// TODO: Refactor this to also test the STD impl using mpsc
#[cfg(feature = "crossbeam")]
pub mod crossbeam_test {
use hashbrown::HashMap;