Crate hvec[−][src]
Expand description
In memory of Anna Harren, who coined the term turbofish - which you’ll see a lot of if you use this crate.
The main purpose of this crate is the HarrenVec type -
a Vec-like data structure that can store items
of different types and sizes from each other.
Values of any type can be stored, and they are
stored contiguous in memory like a normal Vec would.
It supports values with a Drop implementation, however
dropping the HarrenVec will not call the destructors of
the contents. Instead you should use the
HarrenVec::into_iter method to ensure you are consuming
and dropping values correctly. If the values do not have
destructors, this is not necessary.
The intended use case for this data structure is
efficiently packing structs with large optional fields,
while avoiding Box-ing those values.
Examples
use hvec::HarrenVec;
struct SmallMessage {
id: usize,
has_extra: bool,
}
struct LargeExtraField {
data: [[f64; 4]; 4],
}
let mut messages = HarrenVec::new();
messages.push(SmallMessage { id: 0, has_extra: false });
messages.push(SmallMessage { id: 1, has_extra: true });
messages.push(LargeExtraField { data: [[0.; 4]; 4] });
messages.push(SmallMessage { id: 2, has_extra: false });
let mut iter = messages.into_iter();
while let Some(message) = iter.next::<SmallMessage>() {
println!("id = {}", message.id);
if message.has_extra {
let extra = iter.next::<LargeExtraField>().unwrap();
println!("extra data = {:?}", extra.data);
}
}
// Output:
// id = 0
// id = 1
// extra data = [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]
// id = 2