I'm currently working on a data processing project for which I have to store a set of samples with a certain timestamp along with data of different types. There is then a channel struct holding a vector of these samples. I wish to isolate the generic sample from the channel given that I want to hold these channels in a hashmap where I can later retrieve data from a channel by some channel ID.
Given that I wish to isolate the generic sample, I opted for this channel struct to hold a vector of trait objects. Everything seemed to work fine and I managed to fill these channels with my data, but I can't find any way of actually retrieving the data from those samples again.
This is an outline of the code:
trait SampleContainer: Debug {}
impl<T: Debug> SampleContainer for Sample<T> {}
#[derive(Debug)]
struct Sample<T> {
time: u64,
data: T
}
#[derive(Debug)]
struct Channel {
// There would be some channel configuration struct here
samples: Vec<Box<SampleContainer>>
}
fn main() {
let new_sample = Box::new(Sample::<u8> {time: 0, data: 10});
let another_new_sample = Box::new(Sample::<u8> {time: 1, data: 11});
let mut sample_list: Vec<Box<SampleContainer>> = Vec::new();
sample_list.push(new_sample);
sample_list.push(another_new_sample);
let mut channel_list: HashMap<u16, Channel> = HashMap::new();
let new_channel = Channel {samples: sample_list};
channel_list.insert(1, new_channel);
// This prints what I would expect: [Sample { time: 0, data: 10 }, Sample { time: 1, data: 11 }]
println!("{:?}", channel_list[&1].samples);
}
How would I access the underlying sample data in my channels? Or am I going about this wrong entirely?
I look forward to receiving any input!