Hi
I've just started to study embedded programming with Rust and slammed into a problem with the abstraction model Rust uses. I'm trying to create a multi-protocol radio frequencies scanner, with a simple visualization, so I came to the next design:
- There is a Sequence trait with "scan" and "has_next" methods
- There is a Receiver trait which abstracts a hardware interface providing a "set_frequency" method
- There are a few implementation of this trait each for a specific device
- There is a Band struct holding a Receiver plus an array of channels for it (i.e. frequencies), just a set of u32 values, there is an implementation of the Sequence trait for the Band
- Finally, there is a Scanner which holds an array of Bands, and there is an implementation of the Sequence trait for the Scanner which simply loops through all of it's Bands.
The problem is, when I'm creating an instance of the Scanner, and passing it an array of Bands it "locks" the generics constraints to the type of a first item in array, i.e.:
pub struct Scanner<R: Receiver, const B: usize, const S: usize> {
bands: [Band<R, B>; S],
index: usize,
}
impl<R: Receiver, const B: usize, const S: usize> Scanner<R, B, S> {
fn new(bands: [Band<R, B>; S]) -> Self {
return Self {
bands: bands,
index: 0,
};
}
}
fn main() {
let scanner = Scanner::new([
Band::new(R1::new(), [1, 2]),
Band::new(R2::new(), [3])
]);
}
results in
error[E0308]: mismatched types
--> src/main.rs:105:19
|
105 | Band::new(R2::new(), [3, 4])
| --------- ^^^^^^^^^ expected `R1`, found `R2`
| |
| arguments to this function are incorrect
I've created a full example on the Playground for a better understanding of the context.
As far as I understood this limitation comes from static memory allocation typical for a no_std environments...how may I manage to workaround this issue, probably there is some design pattern for such a scenario?