这是一个以 to_descriptor() 方法开头的工作解决方案,eggyal 在 cmets 上发布。
获得TypeDescriptor 值后,我们需要处理所有可能的情况(Rust 方式)。
match ds_type.to_descriptor().unwrap() {
hdf5::types::TypeDescriptor::Float(_) => {
self.show_dataset::<f64>(&ds, dataset, ctx, &mut is_open);
}
hdf5::types::TypeDescriptor::VarLenUnicode => {
self.show_dataset::<hdf5::types::VarLenUnicode>(
&ds,
dataset,
ctx,
&mut is_open,
);
}
hdf5::types::TypeDescriptor::Integer(_) => {
self.show_dataset::<i64>(&ds, dataset, ctx, &mut is_open);
}
hdf5::types::TypeDescriptor::Unsigned(_) => {
self.show_dataset::<u64>(&ds, dataset, ctx, &mut is_open);
}
hdf5::types::TypeDescriptor::Boolean => {
self.show_dataset::<bool>(&ds, dataset, ctx, &mut is_open);
}
hdf5::types::TypeDescriptor::Enum(_) => todo!(),
hdf5::types::TypeDescriptor::Compound(_) => todo!(),
hdf5::types::TypeDescriptor::FixedArray(_, _) => todo!(),
hdf5::types::TypeDescriptor::FixedAscii(_) => todo!(),
hdf5::types::TypeDescriptor::FixedUnicode(_) => todo!(),
hdf5::types::TypeDescriptor::VarLenArray(_) => todo!(),
hdf5::types::TypeDescriptor::VarLenAscii => {
self.show_dataset::<hdf5::types::VarLenAscii>(
&ds,
dataset,
ctx,
&mut is_open,
);
}
}
在稍后的处理中,如果是标量,我只需通过std::fmt::Display 显示标量类型的内容。我在那里使用了 generic types 以避免在处理不同类型时代码重复。
在非标量(矢量)的情况下,我使用read_raw 加载它。
fn show_dataset<T: hdf5::H5Type + std::fmt::Display>(
&mut self,
ds: &hdf5::Dataset,
dataset: &str,
ctx: &egui::Context,
is_open: &mut bool,
) {
if ds.is_scalar() {
let x_data: T = ds.read_scalar().unwrap();
Window::new(dataset.to_owned())
.open(is_open)
.vscroll(true)
.resizable(true)
.default_height(300.0)
.show(ctx, |ui| {
ui.label(format!("{}", x_data));
});
} else {
let x_data: Vec<T> = ds.read_raw().unwrap();
let mut table_box = Box::<super::table::TableWindow<T>>::default();
table_box.set_name(dataset.to_owned());
table_box.set_data(x_data);
table_box.show(ctx, is_open);
}
}
不过,此解决方案有一个警告。
正如您可能已经注意到的,下面这些类型没有被处理。在我的应用程序中,不需要处理这些类型,但通常我不知道如何处理这些类型。
hdf5::types::TypeDescriptor::FixedArray(_, _) => todo!(),
hdf5::types::TypeDescriptor::FixedAscii(_) => todo!(),
hdf5::types::TypeDescriptor::FixedUnicode(_) => todo!(),