aldanor / hdf5-rust

HDF5 for Rust
https://docs.rs/hdf5
Apache License 2.0
310 stars 84 forks source link

`T` in `VarLenArray<T>` cannot contain another `VarLenArray` #222

Open AlanRace opened 1 year ago

AlanRace commented 1 year ago

Hello, I have a file where one dataset is stored as a VarLenArray of a custom compound which contains at least one VarLenArray. The following code does not work with the current version as the trait Copy is not implemented for VarLenArray

#[derive(H5Type, Copy, Debug)] // register with HDF5
#[repr(C)]
pub struct Cluster {
    id: i32,
    name: VarLenAscii,
    colour: i32,
    neighbours: VarLenArray<i32>,
}

fn read_clusters(dataset: Dataset) -> Result<()> {
    let clusters: VarLenArray<Cluster> = dataset.read_scalar()?;

    println!("{:?}", clusters);

    // ... process clusters ...

    Ok(())
}

Results in the error:

error[E0204]: the trait `Copy` may not be implemented for this type
  --> src\main.rs:60:18
   |
60 | #[derive(H5Type, Copy, Debug)] // register with HDF5
   |                  ^^^^
...
63 |     name: VarLenAscii,
   |     ----------------- this field does not implement `Copy`
...
67 |     neighbours: VarLenArray<i32>,
   |     -------------------------- this field does not implement `Copy`

If I change VarLenArray to requiring Clone instead of Copy then I can get this to compile, run and read in the correct data, but I am unsure what effects that has on the code. What would be a good solution to this problem?

mulimoen commented 1 year ago

I would think it should be OK to lower from Copy to Clone. I would recommend creating a PR for this and see if CI complains ;)