Thanks in advance for the help. I'm coming to rust as an embedded C developer. I'm trying to create an iterator to loop over the members of this struct. I've found a few solutions struct_iterable, or using 'any', but being in a no_std environment my understanding is these aren't available to me (Is my assumption correct?)
The generics for the DataChannel types are "<const TAG: u8, T: SimpleValue>" where SimpleValue is a trait I've defined. The generics are confusing me as what the "type Item" should be. Intuitively I would think that because each of the members supports the same functions (defined by the 'SimpleValue' trait) that this is workable, but this level of abstraction is new territory for me .
I've been reading up on this problem but getting a little lost. Any nudge in the right direction is appreciated. Thanks again!
Each TAG value and T type makes your SimpleTlv<TAG, T> a separate and distinct type. You would have to make a type that can encode that distinction as a runtime value. The most straight forward in your case may be an enum with a variant for each outcome.
If the value type is the same at its core, you can also consider a struct. You can have converters to and from the static types.
struct AnySimpleTlv {
tag: u8,
value: f32, // example
}
A third alternative would be a trait object. no_std complicates this, but there are crates for "inline" or "small" boxes that don't need a heap allocation. They have a limited capacity instead, but your use case should probably be fine with that.
If you do end up going with the dynamic dispatch solution, you could consider using a lending iterator: make a new struct like DataChannelSendStateIter that borrows the original struct and keeps track of where it is in the iteration, and return &dyn Any (or whatever trait you decide to use).
Thanks for you're help and insight. Yes here is more background.
The problem I'm trying to solve is having a variety of sensor types and channels whose data can arrive in an undetermined order. Some are more interrupt based while others are read explicitly, and take various amounts of time to retrieve. I would like to know when all (of those that are enabled) have arrived, and then pack their values into a buffer to be transmitted. It reduces overhead to send the values as a single packet.
Having this be a struct (as opposed to a vec) allows the ability to access each field directly. The reason I'd like to loop over the struct is for the following behavior:
Loop over each member and check that of those enabled, they have received their reading
Loop over and pack the data
There were a few holes in my knowledge that this post has helped clear up for me (generics creating a unique type, and how to achieve runtime polymorphism with "dyn"). I'm also planning to post some code soon when I can get time to.
All that said if there's a better way to implement/architect the behavior I'm looking for, I'd love to hear it for the sake of learning.
This way, you can iterate over fields() to do your tasks. You’ll need to add methods to ErasedDataChannel for whatever operations you need to do in those loops.