Skip to content

Loading enum succeeds even if number of variants doesn't match saved type #131

@watsaig

Description

@watsaig

I'm not sure if this is an issue or expected behavior, but I'm surprised that this doesn't cause an error.

If I have a dataset with an enum type, and attempt to read it as a different enum, reading succeeds but results in incorrect values.

Concretely:

use hdf5_metno::{File, H5Type};
use ndarray::Ix1;

#[derive(H5Type, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum First {
    A = 0,
    B = 1,
    C = 2,
}

#[derive(H5Type, Debug)]
#[repr(u8)]
enum Second {
    A = 0,
    B = 1,
    C = 2,
    D = 3,
    E = 4,
}

fn main() -> anyhow::Result<()> {
    let file = File::create("test.h5")?;
    let to_write = [Second::A, Second::B, Second::C, Second::D, Second::E];
    let dataset = file.new_dataset::<Second>().shape([5]).create("second")?;
    dataset.write(&to_write)?;
    file.close()?;

    let file = File::open("test.h5")?;
    let dataset = file.dataset("second")?;
    let data_in_as_second = dataset.read::<Second, Ix1>()?;
    println!("data read as `Second`: {:?}", data_in_as_second);
    let data_in_as_first = dataset.read::<First, Ix1>()?;
    println!("data read as `First`: {:?}", data_in_as_first);

    Ok(())
}

Outputs:

data read as `Second`: [A, B, C, D, E], shape=[5], strides=[1], layout=CFcf (0xf), const ndim=1
data read as `First`: [A, B, C, A, A], shape=[5], strides=[1], layout=CFcf (0xf), const ndim=1
Cargo.toml
[package]
name = "hdf5_enums"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.100"
hdf5-metno = "0.11.0"
ndarray = "0.17.1"

Should read return an error in this kind of situation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions